群組資料的特徵工程
現在你要在前一個練習的基礎上,再加入一個特徵:每台來源電腦所使用的唯一通訊協定數量。請注意,對群組後的資料,常能用這種方式來建構特徵:以所有類別型欄位的唯一元素數量,以及所有數值型欄位的平均值作為起點。和之前一樣,已預先載入 flows,並提供 cross_val_score() 用來衡量準確率、AdaBoostClassifier()、以及作為 pd 的 pandas 和作為 np 的 numpy。
本練習屬於課程
在 Python 設計機器學習工作流程
練習說明
- 在提供的群組迭代器上套用
lambda函式,計算每台來源電腦所使用的唯一通訊協定數量。你可以用set()將protocol欄位縮減為唯一值的集合。 - 透過提供索引並將欄位命名為
protocol,把結果轉成形狀正確的資料框。 - 將新的資料框與現有的
X串接。 - 使用
cross_val_score()評估AdaBoostClassifier()在此新資料集上的準確率。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create a feature counting unique protocols per source
protocols = flows.groupby('source_computer').apply(
lambda df: ____)
# Convert this feature into a dataframe, naming the column
protocols_DF = pd.DataFrame(
protocols, index=____, columns=____)
# Now concatenate this feature with the previous dataset, X
X_more = pd.concat([X, ____], axis=____)
# Refit the classifier and report its accuracy
print(____(____(
AdaBoostClassifier(), ____, y)))