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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM