-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex4_tools.py
executable file
·168 lines (144 loc) · 5.67 KB
/
ex4_tools.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"""
===================================================
Introduction to Machine Learning (67577)
===================================================
This module provides some useful tools for Ex4.
NOTE: To use the function view_dtree you need to install graphviz.
See https://pypi.python.org/pypi/graphviz for more details.
Author: Noga Zaslavsky
"""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
# from graphviz import Digraph # TODO - comment out if you didn't install graphviz
class DecisionStump(object):
"""
Decision stump classifier
"""
def __init__(self, D, X, y):
self.theta = 0
self.j = 0
self.s = 0
self.train(D, X, y)
def train(self,D, X, y):
"""
Train the classifier over the sample (X,y) w.r.t. the weights D over X
Parameters
----------
D : weights over the sample
X, y: sample
"""
m, d = X.shape
F, J, theta = [0]*2, [0]*2, [0]*2
for b in [0,1]:
s = 2*b - 1
F[b], theta[b], J[b] = D[y==s].sum(), X[:,0].min()-1, 0
for j in range(d): # go over all features
ind = np.argsort(X[:, j])
Xj = np.sort(X[:, j]) # sort by coordinate j
Xj = np.hstack([Xj,Xj.max()+1])
f = D[y==s].sum()
for i in range(m): # check thresholds over Xj for improvement
f -= s*y[ind[i]]*D[ind[i]]
if f < F[b] and Xj[i] != Xj[i+1]:
F[b], J[b], theta[b] = f, j, (Xj[i]+Xj[i+1])/2
b = np.argmin(F)
self.theta, self.j, self.s = theta[b], J[b], 2*b-1
def predict(self, X):
"""
Returns
-------
y_hat : a prediction vector for X
"""
y_hat = self.s*np.sign(self.theta - X[:,self.j])
y_hat[y_hat==0] = 1
return y_hat
class h_opt(object):
"""
The optimal classifier for the synthetic data provided in ex4
"""
@staticmethod
def predict(X):
def b(X,c,r2):
z = X-c
return np.sign(r2-(z*z).sum(axis=1))
return np.sign(b(X,np.array([-.5,0]),.2)+b(X,np.array([0.45,0.5]),.4)+1)
def decision_boundaries(classifier, X, y, title_str='', weights=None):
"""
Plot the decision boundaries of a binary classfiers over X \subseteq R^2
Parameters
----------
classifier : a binary classifier, implements classifier.predict(X)
X : m*2 matrix whose rows correspond to the data points
y : m dimensional vector of binary labels
title_str : optional title
weights : weights for plotting X
"""
cm = ListedColormap(['#AAAAFF','#FFAAAA'])
cm_bright = ListedColormap(['#0000FF','#FF0000'])
h = .01 # step size in the mesh
# Plot the decision boundary.
x_min, x_max = X[:, 0].min() - .2, X[:, 0].max() + .2
y_min, y_max = X[:, 1].min() - .2, X[:, 1].max() + .2
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
Z = classifier.predict(np.c_[xx.ravel(), yy.ravel()])
# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.pcolormesh(xx, yy, Z, cmap=cm)
# Plot also the training points
if weights is not None: plt.scatter(X[:, 0], X[:, 1], c=y, s=weights, cmap=cm_bright)
else: plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cm_bright)
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.xticks([])
plt.yticks([])
plt.title(title_str)
plt.show()
def view_dtree(dtree, feature_names = None, class_names=None, filename='dtree'):
"""
Cerate a graphical view of a decision tree.
For this function to work well, you need to set correctly the attributes of each node in the tree.
Parameters
----------
dtree : DecisionTree object that follows the guidlines in the skeleton 'decision_tree.py'
feature_names : By default the feature names are 'X[j]'. You may give a list of strings, with the same size
as the dimension of X.
class_names : By default the class names are the labels (e.g. 0/1). You may give a list of strings with
custom class names.
filename : name of the PDF file
"""
if dtree.root is not None:
def shape(node):
if node.leaf:
return 'oval'
else:
return 'box'
def node_to_str(node):
if node.leaf:
if class_names is not None:
return 'label = '+class_names[node.label] +'\nsamples = %d'%node.samples
return 'label = %d'%node.label +'\nsamples %d'%node.samples
else:
if feature_names is not None:
feature_str = feature_names[node.feature]
else:
feature_str = 'X[%d'%node.feature + ']'
return feature_str +' < %0.2f'%node.theta + '?\ninfo-gain = %0.2f'\
%node.gain +'\nsamples = %d'%+node.samples
def build_dot(dot,node,path):
if node.leaf:
return
else:
left_path = path+'0'
right_path = path+'1'
dot.node(left_path,node_to_str(node.left),shape=shape(node.left))
dot.node(right_path,node_to_str(node.right),shape=shape(node.right))
dot.edge(path, left_path)
dot.edge(path, right_path)
build_dot(dot,node.left,left_path)
build_dot(dot,node.right,right_path)
dot = Digraph(filename)
if dtree.root is not None:
dot.node('0', node_to_str(dtree.root),shape=shape(dtree.root))
build_dot(dot,dtree.root,'0')
dot.view()