文字轉成 DataFrame
你已經把這些以計數為基礎的特徵生成為陣列,接下來需要重新整理格式,好與其餘資料集結合。作法是把陣列轉成 pandas 的 DataFrame,並以你先前取得的特徵名稱作為欄名,然後再與原始 DataFrame 串接。
你在上一個練習擬合好的 numpy 陣列(cv_array)與向量化器(cv)已經在你的工作環境中。
本練習屬於課程
Feature Engineering for Machine Learning in Python
練習說明
- 建立一個 DataFrame
cv_df,其中數值來自cv_array,欄名使用特徵名稱。 - 為了方便識別,替欄名加上前綴
Counts_。 - 將這個 DataFrame(
cv_df)以欄方向與原始 DataFrame(speech_df)串接。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create a DataFrame with these features
cv_df = pd.DataFrame(____,
columns=____).____('Counts_')
# Add the new columns to the original DataFrame
speech_df_new = ____([speech_df, cv_df], axis=1, sort=False)
print(speech_df_new.head())