Python完成的数据化运营案例——销售预测

说明

  • Python版本:64位 3.7
  • 依赖库:pandas、matplotlib、sklearn
  • 程序输入:data.txt
  • 程序输出:预测的销售量

1.导入库

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from sklearn import linear_model
from sklearn.metrics import mean_squared_error, r2_score

2. 读取数据

raw_data = pd.read_csv('data.txt')

3. 数据预处理

num = int(raw_data.shape[0] * 0.7)
x, y = raw_data[['money']], raw_data[['amount']]
x_train, x_test = x[:num], x[num:]
y_train, y_test = y[:num], y[num:]

4. 探索性数据分析

plt.scatter(x_train, y_train)
plt.show()

5. 数据建模

model = linear_model.LinearRegression()
model.fit(x_train, y_train)

6. 模型评估

predict_test_y = model.predict(x_test)
print("Mean squared error: %.0f"
      % mean_squared_error(y_test, predict_test_y))
print('Variance score: %.2f' % r2_score(y_test, predict_test_y))

7. 线性回归参数

model_coef = model.coef_
model_intercept = model.intercept_
print('coef is: ', model_coef)
print('intercept is: ', model_intercept)

8. 销售预测应用

new_x = 84610
pre_y = model.predict(np.array(new_x).reshape(1, -1))
pre_y = model.predict(new_x)

注:数据文件链接链接: https://pan.baidu.com/s/1eDCkYuNu_reOWzJJGkd8zw 密码: atju

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值