Skip to content

Commit 22c3431

Browse files
authored
update parsing scripts (#43)
1 parent 8c54875 commit 22c3431

30 files changed

Lines changed: 139 additions & 252 deletions

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,17 @@ addons:
2222
install:
2323
- sudo apt-get update
2424
- source .travis/install_travis_env.sh
25+
26+
before_script: # configure a headless display to test plot generation
27+
- "export DISPLAY=:99.0"
28+
- "sh -e /etc/init.d/xvfb start"
29+
- sleep 3 # give xvfb some time to start
30+
2531
script:
2632
- bash .travis/run_travis_tests.sh
2733
#after_success:
2834
# - if [[ "${COVERAGE}" == "true" ]]; then coveralls; codecov; else echo "failed"; fi
35+
2936
notifications:
3037
email:
3138
recipients:

code/.convert_notebook_to_script.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import argparse
88
import os
99
import subprocess
10-
import textwrap
1110

1211

1312
def convert(input_path, output_path):
@@ -26,15 +25,23 @@ def cleanup(path):
2625

2726
clean_content = []
2827
imports = []
28+
existing_imports = set()
2929
with open(path, 'r') as f:
3030
next(f)
3131
next(f)
3232
for line in f:
33+
line = line.rstrip(' ')
3334
if line.startswith(skip_lines_startwith):
3435
continue
35-
if line.startswith('import') or (
36-
'from' in line and 'import' in line):
37-
imports.append(line)
36+
if line.startswith('import ') or (
37+
'from ' in line and 'import ' in line):
38+
if 'from __future__ import print_function' in line:
39+
if line != imports[0]:
40+
imports.insert(0, line)
41+
else:
42+
if line.strip() not in existing_imports:
43+
imports.append(line)
44+
existing_imports.add(line.strip())
3845
else:
3946
clean_content.append(line)
4047

code/ch02/ch02.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1363,7 +1363,7 @@
13631363
},
13641364
{
13651365
"cell_type": "code",
1366-
"execution_count": 6,
1366+
"execution_count": 1,
13671367
"metadata": {},
13681368
"outputs": [
13691369
{

code/ch02/ch02.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import numpy as np
55
import pandas as pd
66
import matplotlib.pyplot as plt
7-
import numpy as np
87
from matplotlib.colors import ListedColormap
98

109
# *Python Machine Learning 2nd Edition* by [Sebastian Raschka](https://sebastianraschka.com), Packt Publishing Ltd. 2017

code/ch03/ch03.ipynb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1743,7 +1743,7 @@
17431743
},
17441744
{
17451745
"cell_type": "code",
1746-
"execution_count": 1,
1746+
"execution_count": 4,
17471747
"metadata": {},
17481748
"outputs": [
17491749
{
@@ -1758,6 +1758,15 @@
17581758
"source": [
17591759
"! python ../.convert_notebook_to_script.py --input ch03.ipynb --output ch03.py"
17601760
]
1761+
},
1762+
{
1763+
"cell_type": "code",
1764+
"execution_count": null,
1765+
"metadata": {
1766+
"collapsed": true
1767+
},
1768+
"outputs": [],
1769+
"source": []
17611770
}
17621771
],
17631772
"metadata": {

code/ch03/ch03.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,9 @@
1111
from sklearn.metrics import accuracy_score
1212
from matplotlib.colors import ListedColormap
1313
import matplotlib.pyplot as plt
14-
import matplotlib.pyplot as plt
15-
import numpy as np
1614
from sklearn.linear_model import LogisticRegression
1715
from sklearn.svm import SVC
1816
from sklearn.linear_model import SGDClassifier
19-
import matplotlib.pyplot as plt
20-
import numpy as np
21-
from sklearn.svm import SVC
22-
import matplotlib.pyplot as plt
23-
import numpy as np
2417
from sklearn.tree import DecisionTreeClassifier
2518
from pydotplus import graph_from_dot_data
2619
from sklearn.tree import export_graphviz

code/ch03/tree.png

141 KB
Loading

code/ch04/ch04.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2522,7 +2522,7 @@
25222522
},
25232523
{
25242524
"cell_type": "code",
2525-
"execution_count": 1,
2525+
"execution_count": 2,
25262526
"metadata": {},
25272527
"outputs": [
25282528
{

code/ch04/ch04.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,19 @@
55
from io import StringIO
66
import sys
77
from sklearn.preprocessing import Imputer
8-
import pandas as pd
98
import numpy as np
109
from sklearn.preprocessing import LabelEncoder
1110
from sklearn.preprocessing import OneHotEncoder
1211
from sklearn.model_selection import train_test_split
1312
from sklearn.preprocessing import MinMaxScaler
1413
from sklearn.preprocessing import StandardScaler
1514
from sklearn.linear_model import LogisticRegression
16-
from sklearn.linear_model import LogisticRegression
1715
import matplotlib.pyplot as plt
1816
from sklearn.base import clone
1917
from itertools import combinations
20-
import numpy as np
2118
from sklearn.metrics import accuracy_score
22-
from sklearn.model_selection import train_test_split
23-
import matplotlib.pyplot as plt
2419
from sklearn.neighbors import KNeighborsClassifier
2520
from sklearn.ensemble import RandomForestClassifier
26-
importances = forest.feature_importances_
2721
from sklearn.feature_selection import SelectFromModel
2822

2923
# *Python Machine Learning 2nd Edition* by [Sebastian Raschka](https://sebastianraschka.com), Packt Publishing Ltd. 2017
@@ -592,6 +586,7 @@ def _calc_score(self, X_train, y_train, X_test, y_test, indices):
592586
random_state=1)
593587

594588
forest.fit(X_train, y_train)
589+
importances = forest.feature_importances_
595590

596591
indices = np.argsort(importances)[::-1]
597592

code/ch05/ch05.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2125,7 +2125,7 @@
21252125
"output_type": "stream",
21262126
"text": [
21272127
"[NbConvertApp] Converting notebook ch05.ipynb to script\n",
2128-
"[NbConvertApp] Writing 27719 bytes to ch05.py\n"
2128+
"[NbConvertApp] Writing 27705 bytes to ch05.py\n"
21292129
]
21302130
}
21312131
],

0 commit comments

Comments
 (0)