-
Notifications
You must be signed in to change notification settings - Fork 0
/
q6_Bagging.py
41 lines (35 loc) · 1.17 KB
/
q6_Bagging.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
"""
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 ensemble.bagging import BaggingClassifier
from tree.base import DecisionTree
# Or use sklearn decision tree
# from linearRegression.linearRegression import LinearRegression
from sklearn import tree
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, plot_tree
########### BaggingClassifier ###################
N = 30
P = 2
NUM_OP_CLASSES = 2
n_estimators = 3
X = pd.DataFrame(np.abs(np.random.randn(N, P)))
y = pd.Series(np.random.randint(NUM_OP_CLASSES, size = N), dtype="category")
criteria = 'information_gain'
Dtree = tree.DecisionTreeClassifier()
Classifier_B = BaggingClassifier(base_estimator=Dtree, n_estimators=n_estimators )
Classifier_B.fit(X, y)
y_hat = Classifier_B.predict(X)
[fig1, fig2] = Classifier_B.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))