Skip to content

Commit 5fb3ec3

Browse files
committed
Merge remote-tracking branch 'refs/remotes/origin/master' into Book
2 parents 2d11bfe + e0d3052 commit 5fb3ec3

File tree

10 files changed

+69
-25
lines changed

10 files changed

+69
-25
lines changed

a_FirstExample/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ Simple regression under **LSE** criterion using `np.polyfit` & `np.polyval`
44
Dataset comes from Coursera's Machine Learning Course
55

66
## Result
7-
![image](http://i2.muimg.com/567571/f0cac9ec46fd2837.png)
7+
![image](http://oph1aen4o.bkt.clouddn.com/18-1-30/89962212.jpg)

b_NaiveBayes/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ Naive Bayes algorithms implemented with
88
## Visualization
99
+ Dataset comes from UCI: [Bank Marketing dataset](http://archive.ics.uci.edu/ml/datasets/Bank+Marketing)
1010

11-
![Discrete Features](http://i4.buimg.com/567571/79ea5cb079b16a72.png)
11+
![Categorical Features](http://oph1aen4o.bkt.clouddn.com/18-1-30/53941771.jpg)
1212

13-
![Continuous Features](http://i2.muimg.com/567571/f267b1a607e4c95e.png)
13+
![Numerical Features](http://oph1aen4o.bkt.clouddn.com/18-1-30/53966960.jpg)
1414

1515
## Example
1616
```python
@@ -24,4 +24,4 @@ nb.evaluate(x, y) # Print out accuracy
2424
nb.visualize2d(x, y) # Visualize result (2d)
2525
nb.visualize() # Visualize distribution
2626
nb.show_timing_log() # Show timing log
27-
```
27+
```

c_CvDTree/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ Implemented **ID3**, **C4.5** & **CART**. Capable for dealing with relatively 'r
55
+ Dataset comes from UCI: [Mushroom dataset](http://archive.ics.uci.edu/ml/datasets/Mushroom)
66

77
### ID3
8-
![ID3](http://i1.piimg.com/567571/b202b2dfd1394757.png)
8+
![ID3](http://oph1aen4o.bkt.clouddn.com/18-1-30/99814955.jpg)
99

1010
### C4.5
11-
![C4.5](http://i1.piimg.com/567571/d64bffa200033d00.png)
11+
![C4.5](http://oph1aen4o.bkt.clouddn.com/18-1-30/48437221.jpg)
1212

1313
### CART
14-
![CART](http://i1.piimg.com/567571/330a93ad355c0a05.png)
14+
![CART](http://oph1aen4o.bkt.clouddn.com/18-1-30/32419745.jpg)
1515

1616
## Example
1717
```python
@@ -26,4 +26,4 @@ tree.evaluate(x, y) # Print out accuracy
2626
tree.visualize2d(x, y) # Visualize result (2d)
2727
tree.visualize() # Visualize Cart Tree itself
2828
tree.show_timing_log() # Show timing log
29-
```
29+
```

c_CvDTree/TestTree.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,6 @@ def main(visualize=True):
110110

111111
tree.show_timing_log()
112112

113+
113114
if __name__ == '__main__':
114115
main(False)

c_CvDTree/Tree.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -188,20 +188,24 @@ def view(self):
188188
self.root.view()
189189

190190
@CvDBaseTiming.timeit(level=2, prefix="[API] ")
191-
def visualize(self, radius=24, width=1200, height=800, padding=0.2, plot_num=30, title="CvDTree"):
191+
def visualize(self, radius=24, width=1200, height=800,
192+
height_padding_ratio=0.2, width_padding=30, title="CvDTree"):
192193
self._update_layers()
193-
units = [len(layer) for layer in self.layers]
194+
n_units = [len(layer) for layer in self.layers]
194195

195196
img = np.ones((height, width, 3), np.uint8) * 255
196-
axis0_padding = int(height / (len(self.layers) - 1 + 2 * padding)) * padding + plot_num
197-
axis0 = np.linspace(
198-
axis0_padding, height - axis0_padding, len(self.layers), dtype=np.int)
199-
axis1_padding = plot_num
200-
axis1 = [np.linspace(axis1_padding, width - axis1_padding, unit + 2, dtype=np.int)
201-
for unit in units]
202-
axis1 = [axis[1:-1] for axis in axis1]
203-
204-
for i, (y, xs) in enumerate(zip(axis0, axis1)):
197+
height_padding = int(
198+
height / (len(self.layers) - 1 + 2 * height_padding_ratio)
199+
) * height_padding_ratio + width_padding
200+
height_axis = np.linspace(
201+
height_padding, height - height_padding, len(self.layers), dtype=np.int)
202+
width_axis = [
203+
np.linspace(width_padding, width - width_padding, unit + 2, dtype=np.int)
204+
for unit in n_units
205+
]
206+
width_axis = [axis[1:-1] for axis in width_axis]
207+
208+
for i, (y, xs) in enumerate(zip(height_axis, width_axis)):
205209
for j, x in enumerate(xs):
206210
if i == 0:
207211
cv2.circle(img, (x, y), radius, (225, 100, 125), 1)
@@ -216,13 +220,13 @@ def visualize(self, radius=24, width=1200, height=800, padding=0.2, plot_num=30,
216220
color = (0, 255, 0)
217221
cv2.putText(img, text, (x-7*len(text)+2, y+3), cv2.LINE_AA, 0.6, color, 1)
218222

219-
for i, y in enumerate(axis0):
220-
if i == len(axis0) - 1:
223+
for i, y in enumerate(height_axis):
224+
if i == len(height_axis) - 1:
221225
break
222-
for j, x in enumerate(axis1[i]):
223-
new_y = axis0[i + 1]
226+
for j, x in enumerate(width_axis[i]):
227+
new_y = height_axis[i + 1]
224228
dy = new_y - y - 2 * radius
225-
for k, new_x in enumerate(axis1[i + 1]):
229+
for k, new_x in enumerate(width_axis[i + 1]):
226230
dx = new_x - x
227231
length = np.sqrt(dx**2+dy**2)
228232
ratio = 0.5 - min(0.4, 1.2 * 24/length)

d_Ensemble/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ rf.show_timing_log() # Show timing log
1616
```
1717

1818
### Result
19-
![Cart on Spiral](http://i4.buimg.com/567571/fd50b722d988b1dd.png)
19+
![Cart on Spiral](http://oph1aen4o.bkt.clouddn.com/18-1-30/25490957.jpg)

e_SVM/KP.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def _get_grads(self, x_batch, y_batch, y_pred, sample_weight_batch, *args):
5858
]
5959
return np.sum(err[mask])
6060

61+
6162
if __name__ == '__main__':
6263
# xs, ys = DataUtil.gen_two_clusters(center=5, dis=1, scale=2, one_hot=False)
6364
xs, ys = DataUtil.gen_spiral(20, 4, 2, 2, one_hot=False)

e_SVM/Perceptron.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,39 @@ def predict(self, x, get_raw_results=False, **kwargs):
6060
if get_raw_results:
6161
return rs
6262
return np.sign(rs).astype(np.float32)
63+
64+
65+
class Perceptron2(Perceptron):
66+
def fit(self, x, y, sample_weight=None, lr=None, epoch=None, animation_params=None):
67+
if sample_weight is None:
68+
sample_weight = self._params["sample_weight"]
69+
if lr is None:
70+
lr = self._params["lr"]
71+
if epoch is None:
72+
epoch = self._params["epoch"]
73+
*animation_properties, animation_params = self._get_animation_params(animation_params)
74+
75+
x, y = np.atleast_2d(x), np.asarray(y)
76+
if sample_weight is None:
77+
sample_weight = np.ones(len(y))
78+
else:
79+
sample_weight = np.asarray(sample_weight) * len(y)
80+
81+
self._w = np.random.random(x.shape[1])
82+
self._b = 0.
83+
ims = []
84+
bar = ProgressBar(max_value=epoch, name="Perceptron")
85+
for i in range(epoch):
86+
y_pred = self.predict(x, True)
87+
err = -y * y_pred * sample_weight
88+
idx = np.argmax(err)
89+
if err[idx] < 0:
90+
bar.terminate()
91+
break
92+
w_norm = np.linalg.norm(self._w)
93+
delta = lr * y[idx] * sample_weight[idx] / w_norm
94+
self._w += delta * (x[idx] - y_pred[idx] * self._w / w_norm ** 2)
95+
self._b += delta
96+
self._handle_animation(i, x, y, ims, animation_params, *animation_properties)
97+
bar.update()
98+
self._handle_mp4(ims, animation_properties)

e_SVM/TestLinear.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,6 @@ def main():
4343

4444
perceptron.show_timing_log()
4545

46+
4647
if __name__ == '__main__':
4748
main()

e_SVM/TestSVM.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,6 @@ def main():
110110

111111
svm.show_timing_log()
112112

113+
113114
if __name__ == '__main__':
114115
main()

0 commit comments

Comments
 (0)