用 EDA 探索資料
先來探索資料吧。每當我們開始一個機器學習(ML)專案時,都需要先做探索式資料分析(EDA),以熟悉資料內容。這包含以下項目:
- 原始資料走勢圖
- 直方圖
- 以及其他…
我通常會從原始資料走勢圖和直方圖開始。這能幫助我們了解資料的分佈情況。若是常態分佈,我們就能使用母體參數統計等方法。
已經為你載入了兩檔股票到 pandas 的 DataFrame:lng_df 與 spy_df(LNG 與 SPY)。用 .head() 看一下它們。我們會使用收盤價,之後也會把成交量納入,作為 ML 演算法的輸入。
注意:每次要產生新圖時,我們會呼叫 plt.clf(),或是使用 f = plt.figure()。
本練習屬於課程
Python 金融 Machine Learning
練習說明
- 印出兩個 DataFrame(
lng_df與spy_df)的前 5 列並檢視其內容。 - 使用 pandas 繪製
'SPY'與'LNG'的原始時間序列資料,以調整後收盤價('Adj_Close')作圖——在.plot()設定legend=True。 - 使用
plt.show()顯示原始時間序列圖(matplotlib.pyplot已以plt匯入)。 - 使用 pandas 與 matplotlib 為 SPY 與 LNG 製作調整後收盤價 1 天百分比變動(使用
.pct_change())的直方圖。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
print(lng_df.head()) # examine the DataFrames
print(____) # examine the SPY DataFrame
# Plot the Adj_Close columns for SPY and LNG
spy_df[____].plot(label='SPY', legend=True)
lng_df[____].plot(label=____, ____, secondary_y=True)
____ # show the plot
plt.clf() # clear the plot space
# Histogram of the daily price change percent of Adj_Close for LNG
lng_df['Adj_Close'].____.plot.hist(bins=50)
plt.xlabel('adjusted close 1-day percent change')
plt.show()