Python 如何在 sklearn 中编写自定义估算器并对其使用交叉验证?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20330445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write a custom estimator in sklearn and use cross-validation on it?
提问by Donbeo
I would like to check the prediction error of a new method trough cross-validation. I would like to know if I can pass my method to the cross-validation function of sklearn and in case how.
我想通过交叉验证检查新方法的预测误差。我想知道我是否可以将我的方法传递给 sklearn 的交叉验证函数,以及如何传递。
I would like something like sklearn.cross_validation(cv=10).mymethod.
我想要类似的东西sklearn.cross_validation(cv=10).mymethod。
I need also to know how to define mymethodshould it be a function and which input element and which output
我还需要知道如何定义mymethod它应该是一个函数以及哪个输入元素和哪个输出
For example we can consider as mymethodan implementation of the least square estimator (of course not the ones in sklearn) .
例如,我们可以将其视为mymethod最小二乘估计器的实现(当然不是 sklearn 中的那些)。
I found this tutorial linkbut it is not very clear to me.
我找到了这个教程链接,但对我来说不是很清楚。
In the documentationthey use
在他们使用的文档中
>>> import numpy as np
>>> from sklearn import cross_validation
>>> from sklearn import datasets
>>> from sklearn import svm
>>> iris = datasets.load_iris()
>>> iris.data.shape, iris.target.shape
((150, 4), (150,))
>>> clf = svm.SVC(kernel='linear', C=1)
>>> scores = cross_validation.cross_val_score(
... clf, iris.data, iris.target, cv=5)
...
>>> scores
But the problem is that they are using as estimator clfthat is obtained by a function built in sklearn. How should I define my own estimator in order that I can pass it to the cross_validation.cross_val_scorefunction?
但问题是他们使用的clf是由 sklearn 中内置的函数获得的估计器。我应该如何定义自己的估算器才能将其传递给cross_validation.cross_val_score函数?
So for example suppose a simple estimator that use a linear model $y=x\beta$ where beta is estimated as X[1,:]+alpha where alpha is a parameter. How should I complete the code?
例如,假设一个简单的估计器使用线性模型 $y=x\beta$,其中 beta 被估计为 X[1,:]+alpha ,其中 alpha 是一个参数。我应该如何完成代码?
class my_estimator():
def fit(X,y):
beta=X[1,:]+alpha #where can I pass alpha to the function?
return beta
def scorer(estimator, X, y) #what should the scorer function compute?
return ?????
With the following code I received an error:
使用以下代码我收到一个错误:
class my_estimator():
def fit(X, y, **kwargs):
#alpha = kwargs['alpha']
beta=X[1,:]#+alpha
return beta
>>> cv=cross_validation.cross_val_score(my_estimator,x,y,scoring="mean_squared_error")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\scikit_learn-0.14.1-py2.7-win32.egg\sklearn\cross_validation.py", line 1152, in cross_val_score
for train, test in cv)
File "C:\Python27\lib\site-packages\scikit_learn-0.14.1-py2.7-win32.egg\sklearn\externals\joblib\parallel.py", line 516, in __call__
for function, args, kwargs in iterable:
File "C:\Python27\lib\site-packages\scikit_learn-0.14.1-py2.7-win32.egg\sklearn\cross_validation.py", line 1152, in <genexpr>
for train, test in cv)
File "C:\Python27\lib\site-packages\scikit_learn-0.14.1-py2.7-win32.egg\sklearn\base.py", line 43, in clone
% (repr(estimator), type(estimator)))
TypeError: Cannot clone object '<class __main__.my_estimator at 0x05ACACA8>' (type <type 'classobj'>): it does not seem to be a scikit-learn estimator a it does not implement a 'get_params' methods.
>>>
采纳答案by BartoszKP
The answer also lies in sklearn's documentation.
答案还在于 sklearn 的文档中。
You need to define two things:
您需要定义两件事:
an estimator that implements the
fit(X, y)function,Xbeing the matrix with inputs andybeing the vector of outputsa scorer function, or callable object that can be used with:
scorer(estimator, X, y)and returns the score of given model
一个实现
fit(X, y)函数的估计器,X它是具有输入的矩阵和y输出的向量一个计分器函数或可调用对象,可用于:
scorer(estimator, X, y)并返回给定模型的分数
Referring to your example: first of all, scorershouldn't be a method of the estimator, it's a different notion. Just create a callable:
参考你的例子:首先,scorer不应该是估算器的一种方法,它是一个不同的概念。只需创建一个可调用的:
def scorer(estimator, X, y)
return ????? # compute whatever you want, it's up to you to define
# what does it mean that the given estimator is "good" or "bad"
Or even a more simple solution: you can pass a string 'mean_squared_error'or 'accuracy'(full list available in this part of the documentation) to cross_val_scorefunction to use a predefined scorer.
或者甚至更简单的解决方案:您可以传递一个字符串'mean_squared_error'或'accuracy'(文档的这一部分提供的完整列表)cross_val_score来使用预定义的记分器。
Another possibility is to use make_scorerfactory function.
另一种可能性是使用make_scorer工厂函数。
As for the second thing, you can pass parameters to your model through the fit_paramsdictparameter of the cross_val_scorefunction (as mentioned in the documentation). These parameters will be passed to the fitfunction.
至于第二件事,您可以通过函数的fit_paramsdict参数cross_val_score(如文档中所述)将参数传递给您的模型。这些参数将传递给fit函数。
class my_estimator():
def fit(X, y, **kwargs):
alpha = kwargs['alpha']
beta=X[1,:]+alpha
return beta
After reading all the error messages, which provide quite clear idea of what's missing, here is a simple example:
在阅读了所有错误消息后,这些消息提供了对缺失内容的清晰了解,这里是一个简单的例子:
import numpy as np
from sklearn.cross_validation import cross_val_score
class RegularizedRegressor:
def __init__(self, l = 0.01):
self.l = l
def combine(self, inputs):
return sum([i*w for (i,w) in zip([1] + inputs, self.weights)])
def predict(self, X):
return [self.combine(x) for x in X]
def classify(self, inputs):
return sign(self.predict(inputs))
def fit(self, X, y, **kwargs):
self.l = kwargs['l']
X = np.matrix(X)
y = np.matrix(y)
W = (X.transpose() * X).getI() * X.transpose() * y
self.weights = [w[0] for w in W.tolist()]
def get_params(self, deep = False):
return {'l':self.l}
X = np.matrix([[0, 0], [1, 0], [0, 1], [1, 1]])
y = np.matrix([0, 1, 1, 0]).transpose()
print cross_val_score(RegularizedRegressor(),
X,
y,
fit_params={'l':0.1},
scoring = 'mean_squared_error')

