我想获取虹膜数据并基于GridSearchCV函数选择最佳的物流模型。
到目前为止我的工作
import numpy as np
from sklearn import datasets
from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import LogisticRegression
iris = datasets.load_iris()
X = iris.data[:, :2]
y = iris.target
# Logistic regression
reg_log = LogisticRegression()
# Penalties
pen = ['l1', 'l2','none']
#Regularization strength (numbers from -10 up to 10)
C = np.logspace(-10, 10, 100)
# Possibilities for those parameters
parameters= dict(C=C, penalty=pen)
# choosing best model based on 5-fold cross validation
Model = GridSearchCV(reg_log, parameters, cv=5)
# Fitting best model
Best_model = Model.fit(X, y)
我得到很多错误。你知道我在做什么错吗?
答案 0 :(得分:1)
由于您选择了不同的正则化,因此可以在help page上看到:
“ newton-cg”,“ sag”和“ lbfgs”求解器仅支持L2 用原始公式进行正则化,或不进行正则化。的 “ liblinear”求解器同时支持L1和L2正则化 仅针对L2惩罚制定公式。 Elastic-Net正则化为 仅受“传奇”求解器支持。
我不确定您是否要使用罚分='none'和罚分进行网格搜索。因此,如果您使用saga
并增加迭代次数:
reg_log = LogisticRegression(solver="saga",max_iter=1000)
pen = ['l1', 'l2']
C = [0.1,0.001]
parameters= dict(C=C, penalty=pen)
Model = GridSearchCV(reg_log, parameters, cv=5)
Best_model = Model.fit(X, y)
res = pd.DataFrame(Best_model.cv_results_)
res[['param_C','param_penalty','mean_test_score']]
param_C param_penalty mean_test_score
0 0.1 l1 0.753333
1 0.1 l2 0.833333
2 0.001 l1 0.333333
3 0.001 l2 0.700000
工作正常。如果您的惩罚值出现更多错误,请尝试查看它们,并确保它们不是一些疯狂的值。