值错误:无法处理未知和二进制的混合



我最近使用scikit-learn进行情感分析,所以在我训练了我的标记数据之后,然后试图在未标记的数据集上测试它们,这个错误出现了'ValueError:不能处理连续多输出和二进制的混合'

我认为我做错的是我给了(y_pred)错误的假设。

错误来自以下内容:accuracy = classifier.score(test_matrix,ALL_test)

但是当我将ALL_test更改为ALL_train(训练和标记的数据)时,它带来了0.971251409245的准确性;这是绝对错误的

我该怎么办?

# -*- coding:utf-8 -*-
import sklearn.cross_validation
import sklearn.feature_extraction.text
import sklearn.metrics
import sklearn.naive_bayes
from sklearn import svm
import numpy as np
import pandas as pd
from sklearn.metrics import accuracy_score, precision_score, recall_score

name = ['Tweet','Label']
name2 =['Tweet','Label']
data_train = pd.read_table('unstemmedtrain.csv',sep = ';',names = name)
data_test = pd.read_table('unstemmedtest.csv',names=name2)
train_data =pd.DataFrame(data_test,columns=name2)
test_data=pd.DataFrame(data_train,columns=name)
vectorizer =  sklearn.feature_extraction.text.CountVectorizer()
train_matrix = vectorizer.fit_transform(train_data['Tweet'])
test_matrix = vectorizer.transform(test_data['Tweet'])
#print train_matrix
positive_train = (train_data['Label']=='positive')
negative_train= (train_data['Label']=='negative')
neutral_train=(train_data['Label']=='neutral')
#print negative_cases_train
ALL_train = positive_train +negative_train +neutral_train
#print positive_cases_train
ALL_test = (test_data['Tweet'])
positive_test =(test_data['Label']=='positive')
negative_test =(test_data['Label']=='negative')
neutral_test = (test_data['Label']=='neutral')
ALL_Test = positive_test + negative_test + neutral_test
#print positive_cases_test

classifier=sklearn.naive_bayes.MultinomialNB()
classifier2 = classifier.fit(train_matrix,ALL_train)
p_sentiment = classifier.predict(test_matrix)
p_prob = classifier.predict_proba(test_matrix)
#print predicted_prob
accuracy = classifier.score(test_matrix,ALL_test)
print accuracy

我看到了一些问题。

  1. 你是在试图预测哪条推文是积极的,哪条是消极的,哪条是中立的,还是你在试图预测一条推文是积极的/消极的/中立的?你做的是后者。假设train_data['Label'] = ['positive', 'positive', 'negative', 'neutral']。所以你的代码是:

    positive_train = (train_data['Label']=='positive') # = [True, True, False, False]
    negative_train= (train_data['Label']=='negative') # = [False, False, True, False]
    neutral_train=(train_data['Label']=='neutral') # = [False, False, False, True]
    ALL_train = positive_train +negative_train +neutral_train # = [True, True, True, True]
    
  2. 你给分数函数ALL_test = (test_data['Tweet']),这是文本,而不是ALL_Test = positive_test + negative_test + neutral_test,这是你真正的y。这就是例外的来源。我不知道你为什么需要All_test,但如果你需要,请将其命名为不同的名称-这会使你感到困惑。

必须将All_train传递给classifier.score

:

accuracy = classifier.score(test_matrix,ALL_train)
print accuracy

如果您想为测试数据评估您的模型,那么Recall,precision,f1 score和auc_score可能会有所帮助

相关内容

  • 没有找到相关文章

最新更新