-1
$\begingroup$

I am fairly new to python and I have a program for data classification using the k-nearest neighbor method. But I encountered an error when running the program.

  • Here my source code:
import os

import numpy as np

import cv2

from sklearn.neighbors import KNeighborsClassifier

from sklearn.utils import shuffle

from sklearn.model_selection import train_test_split


def load_images(path):
    
    #path = ./dataset/
    
    x_, y_ = [], []
    
    labels = os.listdir(path)
    
    for label in labels:
        
        images = os.listdir(path + label)
        
        for img in images:
            
            im = cv2.imread(path+label+"/"+img)
            
            im = cv2.resize(im, (100,100))
            
            x_.append(im)
            
            y_.append(label)
    
   return x_, y_


X, Y = load_images("./dataset/")

X = np.array(X)

Y = np.array(Y)

print(X.shape, Y.shape)


#flatten

X = X.reshape(X.shape[1:])

X = X.transpose()

print(X.shape)


X, Y = shuffle(X, Y, random_state=0)

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2)


knn = KNeighborsClassifier()

knn.fit(X_train, Y_train)

print(knn.score(X_test, Y_test))
  • error when the program is running

enter image description here

Please help me to solve problems in the program. Thanks

$\endgroup$
2
  • $\begingroup$ What are you expecting to do? What do you want the final shape of $X$ to be? $\endgroup$ – Übermensch Mar 23 at 14:23
  • $\begingroup$ honestly, I don't know the meaning of this program. i got this program from github for my assignment. When the program run, there is an error like this: "Found input variables with inconsistent numbers of samples: [20, 30]" then i changed the source code like this: ""X = X.reshape(20,-1) print(X.shape)"" to be: ""X = X.reshape(X.shape[1:]) X = X.transpose() print(X.shape)"" error that occurs in the program becomes like this: "cannot reshape array of size 900000 into shape (100,100,3)." so, what to do? Thanks $\endgroup$ – Monica Kristy Mar 23 at 17:59
0
$\begingroup$

You can flatten X to have (number_of_batches, flatten_dims) rather than (number_of_batches, dim_1, dim_2, dim_3).

According to your code, the initial shape of X is $(30, 100, 100, 3)$ which translates to having $30$ images each of $(100 \times 100)$ dimension and $3$ channels. To flatten X from $(30,100,100,3)$ to $(30, 100\times100\times3)$ you could replace:

X = X.reshape(X.shape[1:])
X = X.transpose()

with:

X = X.reshape(30, -1)

Check out here to understand how it is working.

Alternatively, you could avoid using .reshape() by slicing and broadcasting:

X = X[:,0,0,0][:, np.newaxis] * X[-1:].flatten()[np.newaxis, :]

But not only does this look unintuitive but might also be computationally less efficient.

$\endgroup$
1
  • $\begingroup$ Amazing. Your answer really helped me. Thank you very much. :D $\endgroup$ – Monica Kristy Mar 25 at 2:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.