首页 > 解决方案 > 类型错误:预测()缺少 1 个必需的位置参数:'y_train'

问题描述

我无法将 GridSearchCV 与我的自定义估算器一起使用。我收到一条警告说评分失败并且我的 predict() 缺少 1 个位置参数,即 y_train。这是我的代码`class 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 个参数。我该如何解决这个问题?由于我使用预测来比较结果和实际标签,因此它显示了模型的准确性

标签: pythonvalidationscikit-learncross-validationgridsearchcv

解决方案


为了清楚起见,我没有抓住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-learn/线性回归


推荐阅读