ParrotPrediction / docker-course-xgboost

Materials for an online-course - "Practical XGBoost in Python"
http://education.parrotprediction.teachable.com/courses/practical-xgboost-in-python
217 stars 136 forks source link

using sklearn api for imbalanced data #10

Open rayeaster opened 6 years ago

rayeaster commented 6 years ago
from sklearn.metrics import accuracy_score, precision_score, recall_score
from xgboost.sklearn import XGBClassifier
from sklearn.utils import class_weight
......
#using skleran api for imbalanced data
class_weight = class_weight.compute_class_weight('balanced', np.unique(y_train), y_train)

weightbase = class_weight[0]
for index, w in np.ndenumerate(class_weight):
    class_weight[index] = w/weightbase

params = {
    'objective': 'binary:logistic',
    'max_depth': 1,
    'learning_rate': 1,
    'n_estimators': 15
}

sample_weight=np.ones(y_train.shape);
for index, y in np.ndenumerate(y_train):    
    sample_weight[index] = class_weight[y]

bst = XGBClassifier(**params).fit(X_train, y_train, sample_weight=sample_weight)
y_pred = bst.predict(X_test)
predictions = [round(value) for value in y_pred]
print('sklearn-Accuracy: {0:.2f}'.format(accuracy_score(y_test, predictions)))
print('sklearn-Precision: {0:.2f}'.format(precision_score(y_test, predictions)))
print('sklearn-Recall: {0:.2f}'.format(recall_score(y_test, predictions)))