-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathRandom_forest.py
52 lines (38 loc) · 1.83 KB
/
Random_forest.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
import logging
import numpy as np
from sklearn.datasets import make_classification
from sklearn.datasets import make_regression
from sklearn.metrics import roc_auc_score, accuracy_score
try:
from sklearn.model_selection import train_test_split
except ImportError:
from sklearn.cross_validation import train_test_split
from mla.ensemble.random_forest import RandomForestClassifier, RandomForestRegressor
from mla.metrics.metrics import mean_squared_error
logging.basicConfig(level=logging.DEBUG)
def classification():
# Generate a random binary classification problem.
X, y = make_classification(
n_samples=500, n_features=10, n_informative=10, random_state=1111, n_classes=2, class_sep=2.5, n_redundant=0
)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15, random_state=1111)
model = RandomForestClassifier(n_estimators=10, max_depth=4)
model.fit(X_train, y_train)
predictions_prob = model.predict(X_test)[:, 1]
predictions = np.argmax(model.predict(X_test), axis=1)
#print(predictions.shape)
print("classification, roc auc score: %s" % roc_auc_score(y_test, predictions_prob))
print("classification, accuracy score: %s" % accuracy_score(y_test, predictions))
def regression():
# Generate a random regression problem
X, y = make_regression(
n_samples=500, n_features=5, n_informative=5, n_targets=1, noise=0.05, random_state=1111, bias=0.5
)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=1111)
model = RandomForestRegressor(n_estimators=50, max_depth=10, max_features=3)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
print("regression, mse: %s" % mean_squared_error(y_test.flatten(), predictions.flatten()))
if __name__ == "__main__":
classification()
# regression()