我正在尝试建立一个回归模型,以便根据出现的单词来预测收视率(1-5)(回归本身并不一定表现良好,更多的是关于所采用的方法).
我使用以下代码创建了一个词频矩阵:
bow = df.Review2.str.split().apply(pd.Series.value_counts)
看起来像这样:
我现在有兴趣删除在整个评论中很少出现的列(单词).此外,我只想迭代不具有NaN的Rating值的评论(行).
这是我的尝试:
# Delete row if Rating less than 1
for index, row in df.iterrows():
if (df.Rating[index] < 1):
bow.drop(bow.index[index], axis=0, inplace = True)
# Delete column if word occurs less than 50 times
sum1 = bow.sum(axis=0)
cntr = 0
for i in sum1:
if (i < 50):
bow.drop(bow.index[cntr], axis=1, inplace = True)
cntr += 1
这似乎没有用,因为它使单词只出现一次.
编辑:
这是我的稀疏数据框,包含单词的出现.
Col->话;
行->句子(项目的评论)(我有1.5k项,因此有1.5k行)
hi this are just some random words I don t ... zing zingy zingzang
0 1.0 NaN 1.0 1.0 1.0 NaN NaN NaN NaN NaN ... NaN NaN NaN
1 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN
2 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN
3 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN
4 NaN NaN NaN NaN NaN NaN NaN NaN NaN 1.0 ... NaN NaN NaN
评分是我原始数据帧中的一列,其中包含[1,5]范围内的整数或NaN