-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathclassifier_proba_comb_example.py
111 lines (88 loc) · 3.9 KB
/
classifier_proba_comb_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# -*- coding: utf-8 -*-
"""Example of combining multiple base classifiers. Two combination
frameworks are demonstrated:
1. Average: take the average of all base detectors
2. maximization : take the maximum score across all detectors as the score
"""
# Author: Yue Zhao <[email protected]>
# License: BSD 2 clause
import os
import sys
# temporary solution for relative imports in case combo is not installed
# if combo is installed, no need to use the following line
sys.path.append(
os.path.abspath(os.path.join(os.path.dirname("__file__"), '..')))
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import roc_auc_score
from sklearn.datasets import load_breast_cancer
from combo.models.classifier_comb import SimpleClassifierAggregator
from combo.utils.data import evaluate_print
import warnings
warnings.filterwarnings("ignore")
if __name__ == "__main__":
# Define data file and read X and y
random_state = 42
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4,
random_state=random_state)
# fit and predict by individual classifiers
clf = DecisionTreeClassifier(random_state=random_state)
clf.fit(X_train, y_train)
print('Decision Tree |',
np.round(roc_auc_score(y_test, clf.predict_proba(X_test)[:, 1]),
decimals=4))
clf = LogisticRegression(random_state=random_state)
clf.fit(X_train, y_train)
print('Logistic Regression |',
np.round(roc_auc_score(y_test, clf.predict_proba(X_test)[:, 1]),
decimals=4))
clf = KNeighborsClassifier()
clf.fit(X_train, y_train)
print('K Neighbors |',
np.round(roc_auc_score(y_test, clf.predict_proba(X_test)[:, 1]),
decimals=4))
clf = GradientBoostingClassifier(random_state=random_state)
clf.fit(X_train, y_train)
print('Gradient Boosting |',
np.round(roc_auc_score(y_test, clf.predict_proba(X_test)[:, 1]),
decimals=4))
clf = RandomForestClassifier(random_state=random_state)
clf.fit(X_train, y_train)
print('Random Forest |',
np.round(roc_auc_score(y_test, clf.predict_proba(X_test)[:, 1]),
decimals=4))
print()
# initialize a group of classifiers
classifiers = [DecisionTreeClassifier(random_state=random_state),
LogisticRegression(random_state=random_state),
KNeighborsClassifier(),
RandomForestClassifier(random_state=random_state),
GradientBoostingClassifier(random_state=random_state)]
# combine by averaging
clf = SimpleClassifierAggregator(classifiers, method='average')
clf.fit(X_train, y_train)
y_test_predicted = clf.predict_proba(X_test)
print('Combination by avg |', np.round(
roc_auc_score(y_test, clf.predict_proba(X_test)[:, 1]),
decimals=4))
# combine by weighted averaging
clf_weights = np.array([0.1, 0.4, 0.1, 0.2, 0.2])
clf = SimpleClassifierAggregator(classifiers, method='average')
clf.fit(X_train, y_train)
y_test_predicted = clf.predict_proba(X_test)
print('Combination by w_avg|', np.round(
roc_auc_score(y_test, clf.predict_proba(X_test)[:, 1]),
decimals=4))
# combine by maximization
clf = SimpleClassifierAggregator(classifiers, method='maximization')
clf.fit(X_train, y_train)
y_test_predicted = clf.predict_proba(X_test)
print('Combination by max |', np.round(
roc_auc_score(y_test, clf.predict_proba(X_test)[:, 1]),
decimals=4))