python - 类型错误:预测()缺少 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 个参数。我该如何解决这个问题?由于我使用预测来比较结果和实际标签,因此它显示了模型的准确性
解决方案
为了清楚起见,我没有抓住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/线性回归
推荐阅读
- javascript - react中状态变化时如何添加动画?
- marklogic - POST“/v1/rest-apis”没有按预期工作
- java - 在Java中创建和访问文件时的默认路径不应该与jar一起使用吗?
- python - 在带有返回另一个 DataFrame 的函数的 pandas DataFrame 上使用 apply
- javascript - 如何让我的 JavaScript 程序运行得更快?
- python - 在 Python 中将字典频率转换为列表的最快方法是什么?
- azure-sql-database - 将数据从一个 Azure SQL DB 复制到另一个的有效方法
- apache-flink - Apache Flink 加入流
- sql - 更新语句 Sql Access 双内连接
- python - 粘贴页面中的所有文本 Selenium webdriver