【问题标题】:Using a trained sentiment analysis model, TF-IDF and logistic regression使用经过训练的情感分析模型、TF-IDF 和逻辑回归
【发布时间】:2020-10-27 05:02:25
【问题描述】:

我正在对 Twitter 数据集进行情绪分析项目。我使用 TF-IDF 特征提取和逻辑回归模型进行分类。到目前为止,我已经使用以下方法训练了模型:

def get_tfidf_features(train_fit, ngrams=(1,1)):
    vector = TfidfVectorizer(ngrams, sublinear_tf=True)
    vector.fit(train_fit)
    return vector

X = tf_vector.transform(df['text'])

y = df['sentiment']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.01, random_state = 42)

LR_model = LogisticRegression(solver='lbfgs')
LR_model.fit(X_train, y_train)
y_predict_lr = LR_model.predict(X_test)

这个逻辑回归模型是在大约 150 万条推文的数据集上训练的。我有一组大约 18,000 条推文,我想使用这个模型来预测这个新数据集中推文的情绪得分。我不知道如何将这个训练有素的模型实际应用于新数据。这个新数据框 df_chi 的头部如下所示:

具有形状 (18393, 7)。我想采用我已经拥有的训练模型,将其应用于文本列,并创建一个新的sentiment 列,其中包含df_chi 数据框中的这些预测分数。 (注意:图片没有显示干净的文本,但我会这样做。)

我是一名机器学习新手,我从未采用过训练好的模型并将其应用于新数据。我的困惑始于使用 TF-IDF 从df_chi 文本中提取特征。我试图这样做(完全猜测):

tf_vector = get_tfidf_features(df_chi['text'])
X = tf_vector.transform(df_chi['text'])
df_chi['sentiment'] = LR_model.predict(X)

给出以下 ValueError:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-188-0cf1a4f34c8b> in <module>
      1 tf_vector = get_tfidf_features(df_chi['text'])
      2 X = tf_vector.transform(df_chi['text'])
----> 3 df_chi['sentiment'] = LR_model.predict(X)

~/opt/anaconda3/lib/python3.7/site-packages/sklearn/linear_model/_base.py in predict(self, X)
    291             Predicted class label per sample.
    292         """
--> 293         scores = self.decision_function(X)
    294         if len(scores.shape) == 1:
    295             indices = (scores > 0).astype(np.int)

~/opt/anaconda3/lib/python3.7/site-packages/sklearn/linear_model/_base.py in decision_function(self, X)
    271         if X.shape[1] != n_features:
    272             raise ValueError("X has %d features per sample; expecting %d"
--> 273                              % (X.shape[1], n_features))
    274 
    275         scores = safe_sparse_dot(X, self.coef_.T,

ValueError: X has 22806 features per sample; expecting 265054

很确定我将训练模型应用于新数据的整个方法是不正确的。这样做的正确方法是什么?

【问题讨论】:

    标签: python machine-learning scikit-learn sentiment-analysis tf-idf


    【解决方案1】:

    与此面条栏,并提出以下解决方案:

    tfidf = TfidfVectorizer()
    X_chi = tfidf.fit_transform(df_chi['text'])
    
    X1 = pd.DataFrame.sparse.from_spmatrix(X)
    X_chi1 = pd.DataFrame.sparse.from_spmatrix(X_chi)
    
    not_existing_cols = [c for c in X1.columns.tolist() if c not in X_chi1]
    X_chi1 = X_chi1.reindex(X_chi1.columns.tolist() + not_existing_cols, axis=1)
    #X_chi.fillna(0, inplace=True)
    X_chi1 = X_chi1[X1.columns.tolist()]
    a = LR_model.predict(X_chi1)
    df_chi['sentiment'] = a
    

    解决方案灵感来自Logistic regression: X has 667 features per sample; expecting 74869

    但是看起来有点笨拙。如果它有效,我猜。虽然我怀疑可能是一个更好的方法来做这件事,否?

    【讨论】:

      猜你喜欢
      • 2018-05-01
      • 2020-10-26
      • 2019-07-11
      • 2021-05-18
      • 1970-01-01
      • 2020-09-22
      • 1970-01-01
      • 2016-02-01
      • 1970-01-01
      相关资源
      最近更新 更多