python missing 1 required positional argument
As pointed out in the comments your mistake is in the line:
my_classifier = ScrappyKNN
Which you should change to
my_classifier = ScrappyKNN()
The reason you got the error you did is based on how input arguments work in python. Python uses positional arguments. That just means that unless you tell it otherwise, python just assumes inputs are in the same order as your function definition.
In your code you define fit as a method in the ScrappyKNN class with 3 inputs. The definition of fit is:
fit(self,X_train,y_train)
You can see there are three inputs, in order: self, x_train and y_train.
Normally this method would be called by an object of class ScrappyKNN. When you call a class method using an object (as in object.method()), the object is being used as the first input to the method. So in my_classifier.fit(X,y) behind the scenes what is happening is the method fit being called with inputs my_classifier, X,y.
However, in your code you are calling fit without ever instantiating an object, but rather using a reference to the class ScrappyKNN. Because you haven't made an object, there is no "self" to use as an input, so the call to fit only sees 2 inputs.
The error says
Blockquote fit() missing 1 required positional argument: 'y_train'
This is because fit has 3 required arguments, self, x_train and y_train. Python always assumes your first input is self, your second input is X_train, and your third input is y_train. Because you gave it two inputs, it uses your first input, your X_train variable, as self, and your second input, your y_train variable, as X_train. It then can't find a third argument, so it reports to you that it is missing y_train. In reality, the argument you are missing is self, but python has no way of knowing that.
Changing your call from ScrappyKNN to ScrappyKNN() instantiates an object of class ScrappyKNN. Using that object to call a method in the ScrappyKNN class then passes self as the first argument to fit, giving it a total of 3 arguments and solving the error you are seeing.
Bishoy Youhanna
Updated on July 26, 2020Comments
-
Bishoy Youhanna almost 4 years
This is a simple ML program that creates a classifier. I created it by following the Google developers playlist on ML. When I run the program the error(TypeError: fit() missing 1 required positional argument: 'y_train') is outputted. I don't understand how this is possible.
from scipy.spatial import distance def euc(a,b): return distance.euclidean(a,b) class ScrappyKNN(): def fit(self,X_train,y_train): self.X_train=X_train self.Y_train=y_train def predict(self,X_test): predictions=[] for row in X_test: label = self.closest(row) predictions.append(label) return predictions def closest(self,row): best_dist = euc(row,self.X_train[0]) best_index=0 for i in range(1,len(self.X_train)): dist= euc(row,self.X_train[i]) if dist>best_dist: best_dist=dist best_index=i return self.Y_train[best_index] from sklearn import datasets from sklearn.cross_validation import train_test_split from sklearn.metrics import accuracy_score iris = datasets.load_iris() X=iris.data y=iris.target X_train, X_test, y_train, y_test=train_test_split(X,y,test_size=0.5) my_classifier=ScrappyKNN my_classifier.fit(X_train, y_train) predictions=my_classifier.predict(X_test) print(accuracy_score(y_test,predictions))