Skip to content

Commit 862968a

Browse files
Merge pull request CamDavidsonPilon#181 from eli-b/chap7-style
autopep8 --ignore=E501,E703 fixes. Marginal improvement.
2 parents a7c6487 + c9636cf commit 862968a

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

Chapter7_BayesianMachineLearning/DontOverfit.ipynb

Lines changed: 31 additions & 33 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": {},
@@ -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": {},
@@ -234,14 +232,14 @@
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",
247245
"print \"testing: \", testing_data.shape, testing_labels.shape"
@@ -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,8 +345,8 @@
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",
348+
"def Z(coef=coef, to_include=to_include, data=training_data):\n",
349+
" ym = np.dot(to_include * training_data, coef)\n",
352350
" return ym - ym.mean()"
353351
],
354352
"language": "python",
@@ -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,10 +371,10 @@
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",
376+
"model = pm.Model([to_include, coef, Z, T, obs])\n",
377+
"map_ = pm.MAP(model)\n",
380378
"map_.fit()"
381379
],
382380
"language": "python",
@@ -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,7 +482,7 @@
484482
"cell_type": "code",
485483
"collapsed": false,
486484
"input": [
487-
"imshow(t_trace[-10000:,:], aspect=\"auto\")\n",
485+
"imshow(t_trace[-10000:, :], aspect=\"auto\")\n",
488486
"colorbar()"
489487
],
490488
"language": "python",
@@ -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": {},
@@ -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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
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",

0 commit comments

Comments
 (0)