如何在sklearn中使用自定义估计器与GridSearchCV?

我有一个应该与sklearn API兼容的估计器。我试图使用gridsearchcv来拟合这个估计器的一个参数,但我不知道如何操作。

这是我的代码:

import numpy as npimport sklearn as skfrom sklearn.linear_model import LinearRegression, LassoLarsCV, RidgeCVfrom sklearn.linear_model.base import LinearClassifierMixin, SparseCoefMixin, BaseEstimatorclass ELM(BaseEstimator):    def __init__(self, n_nodes, link='rbf', output_function='lasso', n_jobs=1, c=1):        self.n_jobs = n_jobs        self.n_nodes = n_nodes        self.c = c        if link == 'rbf':            self.link = lambda z: np.exp(-z*z)        elif link == 'sig':            self.link = lambda z: 1./(1 + np.exp(-z))         elif link == 'id':            self.link = lambda z: z        else:            self.link = link        if output_function == 'lasso':            self.output_function = LassoLarsCV(cv=10, n_jobs=self.n_jobs)        elif output_function == 'lr':            self.output_function = LinearRegression(n_jobs=self.n_jobs)        elif output_function == 'ridge':            self.output_function = RidgeCV(cv=10)        else:            self.output_function = output_function        return     def H(self, x):        n, p = x.shape        xw = np.dot(x, self.w.T)        xw = xw + np.ones((n, 1)).dot(self.b.T)        return self.link(xw)    def fit(self, x, y, w=None):        n, p = x.shape        self.mean_y = y.mean()        if w == None:            self.w = np.random.uniform(-self.c, self.c, (self.n_nodes, p))        else:            self.w = w        self.b = np.random.uniform(-self.c, self.c, (self.n_nodes, 1))        self.h_train = self.H(x)        self.output_function.fit(self.h_train, y)        return self    def predict(self, x):        self.h_predict = self.H(x)        return self.output_function.predict(self.h_predict)    def get_params(self, deep=True):        return {"n_nodes": self.n_nodes,                 "link": self.link,                "output_function": self.output_function,                "n_jobs": self.n_jobs,                 "c": self.c}    def set_params(self, **parameters):        for parameter, value in parameters.items():            setattr(self, parameter, value)### Fit the c parameter ### X = np.random.normal(0, 1, (100,5))y = X[:,1] * X[:,2] + np.random.normal(0, .1, 100) gs = sk.grid_search.GridSearchCV(ELM(n_nodes=20, output_function='lr'),                                  cv=5,                                  param_grid={"c":np.linspace(0.0001,1,10)},                                 fit_params={})#gs.fit(X, y) # Error

回答:

你的代码中有两个问题:

  1. 你没有为GridSearchCV指定scoring参数。你似乎在做回归,所以mean_squared_error是一个选项。

  2. 你的set_params方法没有返回对象本身的引用。你应该在for循环之后添加return self

    正如Andreas已经提到的,在scikit-learn中,你很少需要重新定义set_paramsget_params。只需继承自BaseEstimator就足够了。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注