Skip to content

Commit 4a5f21c

Browse files
committed
Merge branch 'chap7-style'
2 parents 7a2318d + 3efa40c commit 4a5f21c

File tree

2 files changed

+46
-46
lines changed

2 files changed

+46
-46
lines changed

Chapter7_BayesianMachineLearning/DontOverfit.ipynb

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@
3333
"url = \"https://dl.dropbox.com/s/lnly9gw8pb1xhir/overfitting.zip\"\n",
3434
"\n",
3535
"\n",
36-
"results = requests.get(url)\n",
37-
"\n"
36+
"results = requests.get(url);"
3837
],
3938
"language": "python",
4039
"metadata": {},
@@ -47,8 +46,7 @@
4746
"input": [
4847
"import StringIO\n",
4948
"z = zipfile.ZipFile(StringIO.StringIO(results.content))\n",
50-
"#z.extractall()\n",
51-
"\n"
49+
"# z.extractall();;"
5250
],
5351
"language": "python",
5452
"metadata": {},
@@ -59,7 +57,7 @@
5957
"cell_type": "code",
6058
"collapsed": false,
6159
"input": [
62-
"z.extractall()"
60+
"z.extractall();"
6361
],
6462
"language": "python",
6563
"metadata": {},
@@ -70,7 +68,7 @@
7068
"cell_type": "code",
7169
"collapsed": false,
7270
"input": [
73-
"z.namelist()"
71+
"z.namelist();"
7472
],
7573
"language": "python",
7674
"metadata": {},
@@ -90,7 +88,7 @@
9088
"collapsed": false,
9189
"input": [
9290
"d = z.open('overfitting.csv')\n",
93-
"d.readline()"
91+
"d.readline();"
9492
],
9593
"language": "python",
9694
"metadata": {},
@@ -109,7 +107,7 @@
109107
"cell_type": "code",
110108
"collapsed": false,
111109
"input": [
112-
"import numpy as np"
110+
"import numpy as np;"
113111
],
114112
"language": "python",
115113
"metadata": {},
@@ -120,7 +118,7 @@
120118
"cell_type": "code",
121119
"collapsed": false,
122120
"input": [
123-
"M = np.fromstring(d.read(), sep=\",\" )"
121+
"M = np.fromstring(d.read(), sep=\",\");"
124122
],
125123
"language": "python",
126124
"metadata": {},
@@ -131,7 +129,7 @@
131129
"cell_type": "code",
132130
"collapsed": false,
133131
"input": [
134-
"len(d.read())"
132+
"len(d.read());"
135133
],
136134
"language": "python",
137135
"metadata": {},
@@ -169,7 +167,7 @@
169167
"cell_type": "code",
170168
"collapsed": false,
171169
"input": [
172-
"data = np.loadtxt(\"overfitting.csv\", delimiter=\",\", skiprows=1)"
170+
"data = np.loadtxt(\"overfitting.csv\", delimiter=\",\", skiprows=1);"
173171
],
174172
"language": "python",
175173
"metadata": {},
@@ -195,7 +193,7 @@
195193
"\n",
196194
"\"\"\"\n",
197195
"\n",
198-
"data.shape"
196+
"data.shape;"
199197
],
200198
"language": "python",
201199
"metadata": {},
@@ -234,17 +232,17 @@
234232
"cell_type": "code",
235233
"collapsed": false,
236234
"input": [
237-
"ix_training = data[:,1] == 1\n",
238-
"ix_testing = data[:,1] == 0\n",
235+
"ix_training = data[:, 1] == 1\n",
236+
"ix_testing = data[:, 1] == 0\n",
239237
"\n",
240-
"training_data = data[ ix_training, 5: ]\n",
241-
"testing_data = data[ ix_testing, 5: ]\n",
238+
"training_data = data[ix_training, 5:]\n",
239+
"testing_data = data[ix_testing, 5:]\n",
242240
"\n",
243-
"training_labels = data[ ix_training, 2]\n",
244-
"testing_labels = data[ ix_testing, 2]\n",
241+
"training_labels = data[ix_training, 2]\n",
242+
"testing_labels = data[ix_testing, 2]\n",
245243
"\n",
246244
"print \"training:\", training_data.shape, training_labels.shape\n",
247-
"print \"testing: \", testing_data.shape, testing_labels.shape"
245+
"print \"testing: \", testing_data.shape, testing_labels.shape;"
248246
],
249247
"language": "python",
250248
"metadata": {},
@@ -280,7 +278,7 @@
280278
"cell_type": "code",
281279
"collapsed": false,
282280
"input": [
283-
"figsize( 12, 4 )"
281+
"figsize(12, 4);"
284282
],
285283
"language": "python",
286284
"metadata": {},
@@ -291,8 +289,8 @@
291289
"cell_type": "code",
292290
"collapsed": false,
293291
"input": [
294-
"hist( training_data.flatten() )\n",
295-
"print training_data.shape[0]*training_data.shape[1]"
292+
"hist(training_data.flatten())\n",
293+
"print training_data.shape[0] * training_data.shape[1];"
296294
],
297295
"language": "python",
298296
"metadata": {},
@@ -324,7 +322,7 @@
324322
"input": [
325323
"import pymc as pm\n",
326324
"\n",
327-
"to_include = pm.Bernoulli( \"to_include\", 0.5, size= 200 )"
325+
"to_include = pm.Bernoulli(\"to_include\", 0.5, size=200);"
328326
],
329327
"language": "python",
330328
"metadata": {},
@@ -335,7 +333,7 @@
335333
"cell_type": "code",
336334
"collapsed": false,
337335
"input": [
338-
"coef = pm.Uniform( \"coefs\", 0, 1, size = 200 )"
336+
"coef = pm.Uniform(\"coefs\", 0, 1, size=200);"
339337
],
340338
"language": "python",
341339
"metadata": {},
@@ -347,9 +345,9 @@
347345
"collapsed": false,
348346
"input": [
349347
"@pm.deterministic\n",
350-
"def Z( coef = coef, to_include = to_include, data = training_data ):\n",
351-
" ym = np.dot( to_include*training_data, coef )\n",
352-
" return ym - ym.mean()"
348+
"def Z(coef=coef, to_include=to_include, data=training_data):\n",
349+
" ym = np.dot(to_include * training_data, coef)\n",
350+
" return ym - ym.mean();"
353351
],
354352
"language": "python",
355353
"metadata": {},
@@ -361,8 +359,8 @@
361359
"collapsed": false,
362360
"input": [
363361
"@pm.deterministic\n",
364-
"def T( z = Z ):\n",
365-
" return 0.45*(np.sign(z) + 1.1)"
362+
"def T(z=Z):\n",
363+
" return 0.45 * (np.sign(z) + 1.1);"
366364
],
367365
"language": "python",
368366
"metadata": {},
@@ -373,11 +371,11 @@
373371
"cell_type": "code",
374372
"collapsed": false,
375373
"input": [
376-
"obs = pm.Bernoulli( \"obs\", T, value = training_labels, observed = True)\n",
374+
"obs = pm.Bernoulli(\"obs\", T, value=training_labels, observed=True)\n",
377375
"\n",
378-
"model = pm.Model( [to_include, coef, Z, T, obs] )\n",
379-
"map_ = pm.MAP( model )\n",
380-
"map_.fit()"
376+
"model = pm.Model([to_include, coef, Z, T, obs])\n",
377+
"map_ = pm.MAP(model)\n",
378+
"map_.fit();"
381379
],
382380
"language": "python",
383381
"metadata": {},
@@ -396,7 +394,7 @@
396394
"cell_type": "code",
397395
"collapsed": false,
398396
"input": [
399-
"mcmc = pm.MCMC( model )"
397+
"mcmc = pm.MCMC(model);"
400398
],
401399
"language": "python",
402400
"metadata": {},
@@ -407,7 +405,7 @@
407405
"cell_type": "code",
408406
"collapsed": false,
409407
"input": [
410-
"mcmc.sample(100000, 90000,1) "
408+
"mcmc.sample(100000, 90000, 1);"
411409
],
412410
"language": "python",
413411
"metadata": {},
@@ -434,7 +432,7 @@
434432
"cell_type": "code",
435433
"collapsed": false,
436434
"input": [
437-
"(np.round(T.value) == training_labels ).mean()"
435+
"(np.round(T.value) == training_labels).mean();"
438436
],
439437
"language": "python",
440438
"metadata": {},
@@ -454,7 +452,7 @@
454452
"collapsed": false,
455453
"input": [
456454
"t_trace = mcmc.trace(\"T\")[:]\n",
457-
"(np.round( t_trace[-500:-400,:]).mean(axis=0) == training_labels ).mean()"
455+
"(np.round(t_trace[-500:-400, :]).mean(axis=0) == training_labels).mean();"
458456
],
459457
"language": "python",
460458
"metadata": {},
@@ -473,7 +471,7 @@
473471
"cell_type": "code",
474472
"collapsed": false,
475473
"input": [
476-
"t_mean = np.round( t_trace).mean(axis=1)\n"
474+
"t_mean = np.round(t_trace).mean(axis=1);"
477475
],
478476
"language": "python",
479477
"metadata": {},
@@ -484,8 +482,8 @@
484482
"cell_type": "code",
485483
"collapsed": false,
486484
"input": [
487-
"imshow(t_trace[-10000:,:], aspect=\"auto\")\n",
488-
"colorbar()"
485+
"imshow(t_trace[-10000:, :], aspect=\"auto\")\n",
486+
"colorbar();"
489487
],
490488
"language": "python",
491489
"metadata": {},
@@ -508,9 +506,9 @@
508506
"cell_type": "code",
509507
"collapsed": false,
510508
"input": [
511-
"figsize( 23, 8)\n",
509+
"figsize(23, 8)\n",
512510
"coef_trace = mcmc.trace(\"coefs\")[:]\n",
513-
"imshow(coef_trace[-10000:,:], aspect=\"auto\", cmap=pyplot.cm.RdBu, interpolation=\"none\")"
511+
"imshow(coef_trace[-10000:, :], aspect=\"auto\", cmap=pyplot.cm.RdBu, interpolation=\"none\");"
514512
],
515513
"language": "python",
516514
"metadata": {},
@@ -533,7 +531,7 @@
533531
"cell_type": "code",
534532
"collapsed": false,
535533
"input": [
536-
"include_trace = mcmc.trace(\"to_include\")[:]"
534+
"include_trace = mcmc.trace(\"to_include\")[:];"
537535
],
538536
"language": "python",
539537
"metadata": {},
@@ -544,8 +542,8 @@
544542
"cell_type": "code",
545543
"collapsed": false,
546544
"input": [
547-
"figsize( 23, 8)\n",
548-
"imshow(include_trace[-10000:,:], aspect=\"auto\", interpolation=\"none\")"
545+
"figsize(23, 8)\n",
546+
"imshow(include_trace[-10000:, :], aspect=\"auto\", interpolation=\"none\");"
549547
],
550548
"language": "python",
551549
"metadata": {},

Chapter7_BayesianMachineLearning/MachineLearning.ipynb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,12 @@
6767
"collapsed": false,
6868
"input": [
6969
"from IPython.core.display import HTML\n",
70+
"\n",
71+
"\n",
7072
"def css_styling():\n",
7173
" styles = open(\"../styles/custom.css\", \"r\").read()\n",
7274
" return HTML(styles)\n",
73-
"css_styling()"
75+
"css_styling();"
7476
],
7577
"language": "python",
7678
"metadata": {},

0 commit comments

Comments
 (0)