sklearn0.22版本,用以下代码建立逻辑回归模型
lr = LogisticRegression(C = c_param, penalty = 'l1')
正则化惩罚选择'L1'报错,一脸懵C
嗯,还是打印一下Ir看一下参数
LogisticRegression(C=0.01, class_weight=None, dual=False, fit_intercept=True,
intercept_scaling=1, l1_ratio=None, max_iter=100,
multi_class='auto', n_jobs=None, penalty='l1',
random_state=None, solver='lbfgs', tol=0.0001, verbose=0,
warm_start=False)
问题就出现在solver这个参数上,这个参数定义的是分类器,‘newton-cg’,‘sag’和‘lbfgs’等solvers仅支持‘L2’regularization,‘liblinear’ solver同时支持‘L1’、‘L2’regularization,若dual=Ture,则仅支持L2 penalty。
决定惩罚项选择的有2个参数:dual和solver,如果要选L1范数,dual必须是False,solver必须是liblinear
问题搞清楚了,把上面代码改成:
lr = LogisticRegression(C = c_param, penalty = 'l1',solver='liblinear')