04-作业练习

用三次回归来预测天猫2019年双11销售额

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import numpy as np
data = np.array([
    [9,0.5],
    [10,9.36],
    [11,52],
    [12,191],
    [13,350.19],
    [14,571],
    [15,912.17],
    [16,1207],
    [17,1682],
    [18,2135]
])
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
x = data[:,0]
actual = data[:,1]
np.set_printoptions(suppress=True)
from sympy import *  #导入计算库
m_1, m_2,m_3, b = symbols('m_1, m_2, m_3, b') #声明变量x,y,z
init_printing(pretty_print=True) #初始化latex显示

from sympy import *  #导入计算库
x_i,Actual_i,i= symbols('x_i,Actual_i,i') #声明变量x,y,z
init_printing(pretty_print=True) #初始化latex显示
expr = Sum((m_1*x_i**3 + m_2*x_i**2+m_3*x_i + b-Actual_i)**2, (i, 2009, 2018))

expr

\displaystyle \sum_{i=2009}^{2018} \left(- Actual_{i} + b + m_{1} x_{i}^{3} + m_{2} x_{i}^{2} + m_{3} x_{i}\right)^{2}

1
2
3
4
expand(diff(expr,m_1))
expand(diff(expr,m_2))
expand(diff(expr,m_3))
expand(diff(expr,b))
\displaystyle \sum_{i=2009}^{2018} - 2 Actual_{i} x_{i}^{3} + \sum_{i=2009}^{2018} 2 b x_{i}^{3} + \sum_{i=2009}^{2018} 2 m_{1} x_{i}^{6} + \sum_{i=2009}^{2018} 2 m_{2} x_{i}^{5} + \sum_{i=2009}^{2018} 2 m_{3} x_{i}^{4} = 0
\displaystyle \sum_{i=2009}^{2018} - 2 Actual_{i} x_{i}^{2} + \sum_{i=2009}^{2018} 2 b x_{i}^{2} + \sum_{i=2009}^{2018} 2 m_{1} x_{i}^{5} + \sum_{i=2009}^{2018} 2 m_{2} x_{i}^{4} + \sum_{i=2009}^{2018} 2 m_{3} x_{i}^{3} = 0
\displaystyle \sum_{i=2009}^{2018} - 2 Actual_{i} x_{i} + \sum_{i=2009}^{2018} 2 b x_{i} + \sum_{i=2009}^{2018} 2 m_{1} x_{i}^{4} + \sum_{i=2009}^{2018} 2 m_{2} x_{i}^{3} + \sum_{i=2009}^{2018} 2 m_{3} x_{i}^{2} = 0
\displaystyle \sum_{i=2009}^{2018} - 2 Actual_{i} + \sum_{i=2009}^{2018} 2 b + \sum_{i=2009}^{2018} 2 m_{1} x_{i}^{3} + \sum_{i=2009}^{2018} 2 m_{2} x_{i}^{2} + \sum_{i=2009}^{2018} 2 m_{3} x_{i} =0

联立上诉四个方程,解出m_1,m_2,m_3和b

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
sum1 = 0
sum2 = 0
sum3 = 0
sum4 = 0
for index,item in enumerate(actual):
    sum1 = sum1 -(item*x[index]**3) +b*(x[index]**3)+ m_1*(x[index]**6)+ m_2*(x[index]**5)+m_3*(x[index]**4)
    sum2 = sum2 -(item*x[index]**2) +b*(x[index]**2)+ m_1*(x[index]**5)+ m_2*(x[index]**4)+m_3*(x[index]**3)
    sum3 = sum3 -(item*x[index]) + b*x[index] + m_1*(x[index]**4)+m_2*(x[index]**3)+m_3*(x[index]**2)
    sum4 = sum4 -(item) + b + m_1*(x[index]**3)+m_2*(x[index]**2)+m_3*(x[index])

print(sum1)
print(sum2)
print(sum3)
print(sum4)
result = solve([sum1,sum2,sum3,sum4],[m_1,m_2,m_3,b])
1
2
3
4
预测
x,y = symbols('x,y ') #声明变量x,y,z
y = m_1*x**3 + m_2*x**2 +m_3*x+ b
y.subs([(x,19),(m_1,result[m_1]),(m_2,result[m_2]),(m_3,result[m_3]),(b,result[b])])

绘制表格

1
2
3
4
5
6
7
8
import matplotlib.pyplot as plt
import numpy as np
%matplotlib notebook  
fig = plt.figure(figsize=(6, 6), dpi=80)
XX = np.linspace(9,22,14)
YY = np.power(XX,3)*result[m_1] + np.power(XX,2)*result[m_2] +np.power(XX,1)*result[m_3]+ result[b]  
plt.scatter(XX,YY)
plt.show()

image-20191116161555757