【问题标题】:How to transform customize vectorizer for predicting classification?如何转换自定义矢量化器以预测分类?
【发布时间】:2021-10-06 06:23:42
【问题描述】:

据我谷歌搜索,我没有找到类似的问题,或者我用错误的关键字搜索它。

我想做一个特征提取的变体。

  1. 矢量化为普通的词袋
  2. 矢量化词袋,结合附加功能

所以对于第一种方法,我适合使用此代码转换数据集(这是我的函数的一部分。df 是数据框,vect 是 TFIDF/countvectorizer)

    self.X = self.vect.fit_transform(df.Tweet)
    self.X_columns=self.vect.get_feature_names()

所以在我建立分类模型后,我可以使用这段代码转换任何我想预测的文本。 (vect 是 TFIDF/countvectorizer,new_df 是数据帧,clf 是使用任何算法训练的构建分类器)

    text_features = vect.transform(new_df.Tweet)  
    predictions = clf.predict(text_features)

已经完成,并且可以正常工作。

所以对于第二种情况: 我用一些解决方法做了同样的事情。我在 stackoverflow 中查看了任何有用的代码,并使用此代码进行了操作。 (sp 是 scipy 库,df 是数据框)

    self.X = sp.sparse.hstack((vect.fit_transform(df.Tweet), df[['feature_1','feature_2','score','sentiment']].values), format='csr')
    self.X_columns=vect.get_feature_names() + df[['feature_1','feature_2','score','sentiment']].columns.tolist()

有效,附加功能已添加到 csr 矩阵中。

但问题是如何将 new_df 转换为矩阵? 我不知道从哪里开始尝试解决方案

【问题讨论】:

    标签: python pandas scikit-learn scipy classification


    【解决方案1】:

    我的猜测是

        # count/process each additional features ['feature_1','feature_2','score','sentiment']
        ...
        # then use similar method but using transform instead fit_transform
        text_features = sp.sparse.hstack((vect.transform(new_df.Tweet), new_df[['feature_1','feature_2','score','sentiment']].values), format='csr')
        predictions = clf.predict(text_features)
    

    如果答案正确,我会更新。如果您找到更好的方法/解决方案,请分享。

    【讨论】:

      猜你喜欢
      • 2020-08-19
      • 2021-08-15
      • 2011-05-13
      • 2017-06-21
      • 2021-02-03
      • 2020-07-06
      • 1970-01-01
      • 1970-01-01
      • 2021-02-08
      相关资源
      最近更新 更多