小编Bis*_*nna的帖子

python 缺少 1 个必需的位置参数

这是一个创建分类器的简单 ML 程序。我按照谷歌开发人员在 ML 上的播放列表创建了它。当我运行程序时,输出错误(TypeError: fit() missing 1 required positional argument: 'y_train')。我不明白这怎么可能。

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, …
Run Code Online (Sandbox Code Playgroud)

python machine-learning typeerror scipy scikit-learn

4
推荐指数
1
解决办法
3万
查看次数