Open rayeaster opened 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)))