python - 继承scikit-learn的LassoCV模型

标签 python scikit-learn

我尝试使用继承来扩展 scikit-learn 的 RidgeCV 模型:

from sklearn.linear_model import RidgeCV, LassoCV

class Extended(RidgeCV):
    def __init__(self, *args, **kwargs):
        super(Extended, self).__init__(*args, **kwargs)

    def example(self):
        print 'Foo'


x = [[1,0],[2,0],[3,0],[4,0], [30, 1]]
y = [2,4,6,8, 60]
model = Extended(alphas = [float(a)/1000.0 for a in range(1, 10000)])
model.fit(x,y)
print model.predict([[5,1]])

它工作得很好,但是当我试图从 LassoCV 继承时,它产生了以下回溯:

Traceback (most recent call last):
  File "C:/Python27/so.py", line 14, in <module>
    model.fit(x,y)
  File "C:\Python27\lib\site-packages\sklearn\linear_model\coordinate_descent.py", line 1098, in fit
    path_params = self.get_params()
  File "C:\Python27\lib\site-packages\sklearn\base.py", line 214, in get_params
    for key in self._get_param_names():
  File "C:\Python27\lib\site-packages\sklearn\base.py", line 195, in _get_param_names
    % (cls, init_signature))
RuntimeError: scikit-learn estimators should always specify their parameters in the signature of their __init__ (no varargs). <class '__main__.Extended'> with constructor (<self>, *args, **kwargs) doesn't  follow this convention.

有人可以解释一下如何解决这个问题吗?

最佳答案

您可能想要制作 scikit-learn 兼容模型,以进一步将其与可用的 scikit-learn 功能一起使用。如果你这样做 - 你需要先阅读这个: http://scikit-learn.org/stable/developers/contributing.html#rolling-your-own-estimator

简而言之:scikit-learn 具有许多功能,如估算器克隆(clone() 函数)、元算法(如 GridSearchPipeline)、交叉验证。所有这些东西都必须能够获取估算器内部字段的值,并更改这些字段的值(例如 GridSearch 必须在每次评估之前更改估算器内部的参数),例如参数SGDClassifier 中的 alpha。要更改某些参数的值,它必须知道它的名称。要从 BaseEstimator 类(您隐式继承)中获取每个分类器方法 get_params 中所有字段的名称,需要在 __init__ 中指定所有参数> 一个类的方法,因为很容易反省 __init__ 方法的所有参数名称(查看 BaseEstimator,这是抛出此错误的类)。

所以它只是想让你删除所有可变参数,比如

*args, **kwargs

来自 __init__ 签名。您必须在 __init__ 签名中列出模型的所有参数,并初始化对象的所有内部字段。

这是 SGDClassifier__init__ 方法示例,继承自 BaseSGDClassifier:

def __init__(self, loss="hinge", penalty='l2', alpha=0.0001, l1_ratio=0.15,
             fit_intercept=True, n_iter=5, shuffle=True, verbose=0,
             epsilon=DEFAULT_EPSILON, n_jobs=1, random_state=None,
             learning_rate="optimal", eta0=0.0, power_t=0.5,
             class_weight=None, warm_start=False, average=False):
    super(SGDClassifier, self).__init__(
        loss=loss, penalty=penalty, alpha=alpha, l1_ratio=l1_ratio,
        fit_intercept=fit_intercept, n_iter=n_iter, shuffle=shuffle,
        verbose=verbose, epsilon=epsilon, n_jobs=n_jobs,
        random_state=random_state, learning_rate=learning_rate, eta0=eta0,
        power_t=power_t, class_weight=class_weight, warm_start=warm_start, average=average)

关于python - 继承scikit-learn的LassoCV模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40025406/

相关文章:

python - 如何从 kdeplot 获取半高全宽 (FWHM)

python - 如何将Tensorflow张量尺寸(形状)作为int值?

python - Cloud Run/Docker 加载大文件以进行 ML 预测

python - 自定义模型移至 scikit-learn

python - 如何在 GridSearchCV 中使用 TransformedTargetRegressor?

python - Scikit-Learn .fit() 方法如何将数据传递给 .predict()?

python - 在 Python Pandas 的源代码中,pd.read_csv 处理的 URL 在哪里?

python - 为什么 np.array 将列表及其元素转换为数组?

python - Pycaret 混合/堆栈调整模型

python - 如何正确合并模型中模型的输出?