类型错误: predict() 缺少 1 个必需的位置参数: 'y_train'



我在自定义估计器中使用GridSearchCV时遇到了问题。我收到一个警告,说得分失败,我的预测((缺少一个位置论点,即y_train。这是我的代码`类FLVQ(BaseEstimator(:

def __init__ (self, a=5):
self.a = a
def fit(self, x_train, y_train):
#somecodehere
return self

def compare(self, x):
distance = self.count_dist(x)
index_dist = np.argmin(distance, axis=1)
return np.array([self.centroid[i] for i in index_dist])
def predict(self, x_train, y_train):
return np.sum(self.compare(x_train)==y_train.flatten())/x_train.shape[0]
`

当我运行我的GS:时

params={'a':[3,5]},
gs = GridSearchCV(FLVQ(), param_grid=params,cv=4, scoring="accuracy")   
gs.fit(x_train,y_train)
gs.predict(x_test)

我得到了";用户警告:评分失败。这些参数在列车测试分区上的分数将设置为nan"和TypeError,因为我的预测缺少1个参数。我该如何解决这个问题?由于我使用预测来比较结果和实际标签,因此它显示了模型的准确性

为了清楚起见,我没有抓住在预测函数中写入y_train的要点,只是为了展示我的想法,请看下面的内容:

import numpy as np
from sklearn.linear_model import LinearRegression
LR = LinearRegression()
data = np.arange(0,100)
x_train, y_train= np.split(data.sample(frac=1,random_state=1500),[int(0.7 * len(data))])
LR.fit(X_train,y_train)
LR.predict(x_train)

要获得更多信息,Scikit学习/线性回归

最新更新