Python简单指数平滑

时间:2018-02-04 02:46:38

标签: python csv forecasting exponential python-ggplot

我从www.nasdaq.com下载了TESLA股票;在我下载CSV文件后,我意识到我需要使用Microsoft Excel 2016转换CSV。我使用数据选项卡;并单击“文本到列”。标题现在清晰,它们是:日期,关闭,音量,开放,高,低。请在此处查看csv文件。林克: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程序应该改进什么?任何帮助都会很棒。提前谢谢。

2 个答案:

答案 0 :(得分:4)

统计模型ExponentialSmoothing怎么样?

statsmodels软件包具有许多用于python中的时间序列分析的工具。

from statsmodels.tsa.api import ExponentialSmoothing

另外,请在本文中了解有关python中的时间序列分析的信息:

https://www.analyticsvidhya.com/blog/2018/02/time-series-forecasting-methods/

答案 1 :(得分:0)

在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)
相关问题