sklearn代码21 2-2020天猫双十一销量

# 认为天猫销量与年份之间存在函数关系,一元二次,一元三次
import numpy as np

import matplotlib.pyplot as plt
%matplotlib inline
years = np.arange(2009,2020)
years
array([2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019])
sales = np.array([0.5,9.36,52,191,352,571,912,1207,1682.69,2135,2684])

sales    #此时使用的是科学计数法来表示
array([  5.00000000e-01,   9.36000000e+00,   5.20000000e+01,
         1.91000000e+02,   3.52000000e+02,   5.71000000e+02,
         9.12000000e+02,   1.20700000e+03,   1.68269000e+03,
         2.13500000e+03,   2.68400000e+03])
plt.scatter(years,sales,c= 'red',marker='*',s=80)   # s是用来调节marker的大小的
<matplotlib.collections.PathCollection at 0x23114df3a58>

请添加图片描述

X = (years-2008).reshape(-1,1)
X
array([[ 1],
       [ 2],
       [ 3],
       [ 4],
       [ 5],
       [ 6],
       [ 7],
       [ 8],
       [ 9],
       [10],
       [11]])
y = sales
y
array([  5.00000000e-01,   9.36000000e+00,   5.20000000e+01,
         1.91000000e+02,   3.52000000e+02,   5.71000000e+02,
         9.12000000e+02,   1.20700000e+03,   1.68269000e+03,
         2.13500000e+03,   2.68400000e+03])
from sklearn.linear_model import LinearRegression
lr = LinearRegression(fit_intercept=True)

lr.fit(X,y)
# weight 权重
w = lr.coef_
# bias 偏差
b = lr.intercept_
display(w,b)
plt.scatter(years-2008,sales,c= 'red',marker='*',s=80)  

x = np.linspace(1,12,50)

plt.plot(x,w*x + b, c = 'green')
array([ 267.31027273])



-713.26618181818299





[<matplotlib.lines.Line2D at 0x231177ae400>]

请添加图片描述

X2 = np.concatenate([X**2,X],axis = 1)
X2.shape   #属性有两个
(11, 2)
# 假定函数是一元二次f(x) = w1*x**2 +w2*x +b
lr = LinearRegression(fit_intercept=True)

X2 = np.concatenate([X**2,X],axis = 1)

lr.fit(X2,y)
# weight 权重  有多少个属性就有多少个
w1,w2 = lr.coef_
# bias 偏差 有多少个方程就有多少个
b = lr.intercept_
display(w1,w2,b)
plt.scatter(years-2008,sales,c= 'red',marker='*',s=80)  

x = np.linspace(1,12,50)

f = lambda x : w1*x**2 +w2*x + b

plt.plot(x,f(x), c = 'green')

# 年2009---1
# 2020-----12
print('2020年天猫双十一销量预测:',np.round(f(12),1))
30.215582750582751



-95.276720279720209



72.338969696969116


2020年天猫双十一销量预测: 3280.1

请添加图片描述

# 假定函数是一元三次f(x) = w1*x**2 +w2*x +b
lr = LinearRegression(fit_intercept=True)

X3 = np.concatenate([X**3,X**2,X],axis = 1)

lr.fit(X3,y)
# weight 权重  有多少个属性就有多少个
w1,w2,w3 = lr.coef_
# bias 偏差 有多少个方程就有多少个
b = lr.intercept_

plt.scatter(years-2008,sales,c= 'red',marker='*',s=80)  

x = np.linspace(1,12,50)

f = lambda x : w1*x**3 +w2*x**2+w3*x + b

plt.plot(x,f(x), c = 'green')

# 年2009---1
# 2020-----12
print('2020年天猫双十一销量预测:',np.round(f(12),1))
2020年天猫双十一销量预测: 3294.2

请添加图片描述

#瑞幸咖啡,30K
#本科,华为3年,测试,华为的外包
#线性回归,预测咖啡销量
#提前进货,大概,货物积存,占用现金流

# 推荐系统 效益,每日纯公交量500万
# 不好,100万
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值