MCPcopy Create free account
hub / github.com/dmlc/xgboost / test_multiclass_classification

Function test_multiclass_classification

tests/python/test_with_sklearn.py:51–101  ·  view source on GitHub ↗
(objective)

Source from the content-addressed store, hash-verified

49
50@pytest.mark.parametrize("objective", ["multi:softmax", "multi:softprob"])
51def test_multiclass_classification(objective):
52 from sklearn.datasets import load_iris
53 from sklearn.model_selection import KFold
54
55 def check_pred(preds, labels, output_margin):
56 if output_margin:
57 err = sum(
58 1 for i in range(len(preds)) if preds[i].argmax() != labels[i]
59 ) / float(len(preds))
60 else:
61 err = sum(1 for i in range(len(preds)) if preds[i] != labels[i]) / float(
62 len(preds)
63 )
64 assert err < 0.4
65
66 X, y = load_iris(return_X_y=True)
67 kf = KFold(n_splits=2, shuffle=True, random_state=rng)
68 for train_index, test_index in kf.split(X, y):
69 xgb_model = xgb.XGBClassifier(objective=objective).fit(
70 X[train_index], y[train_index]
71 )
72 assert xgb_model.get_booster().num_boosted_rounds() == 100
73 preds = xgb_model.predict(X[test_index])
74 # test other params in XGBClassifier().fit
75 preds2 = xgb_model.predict(
76 X[test_index], output_margin=True, iteration_range=(0, 1)
77 )
78 preds3 = xgb_model.predict(
79 X[test_index], output_margin=True, iteration_range=None
80 )
81 preds4 = xgb_model.predict(
82 X[test_index], output_margin=False, iteration_range=(0, 1)
83 )
84 labels = y[test_index]
85
86 check_pred(preds, labels, output_margin=False)
87 check_pred(preds2, labels, output_margin=True)
88 check_pred(preds3, labels, output_margin=True)
89 check_pred(preds4, labels, output_margin=False)
90
91 cls = xgb.XGBClassifier(n_estimators=4).fit(X, y)
92 assert cls.n_classes_ == 3
93 proba = cls.predict_proba(X)
94 assert proba.shape[0] == X.shape[0]
95 assert proba.shape[1] == cls.n_classes_
96
97 # custom objective, the default is multi:softprob so no transformation is required.
98 cls = xgb.XGBClassifier(n_estimators=4, objective=tm.softprob_obj(3)).fit(X, y)
99 proba = cls.predict_proba(X)
100 assert proba.shape[0] == X.shape[0]
101 assert proba.shape[1] == cls.n_classes_
102
103
104def test_best_iteration():

Callers

nothing calls this directly

Calls 6

check_predFunction · 0.85
fitMethod · 0.45
num_boosted_roundsMethod · 0.45
get_boosterMethod · 0.45
predictMethod · 0.45
predict_probaMethod · 0.45

Tested by

no test coverage detected