数据来源: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)

 

相关文章:

  • 2021-11-12
  • 2021-05-23
  • 2021-09-05
  • 2021-12-03
  • 2021-09-02
  • 2022-01-14
  • 2021-05-10
  • 2021-10-23
猜你喜欢
  • 2021-07-22
  • 2021-05-31
  • 2021-08-19
  • 2021-10-23
  • 2021-12-06
  • 2021-08-05
  • 2021-06-02
相关资源
相似解决方案