gpt4 book ai didi

python - 如何将多个分布列表传递给 sklearn randomizedSearchCV

转载 作者:太空宇宙 更新时间:2023-11-04 03:08:59 26 4
gpt4 key购买 nike

我有一个自定义的 Python 估算器对象 (mkl_regressor)。此类对象的学习参数之一是 float 的 numpy.array。通常 sklearn 估计器对象由单个参数调整,例如 SVM 的 C。因此,randomizedSearchCV 搜索对象采用分布或值列表来从给定分布(在我的示例中是 scipy.stats.expon)中获取所需的值范围。我试图传递分布列表,但没有成功,因为 randomizedSearchCV 不执行分布数组中的元素。这是我尝试过的:

from modshogun import *
import Gnuplot, Gnuplot.funcutils
from numpy import *
from sklearn.metrics import r2_score

class mkl_regressor():

def __init__(self, widths = [0.01, 0.1, 1.0, 10.0, 50.0, 100.0], kernel_weights = [0.01, 0.1, 1.0,], svm_c = 0.01, mkl_c = 1.0, svm_norm = 1, mkl_norm = 1, degree = 2):
self.svm_c = svm_c
self.mkl_c = mkl_c
self.svm_norm = svm_norm
self.mkl_norm = mkl_norm
self.degree = degree
self.widths = widths
self.kernel_weights = kernel_weights


def fit(self, X, y, **params):
for parameter, value in params.items():
setattr(self, parameter, value)

self.feats_train = RealFeatures(X.T)
labels_train = RegressionLabels(y.reshape((len(y), )))
self._kernels_ = CombinedKernel()
for width in self.widths:
kernel = GaussianKernel()
kernel.set_width(width)
kernel.init(self.feats_train,self.feats_train)
self._kernels_.append_kernel(kernel)
del kernel

kernel = PolyKernel(10, self.degree)
self._kernels_.append_kernel(kernel)
del kernel

self._kernels_.init(self.feats_train, self.feats_train)

binary_svm_solver = SVRLight()
self.mkl = MKLRegression(binary_svm_solver)

self.mkl.set_C(self.svm_c, self.svm_c)
self.mkl.set_C_mkl(self.mkl_c)
self.mkl.set_mkl_norm(self.mkl_norm)
self.mkl.set_mkl_block_norm(self.svm_norm)

self.mkl.set_kernel(self._kernels_)
self.mkl.set_labels(labels_train)
self.mkl.train()
self.kernel_weights = self._kernels_.get_subkernel_weights()

def predict(self, X):
self.feats_test = RealFeatures(X.T)
self._kernels_.init(self.feats_train, self.feats_test)
self.mkl.set_kernel(self._kernels_)
return self.mkl.apply_regression().get_labels()

def set_params(self, **params):
for parameter, value in params.items():
setattr(self, parameter, value)

return self

def get_params(self, deep=False):

return {param: getattr(self, param) for param in dir(self) if not param.startswith('__') and not callable(getattr(self,param))}

def score(self, X_t, y_t):

predicted = self.predict(X_t)
return r2_score(predicted, y_t)

if __name__ == "__main__":

from sklearn.grid_search import RandomizedSearchCV as RS
from scipy.stats import randint as sp_randint
from scipy.stats import expon

labels = array([2.0,0.0,2.0,1.0,3.0,2.0])
labels = labels.reshape((len(labels), 1))
data = array([[1.0,2.0,3.0],[1.0,2.0,9.0],[1.0,2.0,3.0],[1.0,2.0,0.0],[0.0,2.0,3.0],[1.0,2.0,3.0]])
labels_t = array([1.,3.,4])
labels_t = labels_t.reshape((len(labels_t), 1))
data_t = array([[20.0,30.0,40.0],[10.0,20.0,30.0],[10.0,20.0,40.0]])
k = 3

param_grid = [ {'svm_c': expon(scale=100, loc=5),
'mkl_c': expon(scale=100, loc=5),
'degree': sp_randint(0, 32),
#'widths': [array([4.0,6.0,8.9,3.0]), array([4.0,6.0,8.9,3.0,2.0, 3.0, 4.0]), array( [100.0, 200.0, 300.0, 400.0])
'widths': [[expon, expon]]
}]

mkl = mkl_regressor()
rs = RS(mkl, param_distributions = param_grid[0], n_iter = 10, n_jobs = 24, cv = k)#, scoring="r2", verbose=True)
rs.fit(data, labels)
preds = rs.predict(data_t)

print "R^2: ", rs.score(data_t, labels_t)
print "Parameters: ", rs.best_params_

通过将 numpy 数组作为参数字典的列表 'widths' 的元素传递,上述代码运行良好。但是,当我尝试传递分布列表时,randomizedSearchCV 对象没有按预期响应:

/home/ignacio/distributionalSemanticStabilityThesis/mkl_test.py in fit(self=<__main__.mkl_regressor instance>, X=array([[ 1.,  2.,  3.],
[ 1., 2., 0.],
[ 0., 2., 3.],
[ 1., 2., 3.]]), y=array([[ 2.],
[ 1.],
[ 3.],
[ 2.]]), **params={})
24 self.feats_train = RealFeatures(X.T)
25 labels_train = RegressionLabels(y.reshape((len(y), )))
26 self._kernels_ = CombinedKernel()
27 for width in self.widths:
28 kernel = GaussianKernel()
---> 29 kernel.set_width(width)
kernel.set_width = <built-in method set_width of GaussianKernel object>
width = <scipy.stats._continuous_distns.expon_gen object>
30 kernel.init(self.feats_train,self.feats_train)
31 self._kernels_.append_kernel(kernel)
32 del kernel
33

TypeError: in method 'GaussianKernel_set_width', argument 2 of type 'float64_t'

我不想强制估算器执行每个分布生成器,因为在这种情况下,randomizedSearchCV 无法控制使用的值。

一些建议?谢谢。

最佳答案

RandomizedSearchCV 可以采用要尝试的参数值列表具有 rvs 采样方法的分布对象。如果您向它传递一个列表,它会假定您传递了一组离散的参数值以从中进行采样。它不支持单个参数的分布列表。如果现有的发行版不适合您的需要,请定制一个。

如果您需要返回数组的分布,只需创建一个具有 rvs() 方法的类即可返回随机样本并传递该实例的实例,而不是单变量分布列表。

关于python - 如何将多个分布列表传递给 sklearn randomizedSearchCV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38667784/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com