adr*_*rin 4 python python-3.x scikit-learn grid-search
使用 python 和 scikit-learn,我想进行网格搜索。但我的一些模型最终是空的。如何使网格搜索功能忽略这些模型?
我想我可以有一个评分函数,如果模型为空,它会返回 0,但我不确定如何实现。
predictor = sklearn.svm.LinearSVC(penalty='l1', dual=False, class_weight='auto')
param_dist = {'C': pow(2.0, np.arange(-10, 11))}
learner = sklearn.grid_search.GridSearchCV(estimator=predictor,
param_grid=param_dist,
n_jobs=self.n_jobs, cv=5,
verbose=0)
learner.fit(X, y)
Run Code Online (Sandbox Code Playgroud)
我的数据的方式是该learner对象将选择C与空模型相对应的数据。知道如何确保模型不为空吗?
编辑:“空模型”是指选择了 0 个要使用的特征的模型。特别是对于l1正则化模型,这种情况很容易发生。所以在这种情况下,如果CSVM中的 足够小,优化问题就会找到0向量作为系数的最优解。因此predictor.coef_将是 s 的向量0。
尝试实现自定义记分器,类似于:
import numpy as np
def scorer_(estimator, X, y):
# Your criterion here
if np.allclose(estimator.coef_, np.zeros_like(estimator.coef_)):
return 0
else:
return estimator.score(X, y)
learner = sklearn.grid_search.GridSearchCV(...
scoring=scorer_)
Run Code Online (Sandbox Code Playgroud)