Python数据分析:股票数据分析案例
步骤:
准备数据可视化数据、审查数据处理数据根据ACF、PACF定阶拟合ARIMA模型预测import pandas as pdimport pandas_datareaderimport datetimeimport matplotlib.pylab as pltfrom matplotlib.pylab import stylefrom statsmodels.tsa.arima_model import ARIMAfrom statsmodels.graphics.tsaplots import plot_acf, plot_pacfstyle.use('ggplot')# 设置图片显示的主题样式# 解决matplotlib显示中文问题plt.rcParams['font.sans-serif'] = ['SimHei'] # 指定默认字体plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题def run_main():"""主函数"""# 1. 准备数据# 指定股票分析开始日期start_date = datetime.datetime(, 1, 1)# 指定股票分析截止日期end_date = datetime.datetime(, 4, 1)# 股票代码stock_code = '600519.SS' # 沪市贵州茅台stock_df = pandas_datareader.data.DataReader(stock_code, 'yahoo', start_date, end_date)# 预览数据print(stock_df.head())# 2. 可视化数据plt.plot(stock_df['Close'])plt.title('股票每日收盘价')plt.show()# 按周重采样stock_s = stock_df['Close'].resample('W-MON').mean()stock_train = stock_s['':'']plt.plot(stock_train)plt.title('股票周收盘价均值')plt.show()# 分析 ACFacf = plot_acf(stock_train, lags=20)plt.title("股票指数的 ACF")acf.show()# 分析 PACFpacf = plot_pacf(stock_train, lags=20)plt.title("股票指数的 PACF")pacf.show()# 3. 处理数据,平稳化数据# 这里只是简单第做了一节差分,还有其他平稳化时间序列的方法stock_diff = stock_train.diff()diff = stock_diff.dropna()print(diff.head())print(diff.dtypes)plt.figure()plt.plot(diff)plt.title('一阶差分')plt.show()acf_diff = plot_acf(diff, lags=20)plt.title("一阶差分的 ACF")acf_diff.show()pacf_diff = plot_pacf(diff, lags=20)plt.title("一阶差分的 PACF")pacf_diff.show()# 4. 根据ACF和PACF定阶并建立模型model = ARIMA(stock_train, order=(1, 1, 1), freq='W-MON')# 拟合模型arima_result = model.fit()print(arima_result.summary())# 5. 预测pred_vals = arima_result.predict(start=str('-01'),end=str('-03'),dynamic=False, typ='levels')print(pred_vals)# 6. 可视化预测结果stock_forcast = pd.concat([stock_s, pred_vals], axis=1, keys=['original', 'predicted'])plt.figure()plt.plot(stock_forcast)plt.title('真实值vs预测值')plt.savefig('./stock_pred.png', format='png')plt.show()if __name__ == '__main__':run_main()
运行: