Skip to content

Commit 002c7a1

Browse files
committed
autopep'ed
1 parent 8fa3f52 commit 002c7a1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+572
-474
lines changed

ch02/extra/create_tsv.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import milksets.iris
22
import milksets.seeds
33

4+
45
def save_as_tsv(fname, module):
56
features, labels = module.load()
67
nlabels = [module.label_names[ell] for ell in labels]
78
with open(fname, 'w') as ofile:
8-
for f,n in zip(features, nlabels):
9-
print >>ofile, "\t".join(map(str,f)+[n])
9+
for f, n in zip(features, nlabels):
10+
print >>ofile, "\t".join(map(str, f) + [n])
1011

1112
save_as_tsv('iris.tsv', milksets.iris)
1213
save_as_tsv('seeds.tsv', milksets.seeds)

ch02/figure1.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
target = data['target']
99

1010

11-
pairs = [(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)]
12-
for i,(p0,p1) in enumerate(pairs):
13-
plt.subplot(2,3,i+1)
14-
for t,marker,c in zip(range(3),">ox","rgb"):
15-
plt.scatter(features[target == t,p0], features[target == t,p1], marker=marker, c=c)
11+
pairs = [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]
12+
for i, (p0, p1) in enumerate(pairs):
13+
plt.subplot(2, 3, i + 1)
14+
for t, marker, c in zip(range(3), ">ox", "rgb"):
15+
plt.scatter(features[target == t, p0], features[
16+
target == t, p1], marker=marker, c=c)
1617
plt.xlabel(feature_names[p0])
1718
plt.ylabel(feature_names[p1])
1819
plt.xticks([])
1920
plt.yticks([])
2021
plt.savefig('../1400_02_01.png')
21-

ch02/figure2.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,28 @@
1313
virginica = species == 'virginica'
1414

1515
t = 1.75
16-
p0,p1 = 3,2
16+
p0, p1 = 3, 2
1717

1818
if COLOUR_FIGURE:
19-
area1c = (1.,.8,.8)
20-
area2c = (.8,.8,1.)
19+
area1c = (1., .8, .8)
20+
area2c = (.8, .8, 1.)
2121
else:
22-
area1c = (1.,1,1)
23-
area2c = (.7,.7,.7)
22+
area1c = (1., 1, 1)
23+
area2c = (.7, .7, .7)
2424

25-
x0,x1 =[features[:,p0].min()*.9,features[:,p0].max()*1.1]
26-
y0,y1 =[features[:,p1].min()*.9,features[:,p1].max()*1.1]
25+
x0, x1 = [features[:, p0].min() * .9, features[:, p0].max() * 1.1]
26+
y0, y1 = [features[:, p1].min() * .9, features[:, p1].max() * 1.1]
2727

28-
plt.fill_between([t,x1],[y0,y0],[y1,y1],color=area2c)
29-
plt.fill_between([x0,t],[y0,y0],[y1,y1],color=area1c)
30-
plt.plot([t,t],[y0,y1],'k--',lw=2)
31-
plt.plot([t-.1,t-.1],[y0,y1],'k:',lw=2)
32-
plt.scatter(features[virginica,p0], features[virginica,p1], c='b', marker='o')
33-
plt.scatter(features[~virginica,p0], features[~virginica,p1], c='r', marker='x')
34-
plt.ylim(y0,y1)
35-
plt.xlim(x0,x1)
28+
plt.fill_between([t, x1], [y0, y0], [y1, y1], color=area2c)
29+
plt.fill_between([x0, t], [y0, y0], [y1, y1], color=area1c)
30+
plt.plot([t, t], [y0, y1], 'k--', lw=2)
31+
plt.plot([t - .1, t - .1], [y0, y1], 'k:', lw=2)
32+
plt.scatter(features[virginica, p0],
33+
features[virginica, p1], c='b', marker='o')
34+
plt.scatter(features[~virginica, p0],
35+
features[~virginica, p1], c='r', marker='x')
36+
plt.ylim(y0, y1)
37+
plt.xlim(x0, x1)
3638
plt.xlabel(feature_names[p0])
3739
plt.ylabel(feature_names[p1])
3840
plt.savefig('../1400_02_02.png')

ch02/figure4_5.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,34 @@
1818

1919

