调整阈值 cros_val_score sklearn
Adjust threshold cros_val_score sklearn
有一种方法可以设置阈值cross_val_score sklearn?
我已经训练了一个模型,然后我将阈值调整为 0.22。型号如下:
# Try with Threshold
pred_proba = LGBM_Model.predict_proba(X_test)
# Adjust threshold for predictions proba
prediction_with_threshold = []
for item in pred_proba[:,0]:
if item > 0.22 :
prediction_with_threshold.append(0)
else:
prediction_with_threshold.append(1)
print(classification_report(y_test,prediction_with_threshold))
然后我想使用 cross_val_score 验证此模型。我已经搜索过,但找不到为 cross_val_score 设置阈值的方法。我使用的 cross_val_score 如下所示:
F1Scores = cross_val_score(LGBMClassifier(random_state=101,learning_rate=0.01,max_depth=-1,min_data_in_leaf=60,num_iterations=200,num_leaves=70),X,y,cv=5,scoring='f1')
F1Scores
### how to adjust threshold to 0.22 ??
或者有其他方法可以使用阈值验证此模型?
假设您正在处理一个双class class化问题,您可以使用您的阈值方法覆盖LGBMClassifier
对象的predict
方法,如下所示:
import numpy as np
from lightgbm import LGBMClassifier
from sklearn.datasets import make_classification
X, y = make_classification(n_features=10, random_state=0, n_classes=2, n_samples=1000, n_informative=8)
class MyLGBClassifier(LGBMClassifier):
def predict(self,X, threshold=0.22,raw_score=False, num_iteration=None,
pred_leaf=False, pred_contrib=False, **kwargs):
result = super(MyLGBClassifier, self).predict_proba(X, raw_score, num_iteration,
pred_leaf, pred_contrib, **kwargs)
predictions = [1 if p>threshold else 0 for p in result[:,0]]
return predictions
clf = MyLGBClassifier()
clf.fit(X,y)
clf.predict(X,threshold=2) # just testing the implementation
# [0,0,0,0,..,0,0,0] # we get all zeros since we have set threshold as 2
F1Scores = cross_val_score(MyLGBClassifier(random_state=101,learning_rate=0.01,max_depth=-1,min_data_in_leaf=60,num_iterations=2,num_leaves=5),X,y,cv=5,scoring='f1')
F1Scores
#array([0.84263959, 0.83333333, 0.8 , 0.78787879, 0.87684729])
希望对您有所帮助!
有一种方法可以设置阈值cross_val_score sklearn?
我已经训练了一个模型,然后我将阈值调整为 0.22。型号如下:
# Try with Threshold
pred_proba = LGBM_Model.predict_proba(X_test)
# Adjust threshold for predictions proba
prediction_with_threshold = []
for item in pred_proba[:,0]:
if item > 0.22 :
prediction_with_threshold.append(0)
else:
prediction_with_threshold.append(1)
print(classification_report(y_test,prediction_with_threshold))
然后我想使用 cross_val_score 验证此模型。我已经搜索过,但找不到为 cross_val_score 设置阈值的方法。我使用的 cross_val_score 如下所示:
F1Scores = cross_val_score(LGBMClassifier(random_state=101,learning_rate=0.01,max_depth=-1,min_data_in_leaf=60,num_iterations=200,num_leaves=70),X,y,cv=5,scoring='f1')
F1Scores
### how to adjust threshold to 0.22 ??
或者有其他方法可以使用阈值验证此模型?
假设您正在处理一个双class class化问题,您可以使用您的阈值方法覆盖LGBMClassifier
对象的predict
方法,如下所示:
import numpy as np
from lightgbm import LGBMClassifier
from sklearn.datasets import make_classification
X, y = make_classification(n_features=10, random_state=0, n_classes=2, n_samples=1000, n_informative=8)
class MyLGBClassifier(LGBMClassifier):
def predict(self,X, threshold=0.22,raw_score=False, num_iteration=None,
pred_leaf=False, pred_contrib=False, **kwargs):
result = super(MyLGBClassifier, self).predict_proba(X, raw_score, num_iteration,
pred_leaf, pred_contrib, **kwargs)
predictions = [1 if p>threshold else 0 for p in result[:,0]]
return predictions
clf = MyLGBClassifier()
clf.fit(X,y)
clf.predict(X,threshold=2) # just testing the implementation
# [0,0,0,0,..,0,0,0] # we get all zeros since we have set threshold as 2
F1Scores = cross_val_score(MyLGBClassifier(random_state=101,learning_rate=0.01,max_depth=-1,min_data_in_leaf=60,num_iterations=2,num_leaves=5),X,y,cv=5,scoring='f1')
F1Scores
#array([0.84263959, 0.83333333, 0.8 , 0.78787879, 0.87684729])
希望对您有所帮助!