【问题标题】:Python Simple Exponential SmoothingPython 简单指数平滑
【发布时间】:2018-02-04 02:46:37
【问题描述】:

我从 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 程序中改进什么?任何帮助都会很棒。提前谢谢你。

【问题讨论】:

  • 你在问如何在你的情节中应用平滑?
  • 我正试图弄清楚如何在 Python 中进行简单的指数平滑。

标签: python csv forecasting exponential python-ggplot


【解决方案1】:

statsmodels ExponentialSmoothing 怎么样?

statsmodels 包有很多用于在 python 中进行时间序列分析的工具。

from statsmodels.tsa.api import ExponentialSmoothing

另外,看看这篇关于python时间序列分析的文章:

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

【讨论】:

  • 作为补充:exp = ExponentialSmoothing(data)exp_model = exp.fit(smoothing_level=0.1)result = exp_model.fittedvalues
【解决方案2】:

在 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)

【讨论】:

  • 嗨,Yogesh,感谢您的帮助。我接受了您的建议来更改数据集。似乎上传到谷歌驱动器的数据集不正确,因为我从 csv 转换为 excel;然后将其保存到csv。我学到了宝贵的教训,而不是将来。所以,我决定再次从纳斯达克下载特斯拉历史行情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-12
  • 2015-05-05
  • 2019-02-01
  • 2014-09-26
  • 2017-04-29
  • 1970-01-01
相关资源
最近更新 更多