我试图使用决策树和for循环来实现装袋和投票。我使用了sklearn的resample函数。然而,我得到了Number of labels=97 does not match number of samples=77
的错误,我大致能理解为什么会这样,但不确定如何修复它。
数据集中有150个样本。标签也有150个,所以150 * 0.35 = 97,并且97 * 0.8 = 77。X是长度为150的特征矩阵,y是长度为150的标签向量。
以下是我的代码
from sklearn.model_selection import train_test_splitfrom sklearn.metrics import accuracy_scorefrom sklearn.utils import resampleX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.35, random_state=3)predictions = []for i in range(1,20): bootstrap_size = int(0.8*len(X_train)) bag = resample(X_train, n_samples = bootstrap_size , random_state=i , replace = True) Base_DecisionTree = DecisionTreeClassifier(random_state=3) Base_DecisionTree.fit(bag, y_train) y_predict = Base_DecisionTree.predict(X_test) accuracy = accuracy_score(y_test, y_predict) predictions.append(accuracy)
回答:
你应该同时对标签进行重采样,并在fit()
中使用它。
x_bag, y_bag = resample(X_train, y_train, n_samples = bootstrap_size , random_state=i , replace = True) tree = DecisionTreeClassifier(random_state=3)tree.fit(x_bag, y_bag)