【问题标题】:How to predict sentiment of unseen text?如何预测看不见的文本的情绪?
【发布时间】:2021-04-08 00:15:56
【问题描述】:

使用 scikit learn,我已经训练了我的模型,但不知道如何使用该模型来预测新的文本段落。我看过大量的教程,但没有一个超出培训和测试的范围。下面是我使用的代码

data_source_url = "/path/to/file.csv"
airline_tweets = pd.read_csv(data_source_url)

features = airline_tweets.iloc[:, 10].values
labels = airline_tweets.iloc[:, 1].values

processed_features = []

    # I do some text processing here and then append the text to processed_features

        
vectorizer = CountVectorizer(analyzer = 'word', lowercase = False)
features = vectorizer.fit_transform(processed_features)
features_nd = features.toarray() # for easy usage

X_train, X_test, y_train, y_test  = train_test_split(features_nd, labels, train_size=0.80, random_state=1234)

log_model = LogisticRegression()
log_model = log_model.fit(X=X_train, y=y_train)
    
predictions = log_model.predict(X_test)
    

【问题讨论】:

  • text_classifier 是从哪里来的?你是说log_model.predict(X_test)
  • 是的,你是对的。我的意思是 log_model.predict(X_test)

标签: python scikit-learn sentiment-analysis


【解决方案1】:

基本上,您只需按照相同的步骤来转换您的新数据集。然后,使用您经过训练的模型进行预测。它看起来像这样:

new_dataset = ...    # read your new dataset
new_processed_features = []
    # do the same text processing here

# Use the same vectorizer to transform your new dataset        
new_features = vectorizer.transform(new_processed_features)
new_features_nd = new_features.toarray() # for easy usage

# Use your trained model to predict new dataset
new_predictions = log_model.predict(new_features_nd)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 2022-10-15
    • 1970-01-01
    • 2014-12-09
    相关资源
    最近更新 更多