python数据化运营分析实例---销售预测

时间:2022-07-26
本文章向大家介绍python数据化运营分析实例---销售预测,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

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