開始使用免費開始

理解變數之間的差異

現在你要用長條圖繪出每個變數的平均值與標準差,並加以分析。這是前一步的補充,你將以視覺化方式探索各變數的量級與變異的差異。

pandas 已以 pd 載入,matplotlib.pyplotplt 載入。wholesale 資料集已載入為 pandas DataFrame,而 wholesale 各欄的平均值與標準差分別載入為名為 averagesstd_devspandas Series。請在主控台先行探索它們。

本練習屬於課程

Python 的行銷機器學習

檢視課程

練習說明

  • 建立一個包含 wholesale 欄名的清單,及另一個從 0 到 wholesale 欄數的遞增數值清單。
  • 使用灰色繪製 averages、使用橘色繪製 std_devs,並將 x 軸位移 0.2。
  • x_ix 設為刻度、x_names 設為標籤,並將標籤旋轉 90 度。
  • 加入圖例並顯示圖表。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Create column names list and same length integer list
x_names = wholesale.___
x_ix = np.arange(wholesale.shape[1])

# Plot the averages data in gray and standard deviations in orange 
plt.bar(x=x_ix-___, height=averages, color='grey', label='Average', width=0.4)
plt.bar(x=x_ix+___, height=std_devs, color='orange', label='Standard Deviation', width=0.4)

# Add x-axis labels and rotate
plt.xticks(ticks=___, labels=x_names, rotation=90)

# Add the legend and display the chart
plt.legend()
plt.___()
編輯並執行程式碼