第一主成分
資料的第一主成分是資料變異最大的一個方向。在這個練習中,你的工作是使用 PCA 找出穀粒樣本長度與寬度量測的第一主成分,並把它以箭頭的方式標示在散佈圖上。
陣列 grains 提供了穀粒樣本的長度與寬度。PyPlot(plt)與 PCA 已為你匯入。
本練習屬於課程
Unsupervised Learning in Python
練習說明
- 繪製穀粒量測的散佈圖。此步驟已為你完成。
- 建立名為
model的PCA實例。 - 將模型擬合到
grains資料。 - 使用
model的.mean_屬性,取出資料平均值的座標。 - 使用
.components_[0,:]取得model的第一主成分。 - 使用
plt.arrow()在散佈圖上畫出第一主成分的箭頭。你必須指定前兩個引數──mean[0]與mean[1]。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Make a scatter plot of the untransformed points
plt.scatter(grains[:,0], grains[:,1])
# Create a PCA instance: model
model = ____
# Fit model to points
____
# Get the mean of the grain samples: mean
mean = ____
# Get the first principal component: first_pc
first_pc = ____
# Plot first_pc as an arrow, starting at mean
plt.arrow(____, ____, first_pc[0], first_pc[1], color='red', width=0.01)
# Keep axes on same scale
plt.axis('equal')
plt.show()