调整cros_val_score sklearn中Lasso的阈值



我看到这篇文章训练了一个LGBM模型,但我想知道如何将其用于Lasso。我知道预测不一定在0到1之间,但我想试试这个模型。我试过这个,但它不工作:

import numpy as np 
from lightgbm import LGBMClassifier
from sklearn.datasets import make_classification
from sklearn.linear_model import Lasso
X, y = make_classification(n_features=10, random_state=0, n_classes=2, n_samples=1000, n_informative=8)
class Lasso(Lasso):
def predict(self,X, threshold=0.5):
result = super(Lasso, self).predict_proba(X)
predictions = [1 if p>threshold else 0 for p in result[:,0]]
return predictions
clf = Lasso(alpha=0.05)
clf.fit(X,y)
precision = cross_val_score(Lasso(),X,y,cv=5,scoring='precision')

UserWarning: Scoring failed. The score on this train-test partition for these parameters will be set to nan

您选择的特定模型类(Lasso())实际上用于回归问题,因为它最小化了惩罚平方损失,这在您的情况下是不合适的。相反,您可以使用LogisticRegression()L1惩罚来优化具有Lasso惩罚的逻辑函数。为了控制正则化强度,使用C=参数(参见文档)。

from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
X, y = make_classification(
n_features=10, random_state=0, n_classes=2, n_samples=1000, n_informative=8
)
class LassoLR(LogisticRegression):
def predict(self,X, threshold=0.5):
result = super(LassoLR, self).predict_proba(X)
predictions = [1 if p>threshold else 0 for p in result[:,0]]
return predictions
clf = LassoLR(penalty='l1', solver='liblinear', C=1.)
clf.fit(X,y)
precision = cross_val_score(LassoLR(),X,y,cv=5,scoring='precision')
print(precision)
# array([0.04166667, 0.08163265, 0.1010101 , 0.125     , 0.05940594])

最新更新