2020
def train_plot(features, labels):
21-
y0,y1 = features[:,2].min()*.9, features[:,2].max()*1.1
22-
x0,x1 = features[:,0].min()*.9, features[:,0].max()*1.1
23-
X = np.linspace(x0,x1,100)
24-
Y = np.linspace(y0,y1,100)
25-
X,Y = np.meshgrid(X,Y)
26-
27-
model = learn_model(1, features[:,(0,2)], np.array(labels))
28-
C = apply_model(np.vstack([X.ravel(),Y.ravel()]).T, model).reshape(X.shape)
21+
y0, y1 = features[:, 2].min() * .9, features[:, 2].max() * 1.1
22+
x0, x1 = features[:, 0].min() * .9, features[:, 0].max() * 1.1
23+
X = np.linspace(x0, x1, 100)
24+
Y = np.linspace(y0, y1, 100)
25+
X, Y = np.meshgrid(X, Y)
26+
27+
model = learn_model(1, features[:, (0, 2)], np.array(labels))
28+
C = apply_model(
29+
np.vstack([X.ravel(), Y.ravel()]).T, model).reshape(X.shape)
2930
if COLOUR_FIGURE:
30-
cmap = ListedColormap([(1.,.6,.6),(.6,1.,.6),(.6,.6,1.)])
31+
cmap = ListedColormap([(1., .6, .6), (.6, 1., .6), (.6, .6, 1.)])
3132
else:
32-
cmap = ListedColormap([(1.,1.,1.),(.2,.2,.2),(.6,.6,.6)])
33-
plt.xlim(x0,x1)
34-
plt.ylim(y0,y1)
33+
cmap = ListedColormap([(1., 1., 1.), (.2, .2, .2), (.6, .6, .6)])
34+
plt.xlim(x0, x1)
35+
plt.ylim(y0, y1)
3536
plt.xlabel(feature_names[0])
3637
plt.ylabel(feature_names[2])
37-
plt.pcolormesh(X,Y,C, cmap=cmap)
38+
plt.pcolormesh(X, Y, C, cmap=cmap)
3839
if COLOUR_FIGURE:
39-
cmap = ListedColormap([(1.,.0,.0),(.0,1.,.0),(.0,.0,1.)])
40-
plt.scatter(features[:,0], features[:,2], c=labels, cmap=cmap)
40+
cmap = ListedColormap([(1., .0, .0), (.0, 1., .0), (.0, .0, 1.)])
41+
plt.scatter(features[:, 0], features[:, 2], c=labels, cmap=cmap)
4142
else:
42-
for lab,ma in zip(range(3), "Do^"):
43-
plt.plot(features[labels == lab,0], features[labels == lab,2], ma, c=(1.,1.,1.))
43+
for lab, ma in zip(range(3), "Do^"):
44+
plt.plot(features[labels == lab, 0], features[
45+
labels == lab, 2], ma, c=(1., 1., 1.))
4446

4547

46-
features,labels = load_dataset('seeds')
48+
features, labels = load_dataset('seeds')
4749
names = sorted(set(labels))
4850
labels = np.array([names.index(ell) for ell in labels])
4951

ch02/heldout.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,3 @@
2424
Training error was {0:.1%}.
2525
Testing error was {1:.1%} (N = {2}).
2626
'''.format(train_error, test_error, testing.sum()))
27-

ch02/knn.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
11
import numpy as np
2+
3+
24
def learn_model(k, features, labels):
3-
return k, features.copy(),labels.copy()
5+
return k, features.copy(), labels.copy()
6+
47

58
def plurality(xs):
69
from collections import defaultdict
710
counts = defaultdict(int)
811
for x in xs:
912
counts[x] += 1
1013
maxv = max(counts.values())
11-
for k,v in counts.items():
14+
for k, v in counts.items():
1215
if v == maxv:
1316
return k
1417

18+
1519
def apply_model(features, model):
1620
k, train_feats, labels = model
1721
results = []
1822
for f in features:
1923
label_dist = []
20-
for t,ell in zip(train_feats, labels):
21-
label_dist.append( (np.linalg.norm(f-t), ell) )
24+
for t, ell in zip(train_feats, labels):
25+
label_dist.append((np.linalg.norm(f - t), ell))
2226
label_dist.sort(key=lambda d_ell: d_ell[0])
2327
label_dist = label_dist[:k]
24-
results.append(plurality([ell for _,ell in label_dist]))
28+
results.append(plurality([ell for _, ell in label_dist]))
2529
return np.array(results)
2630

31+
2732
def accuracy(features, labels, model):
2833
preds = apply_model(features, model)
2934
return np.mean(preds == labels)

ch02/load.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import numpy as np
2+
3+
24
def load_dataset(dataset_name):
35
'''
46
data,labels = load_dataset(dataset_name)
@@ -20,4 +22,3 @@ def load_dataset(dataset_name):
2022
data = np.array(data)
2123
labels = np.array(labels)
2224
return data, labels
23-

ch02/seeds_knn.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import numpy as np
33
from knn import learn_model, apply_model, accuracy
44

5-
features,labels = load_dataset('seeds')
5+
features, labels = load_dataset('seeds')
6+
67

78
def cross_validate(features, labels):
89
error = 0.0
@@ -14,13 +15,13 @@ def cross_validate(features, labels):
1415
test_error = accuracy(features[testing], labels[testing], model)
1516
error += test_error
1617

17-
return error/ 10.0
18+
return error / 10.0
1819

1920
error = cross_validate(features, labels)
2021
print('Ten fold cross-validated error was {0:.1%}.'.format(error))
2122

2223
features -= features.mean(0)
2324
features /= features.std(0)
2425
error = cross_validate(features, labels)
25-
print('Ten fold cross-validated error after z-scoring was {0:.1%}.'.format(error))
26-
26+
print(
27+
'Ten fold cross-validated error after z-scoring was {0:.1%}.'.format(error))

ch02/seeds_threshold.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import numpy as np
33
from threshold import learn_model, apply_model, accuracy
44

5-
features,labels = load_dataset('seeds')
5+
features, labels = load_dataset('seeds')
66
labels = labels == 'Canadian'
77

88
error = 0.0
@@ -17,4 +17,3 @@
1717
error /= 10.0
1818

1919
print('Ten fold cross-validated error was {0:.1%}.'.format(error))
20-

ch02/simple_threshold.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
target_names = data['target_names']
88
labels = target_names[target]
99

10-
plength = features[:,2]
10+
plength = features[:, 2]
1111
is_setosa = (labels == 'setosa')
1212
print('Maximum of setosa: {0}.'.format(plength[is_setosa].max()))
1313
print('Minimum of others: {0}.'.format(plength[~is_setosa].min()))

0 commit comments

Comments
 (0)