Skip to content

Commit 20a56c9

Browse files
authored
Merge pull request #48 from dspyrhsu/master
Show effects and no deprecation
2 parents 3f5c7bf + 6a05f74 commit 20a56c9

File tree

2 files changed

+30
-70
lines changed

2 files changed

+30
-70
lines changed

notebooks/chapter08_ml/01_scikit.ipynb

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131
{
3232
"cell_type": "code",
3333
"execution_count": null,
34-
"metadata": {
35-
"collapsed": false
36-
},
34+
"metadata": {},
3735
"outputs": [],
3836
"source": [
3937
"import numpy as np\n",
@@ -53,9 +51,7 @@
5351
{
5452
"cell_type": "code",
5553
"execution_count": null,
56-
"metadata": {
57-
"collapsed": false
58-
},
54+
"metadata": {},
5955
"outputs": [],
6056
"source": [
6157
"f = lambda x: np.exp(3 * x)"
@@ -71,9 +67,7 @@
7167
{
7268
"cell_type": "code",
7369
"execution_count": null,
74-
"metadata": {
75-
"collapsed": false
76-
},
70+
"metadata": {},
7771
"outputs": [],
7872
"source": [
7973
"x_tr = np.linspace(0., 2, 200)\n",
@@ -84,19 +78,18 @@
8478
"cell_type": "markdown",
8579
"metadata": {},
8680
"source": [
87-
"4. Now, let's generate our data points within $[0, 1]$. We use the function $f$ and we add some Gaussian noise."
81+
"4. Now, let's generate our data points within $[0, 1]$. We use the function $f$ and we add some Gaussian noise. In order to be able to demonstrate some effects, we use one specific set of data points generated in this fashion."
8882
]
8983
},
9084
{
9185
"cell_type": "code",
9286
"execution_count": null,
93-
"metadata": {
94-
"collapsed": false
95-
},
87+
"metadata": {},
9688
"outputs": [],
9789
"source": [
9890
"x = np.array([0, .1, .2, .5, .8, .9, 1])\n",
99-
"y = f(x) + np.random.randn(len(x))"
91+
"# y = f(x) + np.random.randn(len(x))\n",
92+
"y = np.array([0.59837698, 2.90450025, 4.73684354, 3.87158063, 11.77734608, 15.51112358, 20.08663964])"
10093
]
10194
},
10295
{
@@ -109,9 +102,7 @@
109102
{
110103
"cell_type": "code",
111104
"execution_count": null,
112-
"metadata": {
113-
"collapsed": false
114-
},
105+
"metadata": {},
115106
"outputs": [],
116107
"source": [
117108
"plt.figure(figsize=(6,3));\n",
@@ -129,9 +120,7 @@
129120
{
130121
"cell_type": "code",
131122
"execution_count": null,
132-
"metadata": {
133-
"collapsed": false
134-
},
123+
"metadata": {},
135124
"outputs": [],
136125
"source": [
137126
"# We create the model.\n",
@@ -161,9 +150,7 @@
161150
{
162151
"cell_type": "code",
163152
"execution_count": null,
164-
"metadata": {
165-
"collapsed": false
166-
},
153+
"metadata": {},
167154
"outputs": [],
168155
"source": [
169156
"plt.figure(figsize=(6,3));\n",
@@ -185,9 +172,7 @@
185172
{
186173
"cell_type": "code",
187174
"execution_count": null,
188-
"metadata": {
189-
"collapsed": false
190-
},
175+
"metadata": {},
191176
"outputs": [],
192177
"source": [
193178
"lrp = lm.LinearRegression()\n",
@@ -231,9 +216,7 @@
231216
{
232217
"cell_type": "code",
233218
"execution_count": null,
234-
"metadata": {
235-
"collapsed": false
236-
},
219+
"metadata": {},
237220
"outputs": [],
238221
"source": [
239222
"ridge = lm.RidgeCV()\n",
@@ -287,9 +270,9 @@
287270
"name": "python",
288271
"nbconvert_exporter": "python",
289272
"pygments_lexer": "ipython3",
290-
"version": "3.4.2"
273+
"version": "3.6.0"
291274
}
292275
},
293276
"nbformat": 4,
294-
"nbformat_minor": 0
277+
"nbformat_minor": 1
295278
}

notebooks/chapter08_ml/04_text.ipynb

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,13 @@
4040
{
4141
"cell_type": "code",
4242
"execution_count": null,
43-
"metadata": {
44-
"collapsed": false
45-
},
43+
"metadata": {},
4644
"outputs": [],
4745
"source": [
4846
"import numpy as np\n",
4947
"import pandas as pd\n",
5048
"import sklearn\n",
51-
"import sklearn.cross_validation as cv\n",
52-
"import sklearn.grid_search as gs\n",
49+
"import sklearn.model_selection as ms\n",
5350
"import sklearn.feature_extraction.text as text\n",
5451
"import sklearn.naive_bayes as nb\n",
5552
"import matplotlib.pyplot as plt\n",
@@ -66,9 +63,7 @@
6663
{
6764
"cell_type": "code",
6865
"execution_count": null,
69-
"metadata": {
70-
"collapsed": false
71-
},
66+
"metadata": {},
7267
"outputs": [],
7368
"source": [
7469
"df = pd.read_csv(\"data/troll.csv\")"
@@ -84,9 +79,7 @@
8479
{
8580
"cell_type": "code",
8681
"execution_count": null,
87-
"metadata": {
88-
"collapsed": false
89-
},
82+
"metadata": {},
9083
"outputs": [],
9184
"source": [
9285
"df[['Insult', 'Comment']].tail()"
@@ -102,9 +95,7 @@
10295
{
10396
"cell_type": "code",
10497
"execution_count": null,
105-
"metadata": {
106-
"collapsed": false
107-
},
98+
"metadata": {},
10899
"outputs": [],
109100
"source": [
110101
"y = df['Insult']"
@@ -120,9 +111,7 @@
120111
{
121112
"cell_type": "code",
122113
"execution_count": null,
123-
"metadata": {
124-
"collapsed": false
125-
},
114+
"metadata": {},
126115
"outputs": [],
127116
"source": [
128117
"tf = text.TfidfVectorizer()\n",
@@ -140,9 +129,7 @@
140129
{
141130
"cell_type": "code",
142131
"execution_count": null,
143-
"metadata": {
144-
"collapsed": false
145-
},
132+
"metadata": {},
146133
"outputs": [],
147134
"source": [
148135
"print(\"Each sample has ~{0:.2f}% non-zero features.\".format(\n",
@@ -159,13 +146,11 @@
159146
{
160147
"cell_type": "code",
161148
"execution_count": null,
162-
"metadata": {
163-
"collapsed": false
164-
},
149+
"metadata": {},
165150
"outputs": [],
166151
"source": [
167152
"(X_train, X_test,\n",
168-
" y_train, y_test) = cv.train_test_split(X, y,\n",
153+
" y_train, y_test) = ms.train_test_split(X, y,\n",
169154
" test_size=.2)"
170155
]
171156
},
@@ -179,12 +164,10 @@
179164
{
180165
"cell_type": "code",
181166
"execution_count": null,
182-
"metadata": {
183-
"collapsed": false
184-
},
167+
"metadata": {},
185168
"outputs": [],
186169
"source": [
187-
"bnb = gs.GridSearchCV(nb.BernoulliNB(), param_grid={'alpha':np.logspace(-2., 2., 50)})\n",
170+
"bnb = ms.GridSearchCV(nb.BernoulliNB(), param_grid={'alpha':np.logspace(-2., 2., 50)})\n",
188171
"bnb.fit(X_train, y_train);"
189172
]
190173
},
@@ -198,9 +181,7 @@
198181
{
199182
"cell_type": "code",
200183
"execution_count": null,
201-
"metadata": {
202-
"collapsed": false
203-
},
184+
"metadata": {},
204185
"outputs": [],
205186
"source": [
206187
"bnb.score(X_test, y_test)"
@@ -216,9 +197,7 @@
216197
{
217198
"cell_type": "code",
218199
"execution_count": null,
219-
"metadata": {
220-
"collapsed": false
221-
},
200+
"metadata": {},
222201
"outputs": [],
223202
"source": [
224203
"# We first get the words corresponding to each feature.\n",
@@ -239,9 +218,7 @@
239218
{
240219
"cell_type": "code",
241220
"execution_count": null,
242-
"metadata": {
243-
"collapsed": false
244-
},
221+
"metadata": {},
245222
"outputs": [],
246223
"source": [
247224
"print(bnb.predict(tf.transform([\n",
@@ -277,9 +254,9 @@
277254
"name": "python",
278255
"nbconvert_exporter": "python",
279256
"pygments_lexer": "ipython3",
280-
"version": "3.4.2"
257+
"version": "3.6.0"
281258
}
282259
},
283260
"nbformat": 4,
284-
"nbformat_minor": 0
261+
"nbformat_minor": 1
285262
}

0 commit comments

Comments
 (0)