将文本转为 DataFrame
现在,您已经在数组中生成了基于计数的特征,需要重新整理其格式,以便与其余数据集合并。方法是将该数组转换为一个 pandas DataFrame,使用之前得到的特征名作为列名,然后与原始 DataFrame 进行拼接。
上一练习中拟合得到的 numpy 数组(cv_array)和向量化器(cv)已在您的工作区中提供。
本练习是课程的一部分
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())