Search code examples
pythonmachine-learningscikit-learnclassificationknn

Invalid shape error while using Knn Classfier


Following are the X and Y variable shapes:

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

## Output for shapes 
X_train.shape  = (970, 298) 
X_test.shape   = (478, 298)
len(y_train)   =  970
len(y_test)    =  478

Now I assign Multi-output classifier from Knn :

knn = KNeighborsClassifier(n_neighbors=3)
classifier = MultiOutputClassifier(knn, n_jobs=-1)
classifier.fit(X_train,y_train)

predictions = classifier.predict(X_test)
print classifier.score(y_test,predictions)

When I try to run this,I get the following error:

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

Now I can make out that the error is something related to the shape of the variables, maybe I am mixing them while splitting them for training or testing.

Tried searching but to no avail, what mistake am I making?

Sample :

X = (0, 96) 0.24328157992528274
(0, 191)    0.4086854706249901
(0, 279)    0.3597892480519696
(0, 209)    0.6262243704015803
(0, 287)    0.15142673105175225
(0, 44) 0.2839334104854308
(0, 31) 0.27493029497336746
(0, 62) 0.2702778021025414

Y  =[1252, 12607, 12596], [12480, 12544, 12547], [1252, 12607, 12547], [12480, 12607, 12547], [12480, 12607, 12596], [1252, 12607, 12547], [12480, 12544, 12547], [1252, 12607, 12596], [1252, 12607, 12596], [12480, 12544, 12547], [12480, 12607, 12596]

Solution

  • From Documentation:

    Returns the mean accuracy on the given test data and labels.
    
    In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.
    
    Parameters: 
    X : array-like, shape = (n_samples, n_features)
    Test samples.
    
    y : array-like, shape = (n_samples) or (n_samples, n_outputs)
    True labels for X.
    
    sample_weight : array-like, shape = [n_samples], optional
    Sample weights.
    
    Returns:    
    score : float
    Mean accuracy of self.predict(X) wrt. y
    

    Hence, you need to give X, y for the score function and not y_true and y_pred

    Try:

    print classifier.score(X_test, np.array(y_test))