chapter1

说明:本系列代码均来源于–《Python数据分析于数据化运营》–宋天龙,

嵌入Matplotlib图像

%matplotlib inline
%config InlineBackend.figure_format='retina'

1. 导入库

import pandas as pd
from matplotlib import pyplot as plt
from sklearn import linear_model
from sklearn.metrics import mean_squared_error, r2_score

2. 读取数据

raw_data = pd.read_csv('data.txt')

3. 数据预处理

num = int(raw_data.shape[0]*0.7)
x,y = raw_data[['money']],raw_data[['amount']]
x_train,x_test = x[:num],x[num:]
y_train,y_test = y[:num],y[num:]

4. 探索性数据分析

plt.scatter(x_train, y_train)
<matplotlib.collections.PathCollection at 0x19fcc6ba2b0>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ccQiZwpY-1608615350453)(output_14_1.png)]

5. 数据建模

model = linear_model.LinearRegression()
model.fit(x_train, y_train)
LinearRegression()

6. 模型评估

predict_test_y = model.predict(x_test)
print("Mean squared error: %.0f"
      % mean_squared_error(y_test, predict_test_y))
print('Variance score: %.2f' % r2_score(y_test, predict_test_y))
Mean squared error: 65823229
Variance score: 0.72

7. 线性回归参数

提示:该步骤不是必须的

model_coef = model.coef_
model_intercept = model.intercept_
print('coef is: ',model_coef)
print('intercept is: ',model_intercept)
coef is:  [[2.20734937]]
intercept is:  [9301.37803984]

8. 销售预测应用

new_x = 84610
pre_y = model.predict(new_x)
print(pre_y)
[[196065.20852406]]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

灯下夜无眠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值