中国汽车销售数据分析及预测,本数据为时间序列数据,建模也是用的时间序列的方法。
1数据读取
import pandas as pd
import numpy as np
!pip install openpyxl
import matplotlib as plt
!pip install statsmodels
car_data = pd.read_excel('/work/中国汽车总体销量.xlsx')
car_data.head()
2销量数据和销售时间
import matplotlib.pyplot as plt
x = car_data['时间']
y = car_data['销量']
# 设置matplotlib正常显示中文和负号
# plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
# plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号 #有中文出现的情况,需要u'内容'
plt.figure(figsize=(15,5))
plt.plot(x, y) # 使用x和y画折线图
plt.title(' Car sales ') # 设置图的标题
plt.xlabel('time') # 设置x轴的标签
plt.ylabel('sales') # 设置y轴的标签
plt.grid(color = 'r', linestyle = '--', linewidth = 0.5)
plt.show() # 显示图形
结果显示:
中国汽车销量数据折线图分析:整体来看从2008年到2023年每年的汽车销售数据量均在逐步增加,在2020年汽车销量直线式下跌,很大概率原因是因为新冠导致的。具体看到趋势图里面有周期因素在。
3周期因素
from statsmodels.tsa.seasonal import seasonal_decompose
# 转换日期列为日期格式
car_data['时间'] = pd.to_datetime(car_data['时间'])
# 设置日期列为索引
car_data.set_index('时间', inplace=True)
# 进行季节性因素分析
result = seasonal_decompose(car_data['销量'], model='multiplicative')
# 绘制结果
result.plot()
plt.show()
可以看到有季节因素在里面,很规律的,下面我们建模就会考虑季节因素的。
4 建模
from statsmodels.tsa.arima.model import ARIMA
# 使用ARIMA模型进行建模
model = ARIMA(car_data['销量'], order=(2,3,4)) # 自回归阶数 差分 移动平均
model_fit = model.fit()
# 打印模型摘要
print(model_fit.summary())
建模结果显示:
最大似然为:-2791,这个指标我们只看绝对值,绝对值越小越好;第二个指标是:AIC:赤池信息量,这个也是越小越好;我们再看P值均通过检验,故模型效果很好。
5 预测
### 预测3期数据
forecast = model_fit.forecast(steps=3)
print(forecast)
本数据是截止到2023-09的数据,预测3期,下面是预测结果:
2023-10 662922.322777
2023-11 672882.596872
2023-12 630179.046963