I'm trying to make an algorithm in python that defines handwritten English letters, but I get an error when I try to learn. My code is:

import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.neighbors import KNeighborsClassifier as knc abc_train = pd.read_csv("alphabet.csv") columns = ["let"] for i in range(784): columns.append(f"px_{i+1}") abc_train.columns = columns label_name = abc_train["let"].values let_train = abc_train.values[:, 1:] pict_train = let_train.reshape(372450 , 28, 28) knc = knc.fit( pict_train, label_name) 

Mistake:

 TypeError Traceback (most recent call last) <ipython-input-54-5c26061e5501> in <module> ----> 1 knc = knc.fit( pict_train[1:14325], label_name) TypeError: fit() missing 1 required positional argument: 'y' 

What am I doing wrong? How can I solve this problem / error? Whether there can be it that in a correct conclusion is not specified in?

1 answer 1

The error is that you did not create an object of class KNeighborsClassifier , but used the method of class KNeighborsClassifier.fit() .

Try this:

 from sklearn.neighbors import KNeighborsClassifier knc = KNeighborsClassifier(n_neighbors=5) ... knc.fit(pict_train, label_name) 

PS @ gil9red perfectly supplemented the answer in the comments :

in python, the object's methods have the first, non-obvious self parameter, into which an object reference is placed, which the method called and in the case of the method being called by the class (and not the object), the pict_train object fell into that self parameter, and the label_name , respectively, got into X why swears on y ?

  • 2
    вы не создали объект класса KNeighborsClassifier , here I overlooked the author of the question :) - gil9red
  • 2
    @ gil9red, I also didn’t immediately notice ... - MaxU
  • one
    Kst, mb indicate in the response that in the python the object's methods have the first, non-obvious parameter self , in which a reference is made to the object, which the method called and in the case of the method being called by the class (and not the object), the picttrain object got into that parameter self, and label_name, respectively, got into X, and therefore swears at y? - gil9red
  • @ gil9red, great idea! Post as an answer? :) - MaxU
  • one
    I do not think it is better to add to your answer, such as PS. , or let it remain in my comments - gil9red