数据来源:https://pan.baidu.com/s/1a5kcBy0O0LGO8vo5SXI2Hw
第一步:导入库
import re
import numpy
from sklearn import linear_model
from matplotlib import pyplot as plt
第二步:导入数据
fn = open("C:/Users/***/Desktop/Python数据分析与数据化运营/chapter1/data.txt")
all_data = fn.readlines()
fn.close()
第三步:数据预处理
x=[]
y=[]
for single_data in all_data:
temp_data=re.split('\t|\n',single_data)
x.append(float(temp_data[0]))
y.append(float(temp_data[1]))
x=numpy.array(x).reshape([100,1])
y=numpy.array(y).reshape([100,1])
第四步:数据分析
plt.scatter(x,y)
plt.show()
第五步:数据建模
model = linear_model.LinearRegression()
model.fit(x,y)
第六步:模型评估
model_coef = model.coef_ #获取模型自变量系数并赋值给model_coef
model_intercept = model.intercept_ #获取模型的截距并赋值给model_intercept
r2 = model.score(x,y) #回归方程 y = model_coef*x + model_intercept
第七步:销售预测
new_x = 84610
pre_y = model.predict(new_x)
print(pre_y)
本文分享自 Python爬虫数据分析挖掘 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体分享计划 ,欢迎热爱写作的你一起参与!