Repository navigation
Expand file tree
/
Copy pathbasic_usage.py
More file actions
119 lines (98 loc) · 2.99 KB
/
Copy pathbasic_usage.py
File metadata and controls
119 lines (98 loc) · 2.99 KB
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
112
113
114
115
116
117
118
119
"""Basic citrees classifier, regressor, and forest example."""
from __future__ import annotations
import os
os.environ.setdefault("KMP_WARNINGS", "0")
import numpy as np
from sklearn.datasets import make_classification, make_regression
from sklearn.metrics import accuracy_score, r2_score
from sklearn.model_selection import train_test_split
from citrees import (
ConditionalInferenceForestClassifier,
ConditionalInferenceTreeClassifier,
ConditionalInferenceTreeRegressor,
MaxValuesMethod,
)
def top_features(importances: np.ndarray, n: int = 3) -> list[int]:
"""Return indices of the largest feature-importance values."""
return np.argsort(importances)[::-1][:n].tolist()
def classification_tree_example() -> None:
X, y = make_classification(
n_samples=240,
n_features=12,
n_informative=4,
n_redundant=2,
random_state=42,
shuffle=False,
)
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.3,
random_state=42,
stratify=y,
)
tree = ConditionalInferenceTreeClassifier(
selector="mc",
splitter="gini",
n_resamples_selector=20,
n_resamples_splitter=20,
max_depth=4,
random_state=42,
verbose=0,
)
tree.fit(X_train, y_train)
predictions = tree.predict(X_test)
print("classification tree accuracy:", round(accuracy_score(y_test, predictions), 3))
print("classification tree top features:", top_features(tree.feature_importances_))
def regression_tree_example() -> None:
X, y = make_regression(
n_samples=240,
n_features=12,
n_informative=4,
noise=8.0,
random_state=42,
)
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.3,
random_state=42,
)
tree = ConditionalInferenceTreeRegressor(
selector="pc",
splitter="mse",
n_resamples_selector=20,
n_resamples_splitter=20,
max_depth=4,
random_state=42,
verbose=0,
)
tree.fit(X_train, y_train)
predictions = tree.predict(X_test)
print("regression tree r2:", round(r2_score(y_test, predictions), 3))
print("regression tree top features:", top_features(tree.feature_importances_))
def forest_ranking_example() -> None:
X, y = make_classification(
n_samples=260,
n_features=16,
n_informative=5,
n_redundant=3,
random_state=7,
shuffle=False,
)
forest = ConditionalInferenceForestClassifier(
n_estimators=5,
max_features=MaxValuesMethod.SQRT,
n_resamples_selector=20,
n_resamples_splitter=20,
max_depth=4,
n_jobs=1,
random_state=7,
verbose=0,
)
forest.fit(X, y)
print("forest top features:", top_features(forest.feature_importances_, n=5))
if __name__ == "__main__":
classification_tree_example()
regression_tree_example()
forest_ranking_example()