gpt4 book ai didi

python-3.x - 如何在文本分类的拆分数据测试和训练中修复 'typeerror: only integer scalar arrays can be converted to a scalar index'

转载 作者:行者123 更新时间:2023-12-03 22:23:25 29 4
gpt4 key购买 nike

我想创建一个程序来使用 SVM 对文本数据进行分类。但在此之前,我必须使用 StratifiedKFold() 将数据拆分为训练数据和测试数据。

但它以这个错误结束:

'Traceback (most recent call last):
File "C:\Users\Administrator\PycharmProjects\untitled1\main.py", line 115, in <module>
y_train, y_test = labels[train_index], labels[test_index]
TypeError: only integer scalar arrays can be converted to a scalar index'

如何解决此代码中的这个错误?

这是在python 3.7上运行的代码

labels = []
label_np = np.array(labels)

with open(path, encoding='utf-8') as in_file:
data = csv.reader(in_file)
for line in data:
label_ = np.append(label_np, line)

model = SVC(kernel='linear')
total_svm = []
total_mat_svm = np.zeros((2,2))

kf = StratifiedKFold(n_splits=3)
kf.get_n_splits(result_preprocess, label_)

for train_index, test_index in kf.split(result_preprocess, label_):
# print('Train : ', test_index, 'Test : ', test_index)
x_train, x_test = result_preprocess[train_index], result_preprocess[test_index]
y_train, y_test = label_[train_index], label_[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)
hasil_svm = model.predict(x_test)

total_mat_svm = total_mat_svm + confusion_matrix(y_test, hasil_svm)
total_svm = total_mat_svm + sum(y_test==hasil_svm)

print(total_mat_svm)

我希望结果是分类的分类性能和混淆矩阵。

最佳答案

请看这个答案:numpy array TypeError: only integer scalar arrays can be converted to a scalar index

我怀疑不仅是result_preprocesslabels也是你数据管道中的一个列表。在这种情况下,解决方案只是将 labels 转换为 NumPy 数组,然后再运行您的代码片段:

import numpy as np
labels = np.array(labels)

关于python-3.x - 如何在文本分类的拆分数据测试和训练中修复 'typeerror: only integer scalar arrays can be converted to a scalar index',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55884002/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com