ValueError:X和Y矩阵的维不兼容:X.shape [1] == 1而Y.shape [1] == 7054

时间:2019-05-24 07:10:30

标签: python-3.x scikit-learn knn

我收到值错误。解决它需要什么。该代码用于交叉验证,以使用scikit learn找到最佳的KNN。

import matplotlib.pyplot as plt
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import roc_auc_score


train_auc = []
cv_auc = []
K = [1, 5, 11, 13,  15, 21, 31, 41, 51, 101]
for i in K:
    neigh = KNeighborsClassifier(n_neighbors=i , algorithm= "brute")
    neigh.fit(X_train, y_train)


    y_train_pred = []
    for i in range(0, X_train.shape[0], 1000):
        y_train_pred.extend(neigh.predict_proba(X_train[i,i+1000])[:,1]) 

    y_cv_pred = []
    for i in range(0, X_cv.shape[0], 1000):
        y_cv_pred.extend(neigh.predict_proba(X_cv[i,i+1000])[:,1])


    train_auc.append(roc_auc_score(y_train,y_train_pred))
    cv_auc.append(roc_auc_score(y_cv, y_cv_pred))

plt.plot(K, train_auc, label='Train AUC')
plt.plot(K, cv_auc, label='CV AUC')

Output: ValueError: Incompatible dimension for X and Y matrices: 
X.shape[1] == 1 while Y.shape[1] == 7054

0 个答案:

没有答案