快捷搜索: 王者荣耀 脱发

用Python 实现数据化运营分析实例——销售预测

#案例场景:每个销售公司都有一定的促销费用,促销费用可以带来销量的显著提升;当给一定的促销费用时,预计会带来多大的商品销量呢!

#维度数量:1

#记录数:100

#字段变量:第一列是促销费用,第二列是商品销量。

#数据类型:全部是浮点数值型。

#是否有缺失值:否

#使用python3 编写,utf-8编码

代码如下:

import re #正则表达式

import numpy

from sklearn import linear_model #算法模型库 使用的是线性回归linear_model

from matplotlib import pyplot as plt #图形展示库

fn=open(data.txt,r)

all_data=fn.readlines()

#print(all_data)

fn.close()

x=[]

y=[]

for single_data in all_data:

tmp_data=re.split( | ,single_data)

x.append(float(tmp_data[0]))

y.append(float(tmp_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_intercept=model.intercept_

r2=model.score(x,y)

new_x=70000

pre_y=model.predict(new_x)

print(pre_y)

源码连接:https://github.com/Bigger666/data#data

经验分享 程序员 微信小程序 职场和发展