我想创建一个使用TF-IDF和SVM进行情感分类的程序。在对数据分类之前,我必须使用分层的KFold将数据集分为数据训练和测试。我使用了numpy数组来存储文本(X)和标签(Y)
但最终出现此错误:
'ValueError:支持的目标类型为:('binary','multiclass')。取而代之的是“ multiclass-multioutput”。
此代码在python 3.7上运行
这是我的代码:
labels = []
with open(path, encoding='utf-8') as in_file:
data = csv.reader(in_file)
for line in data:
labels.append(line[1])
label_np = np.array(labels)
lp = label_np.reshape(20,20)
# lp = label_np.transpose(0)
# print(lp)
result_preprocess_np = np.array(result_preprocess)
hp = result_preprocess_np.reshape(20,20)
model = LinearSVC(multi_class='crammer_singer')
total_svm = []
total_mat_svm = np.zeros((20,20))
kf = StratifiedKFold(n_splits=3)
kf.get_n_splits(hp, lp)
for train_index, test_index in kf.split(hp, lp):
# print('Train : ', test_index, 'Test : ', test_index)
x_train, x_test = hp[train_index], hp[test_index]
y_train, y_test = lp[train_index], lp[test_index]
vectorizer = TfidfVectorizer(min_df=5,
max_df=0.8,
sublinear_tf=True,
use_idf=True)
train_vector = vectorizer.fit_transform(x_train)
test_vector = vectorizer.transform(x_test)
model.fit(x_train, y_train)
result_svm = model.score(x_test, y_test)
print(result_svm)
我希望结果是分类的准确性。