gpt4 book ai didi

python - 如何修复名称错误: name 'X_train' is not defined?

转载 作者:行者123 更新时间:2023-11-30 09:25:56 26 4
gpt4 key购买 nike

我正在运行多标签分类的[代码] 1 .如何修复“X_train”未定义的NameError。Python代码如下。

import scipy
from scipy.io import arff
data, meta = scipy.io.arff.loadarff('./yeast/yeast-train.arff')
from sklearn.datasets import make_multilabel_classification

# this will generate a random multi-label dataset
X, y = make_multilabel_classification(sparse = True, n_labels = 20,
return_indicator = 'sparse', allow_unlabeled = False)

# using binary relevance
from skmultilearn.problem_transform import BinaryRelevance
from sklearn.naive_bayes import GaussianNB

# initialize binary relevance multi-label classifier
# with a gaussian naive bayes base classifier
classifier = BinaryRelevance(GaussianNB())

# train
classifier.fit(X_train, y_train)

# predict
predictions = classifier.predict(X_test)

from sklearn.metrics import accuracy_score
accuracy_score(y_test,predictions)

最佳答案

您忘记将数据集拆分为训练集和测试集。

导入库

from sklearn.model_selection import train_test_split

在 classifier.fit() 之前添加此行

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

关于python - 如何修复名称错误: name 'X_train' is not defined?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51258645/

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