pandas 0.23.4 :'pd.ewma'沒有這個模組,改用`Series.ewm` 或 降低版本到 pandas 0.21.0
阿新 • • 發佈:2018-12-20
問題:
在進行畫出指數平滑移動平均線,遇到如下問題:
# pd.ewma(com=None, span=one) # 指數平均線。com:資料;span:時間間隔
AttributeError: module 'pandas' has no attribute 'ewma'
解決辦法:
方法一:
換用下面的方法
# Series.ewm(com=None, span=None, halflife=None, alpha=None, min_periods=0, freq=None, adjust=True, ignore_na=False, axis=0) # com : float, optional # Specify decay in terms of center of mass, \(\alpha = 1 / (1 + com),\text{ for } com \geq 0\) # span : float, optional # Specify decay in terms of span, \(\alpha = 2 / (span + 1),\text{ for } span \geq 1\) # halflife : float, optional # Specify decay in terms of half-life, \(\alpha = 1 - exp(log(0.5) / halflife),\text{ for } halflife > 0\) # alpha : float, optional # Specify smoothing factor \(\alpha\) directly, \(0 < \alpha \leq 1\) # New in version 0.18.0. # min_periods : int, default 0 # Minimum number of observations in window required to have a value (otherwise result is NA). # freq : None or string alias / date offset object, default=None (DEPRECATED) # Frequency to conform to before computing statistic # adjust : boolean, default True # Divide by decaying adjustment factor in beginning periods to account for imbalance in relative weightings (viewing EWMA as a moving average) # ignore_na : boolean, default False # Ignore missing values when calculating weights; specify True to reproduce pre-0.15.0 behavior stock_day["close"].ewm(span=30).mean().plot()
方法二:
在pandas 0.23.4
版本中,已經不存在這種方法,回退到之前版本pandas 0.21.0
就一切完美
pip install pandas==0.21
例項:
# 簡單移動平均線(SMA),又稱“算數移動平均線”,是指特定期間的收盤價進行平均化 # 例:5日的均線 SMA=(C1+ C2 + C3 + C4 + C5) / 5 # Cn為資料中第n天的數 # 計算移動平均線,對每天的股票的收盤價進行計算 close指標 # pd.rolling_mean(data, window=5) # 這種方法已經淘汰了 data.rolling(window=n).mean().plot() # window=n n日的平均數
# 加權移動平均線(WMA):為了提高最近股票(收盤價)資料的影響,防止被平均 # 1) 末日加權移動平均線: MA(N) = (C1+ C2 + C3 + C4 + ... + Cn *2) / (n+1) # 2) 線性加權移動平均線(給的權重比例太大,導致最近的時間序列資料影響過大,一般不選擇): MA(N) = (C1+ C2 * 2 + C3 * 3 + C4 * 4 + ... + Cn * n) / (1 + 2 + ... + n) # 3) 指數平滑移動平均線(EWMA): # 提高最近的資料的比重,不存在給的過大; # 比重都是小數,所有天書的比重加起來等於1 y=[2 * x + (N - 1) * y' ]/ (N + 1) # x:當天的價格;N:第幾天;y':上一次的EWMA結果 # pd.ewma(com=None, span=one) # 指數平均線。com:資料;span:時間間隔
# 股票時間序列資料處理
stock_day = pd.read_csv("./data/stock_day/stock_day.csv")
stock_day.sort_index()
stock_day["index"] = [i for i in range(stock_day.shape[0])]
val = stock_day[["index", "open", "close", "high", "low"]].values
fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(20,8), dpi=80)
# K線圖
candlestick_ochl(axes, val, width=0.2, colorup="r", colordown="g")
# 計算簡單移動平均線,對每天的股票的收盤價進行計算 close指標
# pd.rolling_mean(stock_day["close"], window=5) # 這種方法在pandas 0.23.4 已經淘汰了
# stock_day["close"].rolling(window=5).mean().plot()
# stock_day["close"].rolling(window=10).mean().plot()
# stock_day["close"].rolling(window=30).mean().plot()
# stock_day["close"].rolling(window=60).mean().plot()
# stock_day["close"].rolling(window=120).mean().plot()
# 畫出指數平滑移動平均線
# 方法一:
stock_day["close"].ewm(span=10).mean().plot()
# 方法二:pandas 0.21.0及以下版本的使用方法
# pd.ewma(stock_day["close"], span=10).plot()
plt.show()
具體在pandas 0.23.4
版本中還在繼續查詢其方法!