【问题标题】:How remove few columns from countvectorized sparse dataframe in pandas如何从熊猫中的countvectorizer稀疏数据框中删除几列
【发布时间】:2018-05-25 04:30:09
【问题描述】:

我在 countvectorized 数据框中有大约 2000 个文本特征。我有 800 个文本特征列的列表,它们对预测模型具有实际的特征重要性贡献。我只想保留这 800 列并删除其余 1200 列,因为它们对我的预测贡献不大。

我该怎么做。我有要在文本文件中维护的列列表。

cv = CountVectorizer( max_features = 2000,analyzer='word') 
    cv_text = cv.fit_transform(data.pop('text'))
    for i, col in enumerate(cv.get_feature_names()):
        data[col] = pd.SparseSeries(cv_text[:, i].toarray().ravel(), fill_value=0)

【问题讨论】:

    标签: python pandas scikit-learn nlp


    【解决方案1】:

    应该很简单:

    data = data.drop(list_of_cols_to_drop, axis=1)
    

    data = data.drop(data.columns.difference(list_of_needed_cols), axis=1)
    

    SparseDataFrame 对象有一个 drop 方法。

    来自文档字符串:

    In [139]: pd.SparseDataFrame.drop?
    Signature: pd.SparseDataFrame.drop(self, labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='rai
    se')
    Docstring:
    Return new object with labels in requested axis removed.
    

    【讨论】:

    • 我只有要维护的列的列表,而不是要删除的列的列表。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 2015-03-18
    • 2021-09-10
    • 2014-05-06
    • 1970-01-01
    • 2020-05-28
    • 2018-03-15
    相关资源
    最近更新 更多