|
a |
|
b/binary_classification/cart.py |
|
|
1 |
from sklearn import tree |
|
|
2 |
from sklearn.metrics import roc_auc_score |
|
|
3 |
from util import roc_results, results |
|
|
4 |
|
|
|
5 |
|
|
|
6 |
def cart_training(x_train, y_train): |
|
|
7 |
""" |
|
|
8 |
:param x_train: the x-values we want to train on (2D numpy array) |
|
|
9 |
:param y_train: the y-values that correspond to x_train (1D numpy array) |
|
|
10 |
:return: sklearn CART Classifier object that can now be used for predictions |
|
|
11 |
""" |
|
|
12 |
clf = tree.DecisionTreeClassifier(criterion='entropy', splitter='best', max_depth=5, min_samples_split=2, |
|
|
13 |
min_samples_leaf=1, min_impurity_decrease=0.023, random_state=0) |
|
|
14 |
clf.fit(x_train, y_train) |
|
|
15 |
return clf |
|
|
16 |
|
|
|
17 |
|
|
|
18 |
def cart_classification(clf, x_test): |
|
|
19 |
""" |
|
|
20 |
:param clf: trained sklearn CART Classifier object |
|
|
21 |
:param x_test: the x-values we want to get predictions on (2D numpy array) |
|
|
22 |
:return: a 1D numpy array containing the predictions |
|
|
23 |
""" |
|
|
24 |
return clf.predict(x_test) |
|
|
25 |
|
|
|
26 |
|
|
|
27 |
def cart_pipeline(x_train, y_train, x_test, y_test): |
|
|
28 |
""" |
|
|
29 |
:param x_train: the x-values we want to train on (2D numpy array) |
|
|
30 |
:param y_train: the y-values that correspond to x_train (1D numpy array) |
|
|
31 |
:param x_test: the x-values we want to test on (2D numpy array) |
|
|
32 |
:param y_test: the y-values that correspond to x_test (1D numpy array) |
|
|
33 |
:return: the roc auc score |
|
|
34 |
""" |
|
|
35 |
clf = cart_training(x_train, y_train) |
|
|
36 |
y_pred = cart_classification(clf, x_test) |
|
|
37 |
roc_results(y_pred, y_test, 'CART') |
|
|
38 |
return roc_auc_score(y_test, y_pred), results(y_pred, y_test) |