我从www.nasdaq.com下载了一个特斯拉股票;在我下载CSV文件后,我意识到我需要使用Microsoft Excel2016转换CSV。我使用Data Tab;并单击Text to Columns。标题现在很清楚,它们是: date,close,volume,open,high,low。请在此处查看csv文件。LinK:https://drive.google.com/open?id=1cirQi47U4uumvA14g6vOmgsXbV-YvS4l
Preview (The CSV data is from 02/02/2017 until 02/02/2018):
1. date | close | volume | open | high | low |
2. 02/02/2018 | 343.75 | 3696157 | 348.44 | 351.95 | 340.51|
3. 01/02/2018 | 349.25 | 4187440 | 351.00 | 359.66 | 348.63|对我来说,挑战是创建一个尽可能接近每月第一个月的数据点。我过滤了excel文件,这是我得到的数据。
- date | close
- 01/02/2018 | 349.25
- 02/01/2018 | 320.53
- 01/12/2017 | 306.53
- 01/11/2017 | 321.08
- 02/10/2017 | 341.53
- 01/09/2017 | 355.40
- 01/08/2017 | 319.57
- 03/07/2017 | 352.62
- 01/06/2017 | 340.37
- 01/05/2017 | 322.83
- 03/04/2017 | 298.52
- 01/03/2017 | 250.02
- 02/02/2017 | 251.55如果我创建一个数据点,它会变成这样,需要创建一个图形。为了显示原始数据和“平滑数据”的图形,使用简单的指数平滑,或者有时称为单指数平滑。这更多地是关于使用python-ggplot的时间序列预测。
- x | y
- 01/02/2018 | 349.25
- 02/01/2018 | 320.53
- 01/12/2017 | 306.53
- 01/11/2017 | 321.08
- 02/10/2017 | 341.53
- 01/09/2017 | 355.40
- 01/08/2017 | 319.57
- 03/07/2017 | 352.62
- 01/06/2017 | 340.37
- 01/05/2017 | 322.83
- 03/04/2017 | 298.52
- 01/03/2017 | 250.02
- 02/02/2017 | 251.55我写的python程序是:
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 3 13:20:28 2018
@author: johannesbambang
"""
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
my_data = pd.read_csv('C:\TESLA Exponential Smoothing\TSLA.csv',dayfirst=True,index_col=0)
my_data.plot()
plt.show()我的问题是,我应该在我的python程序中改进什么?任何帮助都是最好的。提前谢谢你。
发布于 2018-02-08 19:08:48
在Python中使用简单的指数平滑。
预测是使用加权平均值计算的,其中权重随着观测值的增加而指数下降。在过去,最小的权重与最旧的观测值相关联:
'''simple exponential smoothing go back to last N values
y_t = a * y_t + a * (1-a)^1 * y_t-1 + a * (1-a)^2 * y_t-2 + ... + a*(1-a)^n *
y_t-n'''
def exponential_smoothing(panda_series, alpha_value):
ouput=sum([alpha_value * (1 - alpha_value) ** i * x for i, x in
enumerate(reversed(panda_series))])
return ouput
panda_series=mydata.y
smoothing_number=exponential_smoothing(panda_series,0.6) # use a=0.6 or 0.5 your choice, which gives less rms error
estimated_values=testdata.copy() # replace testdata with your test dataset
estimated_values['SES'] = smoothing_number
error=sqrt(mean_squared_error(testdata.y, estimated_values.SES))
print(error)发布于 2018-07-31 00:18:58
那么statsmodels ExponentialSmoothing呢?
statsmodels包在python中有很多用于时间序列分析的工具。
from statsmodels.tsa.api import ExponentialSmoothing另外,在这篇关于python中的时间序列分析的文章中也可以看到:
https://www.analyticsvidhya.com/blog/2018/02/time-series-forecasting-methods/
https://stackoverflow.com/questions/48604184
复制相似问题