-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage218.py
More file actions
78 lines (66 loc) · 2.21 KB
/
Copy pathpage218.py
File metadata and controls
78 lines (66 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import pandas as pd
import seaborn as sns
tips = sns.load_dataset('tips')
print(tips.head())
import statsmodels.formula.api as smf
model = smf.ols(formula = 'tip ~ total_bill', data = tips)
results = model.fit()
print(results.summary())
print(results.params)
print(results.conf_int())
from sklearn import linear_model
lr = linear_model.LinearRegression()
'''predicted = lr.fit(X = tips['total_bill'],
y = tips['tip'])'''
# 请注意,X是大写,y是小写
# 重塑数据,使其符合sklearn的要求
predicted = lr.fit(X = tips['total_bill'].values.reshape(-1, 1),
y = tips['tip'])
print(predicted.coef_)
print(predicted.intercept_)
model = smf.ols(formula = 'tip ~ total_bill + size', data = tips).fit()
print(model.summary())
print(tips.info())
print(tips.sex.unique())
model = smf.ols(
formula = 'tip ~ total_bill + size + sex + smoker + day + time',
data = tips
).\
fit()
print(model.summary())
print(tips.day.unique())
lr = linear_model.LinearRegression()
# 由于执行的是多元回归,所以无需重塑X值
predicted = lr.fit(X = tips[['total_bill', 'size']],
y = tips['tip'])
print(predicted.coef_)
print(predicted.intercept_)
tips_dummy = pd.get_dummies(
tips[['total_bill', 'size',
'sex', 'smoker', 'day', 'time']]
)
print(tips_dummy.head())
x_tips_dummy_ref = pd.get_dummies(
tips[['total_bill', 'size',
'sex', 'smoker', 'day', 'time']], drop_first = True
)
print(x_tips_dummy_ref.head())
# 拟合模型
lr = linear_model.LinearRegression()
predicted = lr.fit(X = x_tips_dummy_ref,
y = tips['tip'])
print(predicted.coef_)
print(predicted.intercept_)
import numpy as np
# 创建模型并拟合
lr = linear_model.LinearRegression()
predicted = lr.fit(X = x_tips_dummy_ref, y = tips['tip'])
# 获取截距以及其他系数
values = np.append(predicted.intercept_, predicted.coef_)
# 获取值的名称
names = np.append('intercept', x_tips_dummy_ref.columns)
# 把所有项放入一个带标签的DataFrame中
results = pd.DataFrame(values, index = names,
columns = ['coef'] # 这里用方括号
)
print(results)