我正在运行多标签分类的[code] 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)
答案 0 :(得分:3)
您忘记将数据集拆分为训练集和测试集。
导入库
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)