-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipython_log.py
498 lines (493 loc) · 20.4 KB
/
ipython_log.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# IPython log file
import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend(['D:\\study\\pydata'])
years = range(1880, 2011) #this will generate sequence from 1880 to 2010
pieces = []
columns = ['name', 'sex', 'births']
for year in years:
path = './pydata-book/ch02/names/yob%d.txt' % year
frame = pd.read_csv(path, names=columns)
frame['year'] = year
pieces.append(frame)
# concatenate everything into a single data frame
names = pd.concat(pieces, ignore_index=True)
# we have to pass ignore_index = True because we're not in interested
# in preserving the original row numbers returned from read_csv
names
total_births = names.pivot_table('births', rows='year', cols='sex', aggfunc=sum)
total_births.tail()
total_births.plot(title='Total Birth by Sex and Year')
#insert a column prop with the fraction of babies given each name relative to the total number
#of births.
#We first group the data by year and sex, then add the new column to each group
def add_prop(group):
births = group.births.astype(float) #we first need to cast integer to floating
group['prop'] = births / births.sum() #births.sum() automatically sum by group
#once the data frame has been grouped
return group
names = names.groupby(['year', 'sex']).apply(add_prop) #great use of apply!!
names
class(names.groupby(['year', 'sex'])) #pandas.core.groupby.DataFrameGroupBy
#!!! when performing a group operation like this, it's often valuable to do a sanity check
#like verifying that the prop column of each group sums to 1
#np.allclose
import numpy as np
np.allclose(names.groupby(['year', 'sex']).prop.sum(), 1)
#extract a subset to facilitate further analysis
#top 10000 names for each sex/year combination
def get_top1000(group):
return group.sort_index(by='births', ascending=False)[:1000]
top1000 = names.groupby(['year', 'sex']).apply(get_top1000)
top1000
#if you prefer DIY approach, you could also do:
pieces = []
for year, group in names.groupby(['year', 'sex']):
pieces.append(group.sort_index(by='births', ascending=False)[:1000])
top1000 = pd.concat(pieces, ignore_index=True)
top1000.tail()
#analyzing naming trends
boys = top1000[top1000.sex == 'M']
girls = top1000[top1000.sex == 'F']
total_births = top1000.pivot_table('births', rows='year', cols='name',aggfunc=sum)
subset = total_births[['John', 'Harry', 'Mary', 'Marilyn']]
subset.plot(subplots=True, figsize=(12, 10), grid=False, title='Number of Births per Year')
#Measuring the increase in naming diversity
prop_table = top1000.pivot_table('prop', rows='year', cols='sex', aggfunc=sum)
prop_table.plot(title='Sum of Table1000.Prop by Year and Sex', yticks=np.linspace(0, 1.2, 13), xticks=range(1880, 2020, 10))
#how many of the most popular names it takes to reach 50%.
df2010 = boys[boys.year == 2010]
df2010
prop_cumsum = df2010.sort_index(by='prop', ascending=False).prop.cumsum()
prop_cumsum[:10]
prop_cumsum.values.searchsorted(0.5) + 1 #searchsorted is a method of numpy.ndarray
#plus 1 is because arrays are zero-indexed
type(prop_cumsum.values)
df1900 = boys[boys.year == 1900]
in1900 = df1900.sort_index(by='prop', ascending=False).prop.cumsum()
in1900.values.searchsorted(0.5) + 1 #so 2010's names are more diversified
def get_quantile_count(group, q=0.5):
group = group.sort_index(by='prop', ascending=False)
return group.prop.cumsum().values.searchsorted(q) + 1
diversity = top1000.groupby(['year', 'sex']).apply(get_quantile_count)
diversity = diversity.unstack('sex') #now diversity has two time series, ne for each sex, indexed by year
diversity.head()
diversity.plot(title='Number of Popular Names in Top 50%')
#The 'Last Letter' Revolution
#exact last letter from name column
get_last_letter = lambda x: x[-1]
last_letters = names.name.map(get_last_letter)
#Difference between map, applymap and apply methods in Pandas
#map is a Series method whereas the rest are DataFrame methods.
#apply is column-or-row-wise. applymap and map are element-wise.
last_letters.name = 'last_letter'
table = names.pivot_table('births', rows=last_letters, cols=['sex', 'year'], aggfunc=sum)
suitable = table.reindex(columns=[1910, 1960, 2010], level='year')
#DataFrame.reindex(index=None, columns=None, **kwargs)
type(suitable)
suitable.head()
suitable.sum() #Return the sum of the values for the requested axis(axis : {row (0), columns (1)})
type(suitable.sum()) #pandas.core.series.Series
letter_prop = subtable/subtable.sum().astype(float)
import matplotlib.pyplot as plt
fig, axes = plt.subplots(2, 1, figsize=(10, 10))
letter_prop['M'].plot(kind='bar', rot=0, ax=axes[0], title='Male')
letter_prop['F'].plot(kind='bar', rot=0, ax=axes[1], title='Female', legend=False)
#boy names ending in 'n' have experienced significant growth since the 1960s
full_letter_prop = table/table.sum().astype(float)
dny_ts = full_letter_prop.ix[['d', 'n', 'y'], 'M'].T
dny_ts.head()
type(dny_ts) #pandas.core.frame.DataFrame
dny_ts.plot()
#boy names that became girl names (and vice versa)
all_names = top1000.name.unique()
mask = np.array(['lesl' in x.lower() for x in all_names])
lesley_like = all_names[mask]
lesley_like
years = range(1880, 2011) #this will generate sequence from 1880 to 2010
pieces = []
columns = ['name', 'sex', 'births']
for year in years:
path = './pydata-book/ch02/names/yob%d.txt' % year
frame = pd.read_csv(path, names=columns)
frame['year'] = year
pieces.append(frame)
# concatenate everything into a single data frame
names = pd.concat(pieces, ignore_index=True)
# we have to pass ignore_index = True because we're not in interested
# in preserving the original row numbers returned from read_csv
names
total_births = names.pivot_table('births', rows='year', cols='sex', aggfunc=sum)
total_births.tail()
total_births.plot(title='Total Birth by Sex and Year')
#insert a column prop with the fraction of babies given each name relative to the total number
#of births.
#We first group the data by year and sex, then add the new column to each group
def add_prop(group):
births = group.births.astype(float) #we first need to cast integer to floating
group['prop'] = births / births.sum() #births.sum() automatically sum by group
#once the data frame has been grouped
return group
names = names.groupby(['year', 'sex']).apply(add_prop) #great use of apply!!
names
class(names.groupby(['year', 'sex'])) #pandas.core.groupby.DataFrameGroupBy
#!!! when performing a group operation like this, it's often valuable to do a sanity check
#like verifying that the prop column of each group sums to 1
#np.allclose
import numpy as np
np.allclose(names.groupby(['year', 'sex']).prop.sum(), 1)
#extract a subset to facilitate further analysis
#top 10000 names for each sex/year combination
def get_top1000(group):
return group.sort_index(by='births', ascending=False)[:1000]
top1000 = names.groupby(['year', 'sex']).apply(get_top1000)
top1000
#if you prefer DIY approach, you could also do:
pieces = []
for year, group in names.groupby(['year', 'sex']):
pieces.append(group.sort_index(by='births', ascending=False)[:1000])
top1000 = pd.concat(pieces, ignore_index=True)
top1000.tail()
#analyzing naming trends
boys = top1000[top1000.sex == 'M']
girls = top1000[top1000.sex == 'F']
total_births = top1000.pivot_table('births', rows='year', cols='name',aggfunc=sum)
subset = total_births[['John', 'Harry', 'Mary', 'Marilyn']]
subset.plot(subplots=True, figsize=(12, 10), grid=False, title='Number of Births per Year')
#Measuring the increase in naming diversity
prop_table = top1000.pivot_table('prop', rows='year', cols='sex', aggfunc=sum)
prop_table.plot(title='Sum of Table1000.Prop by Year and Sex', yticks=np.linspace(0, 1.2, 13), xticks=range(1880, 2020, 10))
#how many of the most popular names it takes to reach 50%.
df2010 = boys[boys.year == 2010]
df2010
prop_cumsum = df2010.sort_index(by='prop', ascending=False).prop.cumsum()
prop_cumsum[:10]
prop_cumsum.values.searchsorted(0.5) + 1 #searchsorted is a method of numpy.ndarray
#plus 1 is because arrays are zero-indexed
type(prop_cumsum.values)
df1900 = boys[boys.year == 1900]
in1900 = df1900.sort_index(by='prop', ascending=False).prop.cumsum()
in1900.values.searchsorted(0.5) + 1 #so 2010's names are more diversified
def get_quantile_count(group, q=0.5):
group = group.sort_index(by='prop', ascending=False)
return group.prop.cumsum().values.searchsorted(q) + 1
diversity = top1000.groupby(['year', 'sex']).apply(get_quantile_count)
diversity = diversity.unstack('sex') #now diversity has two time series, ne for each sex, indexed by year
diversity.head()
diversity.plot(title='Number of Popular Names in Top 50%')
#The 'Last Letter' Revolution
#exact last letter from name column
get_last_letter = lambda x: x[-1]
last_letters = names.name.map(get_last_letter)
#Difference between map, applymap and apply methods in Pandas
#map is a Series method whereas the rest are DataFrame methods.
#apply is column-or-row-wise. applymap and map are element-wise.
last_letters.name = 'last_letter'
table = names.pivot_table('births', rows=last_letters, cols=['sex', 'year'], aggfunc=sum)
suitable = table.reindex(columns=[1910, 1960, 2010], level='year')
#DataFrame.reindex(index=None, columns=None, **kwargs)
type(suitable)
suitable.head()
suitable.sum() #Return the sum of the values for the requested axis(axis : {row (0), columns (1)})
type(suitable.sum()) #pandas.core.series.Series
letter_prop = subtable/subtable.sum().astype(float)
import matplotlib.pyplot as plt
fig, axes = plt.subplots(2, 1, figsize=(10, 10))
letter_prop['M'].plot(kind='bar', rot=0, ax=axes[0], title='Male')
letter_prop['F'].plot(kind='bar', rot=0, ax=axes[1], title='Female', legend=False)
#boy names ending in 'n' have experienced significant growth since the 1960s
full_letter_prop = table/table.sum().astype(float)
dny_ts = full_letter_prop.ix[['d', 'n', 'y'], 'M'].T
dny_ts.head()
type(dny_ts) #pandas.core.frame.DataFrame
dny_ts.plot()
#boy names that became girl names (and vice versa)
all_names = top1000.name.unique()
mask = np.array(['lesl' in x.lower() for x in all_names])
lesley_like = all_names[mask]
lesley_like
columns = ['name', 'sex', 'births']
for year in years:
path = './pydata-book/ch02/names/yob%d.txt' % year
frame = pd.read_csv(path, names=columns)
frame['year'] = year
pieces.append(frame)
import pandas as pd
pieces = []
columns = ['name', 'sex', 'births']
for year in years:
path = './pydata-book/ch02/names/yob%d.txt' % year
frame = pd.read_csv(path, names=columns)
frame['year'] = year
pieces.append(frame)
names = pd.concat(pieces, ignore_index=True)
names
total_births = names.pivot_table('births', rows='year', cols='sex', aggfunc=sum)
total_births.tail()
total_births.plot(title='Total Birth by Sex and Year')
#of births.
#insert a column prop with the fraction of babies given each name relative to the total number
#We first group the data by year and sex, then add the new column to each group
births = group.births.astype(float) #we first need to cast integer to floating
def add_prop(group):
group['prop'] = births / births.sum() #births.sum() automatically sum by group
#once the data frame has been grouped
return group
names = names.groupby(['year', 'sex']).apply(add_prop) #great use of apply!!
import numpy as np
np.allclose(names.groupby(['year', 'sex']).prop.sum(), 1)
class(names.groupby(['year', 'sex'])) #pandas.core.groupby.DataFrameGroupBy
#!!! when performing a group operation like this, it's often valuable to do a sanity check
#np.allclose
#like verifying that the prop column of each group sums to 1
from collections import defaultdict
from collections import Counter
from pandas import DataFrame, Series
names = pd.concat(pieces, ignore_index=True)
names
total_births = names.pivot_table('births', rows='year', cols='sex', aggfunc=sum)
total_births.tail()
total_births.plot(title='Total Birth by Sex and Year')
def add_prop(group):
births = group.births.astype(float) #we first need to cast integer to floating
group['prop'] = births / births.sum() #births.sum() automatically sum by group
#once the data frame has been grouped
return group
def add_prop(group):
births = group.births.astype(float) #we first need to cast integer to floating
group['prop'] = births / births.sum() #births.sum() automatically sum by group
#once the data frame has been grouped
return group
names = names.groupby(['year', 'sex']).apply(add_prop) #great use of apply!!
names
class(names.groupby(['year', 'sex'])) #pandas.core.groupby.DataFrameGroupBy
type(names.groupby(['year', 'sex'])) #pandas.core.groupby.DataFrameGroupBy
import numpy as np
np.allclose(names.groupby(['year', 'sex']).prop.sum(), 1)
def get_top1000(group):
return group.sort_index(by='births', ascending=False)[:1000]
top1000 = names.groupby(['year', 'sex']).apply(get_top1000)
top1000
pieces = []
for year, group in names.groupby(['year', 'sex']):
pieces.append(group.sort_index(by='births', ascending=False)[:1000])
top1000 = pd.concat(pieces, ignore_index=True)
top1000.tail()
boys = top1000[top1000.sex == 'M']
girls = top1000[top1000.sex == 'F']
total_births = top1000.pivot_table('births', rows='year', cols='name',aggfunc=sum)
subset = total_births[['John', 'Harry', 'Mary', 'Marilyn']]
subset.plot(subplots=True, figsize=(12, 10), grid=False, title='Number of Births per Year')
prop_table = top1000.pivot_table('prop', rows='year', cols='sex', aggfunc=sum)
prop_table.plot(title='Sum of Table1000.Prop by Year and Sex', yticks=np.linspace(0, 1.2, 13), xticks=range(1880, 2020, 10))
#how many of the most popular names it takes to reach 50%.
df2010 = boys[boys.year == 2010]
df2010
prop_cumsum = df2010.sort_index(by='prop', ascending=False).prop.cumsum()
prop_cumsum[:10]
prop_cumsum.values.searchsorted(0.5) + 1 #searchsorted is a method of numpy.ndarray
#plus 1 is because arrays are zero-indexed
type(prop_cumsum.values)
df1900 = boys[boys.year == 1900]
in1900 = df1900.sort_index(by='prop', ascending=False).prop.cumsum()
in1900.values.searchsorted(0.5) + 1 #so 2010's names are more diversified
def get_quantile_count(group, q=0.5):
group = group.sort_index(by='prop', ascending=False)
return group.prop.cumsum().values.searchsorted(q) + 1
diversity = top1000.groupby(['year', 'sex']).apply(get_quantile_count)
diversity = diversity.unstack('sex') #now diversity has two time series, ne for each sex, indexed by year
diversity.head()
diversity.plot(title='Number of Popular Names in Top 50%')
get_last_letter = lambda x: x[-1]
last_letters = names.name.map(get_last_letter)
#Difference between map, applymap and apply methods in Pandas
#map is a Series method whereas the rest are DataFrame methods.
#apply is column-or-row-wise. applymap and map are element-wise.
last_letters.name = 'last_letter'
table = names.pivot_table('births', rows=last_letters, cols=['sex', 'year'], aggfunc=sum)
suitable = table.reindex(columns=[1910, 1960, 2010], level='year')
#DataFrame.reindex(index=None, columns=None, **kwargs)
type(suitable)
suitable.head()
suitable.sum() #Return the sum of the values for the requested axis(axis : {row (0), columns (1)})
type(suitable.sum()) #pandas.core.series.Series
letter_prop = subtable/subtable.sum().astype(float)
letter_prop = suitable/subtable.sum().astype(float)
last_letters.name = 'last_letter'
table = names.pivot_table('births', rows=last_letters, cols=['sex', 'year'], aggfunc=sum)
suitable = table.reindex(columns=[1910, 1960, 2010], level='year')
#DataFrame.reindex(index=None, columns=None, **kwargs)
type(suitable)
suitable.head()
suitable.sum() #Return the sum of the values for the requested axis(axis : {row (0), columns (1)})
type(suitable.sum()) #pandas.core.series.Series
letter_prop = suitable/subtable.sum().astype(float)
letter_prop = suitable/suitable.sum().astype(float)
subtable = table.reindex(columns=[1910, 1960, 2010], level='year')
#DataFrame.reindex(index=None, columns=None, **kwargs)
type(suitable)
subtable.head()
subtable.sum() #Return the sum of the values for the requested axis(axis : {row (0), columns (1)})
type(suitable.sum()) #pandas.core.series.Series
letter_prop = subtable/subtable.sum().astype(float)
import matplotlib.pyplot as plt
fig, axes = plt.subplots(2, 1, figsize=(10, 10))
letter_prop['M'].plot(kind='bar', rot=0, ax=axes[0], title='Male')
letter_prop['F'].plot(kind='bar', rot=0, ax=axes[1], title='Female', legend=False)
#boy names ending in 'n' have experienced significant growth since the 1960s
full_letter_prop = table/table.sum().astype(float)
dny_ts = full_letter_prop.ix[['d', 'n', 'y'], 'M'].T
dny_ts.head()
type(dny_ts) #pandas.core.frame.DataFrame
dny_ts.plot()
#boy names that became girl names (and vice versa)
all_names = top1000.name.unique()
mask = np.array(['lesl' in x.lower() for x in all_names])
lesley_like = all_names[mask]
lesley_like
filtered = top1000[top1000.name.isin(lesley_like)]
filtered.groupby('name').births.sum()
table = filtered.pivot_table('birth', rows='year', cols='sex', aggfunc='sum')
table = filtered.pivot_table('births', rows='year', cols='sex', aggfunc='sum')
table = table.div(table.sum(1), axis=0)
table
table.tail()
table.head()
table.plot(style={'M': 'k-', 'F': 'k--'})
table.plot(style={'M': 'k-', 'F': 'k--'}, title='Proportion of male/female Lesley-like names over time')
data = {i: randn() for i in xrange(7)}
data = {i : randn() for i in xrange(7)}
a
a = 5
a
from numpy import *
data = {i : randn() for i in xrange(7)}
data = {i : randn() for i in range(7)}
from scipy import *
data = {i : randn() for i in xrange(7)}
data2 = {i : np.random.randn() for i in xrange(7)}
data
data2
print data
import numpy.random as randn
data = {i : randn() for i in xrange(7)}
data
import numpy.random as randn
data = {i : randn() for i in xrange(7)}
data = {i:randn() for i in xrange(7)}
clear
get_ipython().magic(u'reset ')
import numpy.random as randn
data = {i:randn() for i in xrange(7)}
numpy.random
from numpy.random import randn
data = {i:randn() for i in xrange(7)}
data
print data # if
import datatime
import datatime
import datetime
datatime.
datetime.timedelta
b = [1, 2, 3]
b = [1, 2, 3]
get_ipython().magic(u'pinfo b')
get_ipython().magic(u'pinfo b')
get_ipython().magic(u'pinfo b')
import numpy as np
get_ipython().magic(u'psearch np.*load*')
_
__
--
__
_
_27
_i27
_i100
_100
_i99
get_ipython().magic(u'run')
get_ipython().magic(u'logstart')
get_ipython().magic(u'logstate')
get_ipython().magic(u'logon')
get_ipython().magic(u'timeit x.startswith(y)')
x = 'foobar'
y = 'foo'
get_ipython().magic(u'timeit x.startswith(y)')
get_ipython().magic(u'timeit x[:3] == y')
get_ipython().magic(u'run')
get_ipython().magic(u'run prof_mod')
def add_and_sum(x, y):
added = x + y
summed = added.sum(axis=1)
return summed
def call_function():
x = randn(1000, 1000)
y = randn(1000, 1000)
return add_and_sum(x, y)
q
get_ipython().magic(u'prun add_and_sum(x,y)')
get_ipython().magic(u'prun add_and_sum(x,y)')
def add_and_sum(x, y):
added = x + y
summed = added.sum(axis=1)
return summed
def call_function():
x = randn(1000, 1000)
y = randn(1000, 1000)
return add_and_sum(x, y)
get_ipython().magic(u'prun add_and_sum(x,y)')
def add_and_sum(x, y):
added = x + y
summed = added.sum(axis=1)
return summed
from numpy.random import randn
def call_function():
x = randn(1000, 1000)
y = randn(1000, 1000)
return add_and_sum(x, y)
get_ipython().magic(u'prun add_and_sum(x,y)')
get_ipython().magic(u'prun call_function()')
c.TerminalIPythonApp.extensions = ['line_profiler']
ipython notebook %pylab inline
ipython notebook %pylab inline
get_ipython().magic(u'pylab inline')
ipython notebook --pylab=inline
ipython notebook --pylab=inline
ipython notebook --pylab=inline
get_ipython().magic(u'pylab inline')
img = plt.imread('./pydata-book/ch03/stinkbug.png')
import matplotlib.pyplot as plt
img = plt.imread('./pydata-book/ch03/stinkbug.png')
imshow(img)
img = plt.imread('./pydata-book/ch03/stinkbug.png')
imshow(img)
import matplotlib.pyplot as plt
img = plt.imread('./pydata-book/ch03/stinkbug.png')
imshow(img)
img
from pylab import *
imshow(img)
img = plt.imread('./pydata-book/ch03/stinkbug.png')
img
imshow(img)
plot(randn(1000).cumsum())
plt.show()
imshow(img)
plt.show()
plt.show(img)
plt.show(imshow(img))
imshow(img)
plt.imshow(img)
img = plt.imread('./pydata-book/ch03/stinkbug.png')
plt.imshow(img)
from pylab import *
import matplotlib.pyplot as plt
img = plt.imread('./pydata-book/ch03/stinkbug.png')
get_ipython().magic(u'reset ')
import matplotlib.pyplot as plt
from pylab import *
plt.imshow()
plt.show()
img = plt.imread('./pydata-book/ch03/stinkbug.png')
imshow(img)
plot(randn(1000).cumsum())