开始使用免费开始使用

用 EDA 探索数据

首先,我们来探索数据。开始任何机器学习(ML)项目之前,都需要先做探索性数据分析(EDA),以便熟悉数据。这通常包括:

  • 原始数据图
  • 直方图
  • 以及其他分析……

我通常从原始数据图和直方图开始。这样可以帮助我们理解数据的分布。如果接近正态分布,就可以使用参数统计等方法。

已经为您加载了两只股票的数据到 pandas DataFrame:lng_dfspy_df(对应 LNG 与 SPY)。请使用 .head() 查看它们。我们将使用收盘价,后续还会使用成交量,作为机器学习算法的输入。

注意:每次需要绘制新图时,请调用 plt.clf(),或者使用 f = plt.figure()

本练习是课程的一部分

Python 金融机器学习

查看课程

练习说明

  • 打印两个 DataFrame(lng_dfspy_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()
编辑并运行代码