-
Notifications
You must be signed in to change notification settings - Fork 0
/
q7_RandomForest.py
49 lines (40 loc) · 1.27 KB
/
q7_RandomForest.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
"""
The current code given is for the Assignment 2.
> Classification
> Regression
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from metrics import *
from tree.randomForest import RandomForestClassifier
from tree.randomForest import RandomForestRegressor
np.random.seed(42)
########### RandomForestClassifier ###################
N = 30
P = 5
X = pd.DataFrame(np.random.randn(N, P))
y = pd.Series(np.random.randint(P, size = N), dtype="category")
for criteria in ['information_gain', 'gini_index']:
Classifier_RF = RandomForestClassifier(10, criterion = criteria)
Classifier_RF.fit(X, y)
y_hat = Classifier_RF.predict(X)
# Classifier_RF.plot()
print('Criteria :', criteria)
print('Accuracy: ', accuracy(y_hat, y))
for cls in y.unique():
print('Precision: ', precision(y_hat, y, cls))
print('Recall: ', recall(y_hat, y, cls))
########### RandomForestRegressor ###################
N = 30
P = 5
X = pd.DataFrame(np.random.randn(N, P))
y = pd.Series(np.random.randn(N))
criteria = "variance"
Regressor_RF = RandomForestRegressor(10, criterion = criteria)
Regressor_RF.fit(X, y)
y_hat = Regressor_RF.predict(X)
# Regressor_RF.plot()
print('Criteria :', criteria)
print('RMSE: ', rmse(y_hat, y))
print('MAE: ', mae(y_hat, y))