# http://ipython.org/ipython-doc/rel-1.1.0/api/generated/IPython.core.magics.pylab.html# %pylab --no-import-all inline # using loop and xrange n = 100 s = 0L for i in xrange(n+1): s += i print s # using builtin sum and range print range(101) sum(range(101)) # xrange sum(xrange(101)) # itertools is a great library # http://docs.python.org/2/library/itertools.html#itertools.count # itertools.count(start=0, step=1): # "Make an iterator that returns evenly spaced values starting with step." from itertools import islice, count c = count(0, 1) # look at how count() works by repetively calling c.next() print c.next() print c.next() print c.next() # let's add up using count and islice to limit how high we count # also make sure we're also using the builtin sum # http://docs.python.org/2/library/functions.html#sum __builtin__.sum(islice(count(0,1), 101L)) # generator for the lowercase English alphabet import string def alpha1(): m = list(string.lowercase) while m: yield m.pop(0) import string # make a generator comprehension -- generate items on demand k = (s for s in list(string.lowercase)) k k.next() # compare to k1, a list comprehension k1 = [s for s in list(string.lowercase)] k1 # create my own version of itertools.count def my_count(start, step): n = start while True: yield n n += step __builtin__.sum(islice(my_count(0,1), 101L)) from itertools import islice def triangular(): n = 1 i = 1 while True: yield n i +=1 n += i for i, n in enumerate(islice(triangular(), 10)): print i+1, n list(islice(triangular(), 100))[-1] list(islice(triangular(),99,100))[0] # Legend of the Chessboard YouTube video from IPython.display import YouTubeVideo YouTubeVideo('t3d0Y-JpRRg') # generator comprehension k = (pow(2,n) for n in xrange(64)) k.next() __builtin__.sum((pow(2,n) for n in xrange(64))) pow(2,64) -1 m = range(10) m m[0] m[-1] m[::-1] m[2:3] import string alphabet = string.lowercase alphabet # 13 letter of the alphabet alphabet[12] import numpy as np import pandas as pd import matplotlib.pyplot as plt from pandas import Series, DataFrame # first: a numpy array of zero-dimension a0 = np.array(5) a0 a0.ndim, a0.shape # 1-d array a1 = np.array([1,2]) a1.ndim, a1.shape # 2-d array a2 = np.array(([1,2], [3,4])) a2.ndim, a2.shape a2.dtype from numpy import arange type(arange(10)) for k in arange(10): print k list(arange(10)) == list(xrange(10)) #how to map 0..63 -> 2x2 array a3 = np.arange(64).reshape(8,8) a3 # 2nd row, 3rd column --> remember index starts at 0 a3[1,2] # check that reshape works for i in range(8): for j in range(8): if a3[i,j] != i*8 + j: print i, j 2*a3 a3+2 # reverse sort -- best way? #http://stackoverflow.com/a/6771620/7782 np.sort(np.arange(100))[::-1] # list comprehension [i for i in xrange(20) if i % 3 == 0] a3 = np.arange(20) a3 # basic indexing print a3[0] print a3[::-1] print a3[2:5] np.mod(a3, 3) np.mod(a3, 3) == 0 divisible_by_3 = np.mod(a3, 3) == 0 a3[divisible_by_3] # if you want to understand this in terms of the overloaded operators -- don't worry if you don't get this. a3.__getitem__(np.mod(a3,3).__eq__(0)) a4 = arange(100) a4sqrt = np.sqrt(a4) a4[a4sqrt == a4sqrt.astype(np.int)] s1 = Series(arange(5)) type(s1) s1.ndim, isinstance(s1, np.ndarray) s1.index import string allTheLetters = string.lowercase allTheLetters s2 = Series(data=arange(5), index=list(allTheLetters)[:5]) s2 s2.index # can use both numeric indexing and the labels s2[0], s2['a'] for i in range(len(s2)): print i, s2[i] s3 = Series(data=['albert', 'betty', 'cathy'], index=[3,1, 0]) s3 s3[0], list(s3)[0] s3[::-1] for i in range(len(s3)): print i, s3[i:i+1] s3.name = 'person names' s3.name s3.index.name = 'confounding label' s3.index.name s3 # Gauss addition using np.arange, Series from pandas import Series Series(arange(101).cumsum()).plot() from pandas import Series Series((pow(2,k) for k in xrange(64)), dtype=np.float64).cumsum().plot() # http://docs.scipy.org/doc/numpy/reference/generated/numpy.ones.html from numpy import ones 2*ones(64, dtype=np.int) arange(64) sum(np.power(2, arange(64, dtype=np.uint64))) sum(np.power(2*ones(64, dtype=np.uint64), arange(64))) precise_ans = sum([pow(2,n) for n in xrange(64)]) np_ans = sum(np.power(2*ones(64, dtype=np.uint64), arange(64))) precise_ans, np_ans # Raise an assertion if two items are not equal up to desired precision. np.testing.assert_almost_equal(precise_ans, np_ans) is None # not really intuitive to me: reversal of column/row DataFrame(dict([('06', {'name': 'California', 'abbreviation':'CA'})] )) DataFrame([{'name': 'California', 'abbreviation':'CA'}], index= ['06']) Series(['06'], name='FIPS') DataFrame([{'name': 'California', 'abbreviation':'CA'}], index=Series(['06'], name='FIPS')) n0 = 5 n0 == 5 try: n0.__eq__(5) except Exception as e: print e (n0.__cmp__(4), n0.__cmp__(5), n0.__cmp__(6)) arange(5) == 2 # # http://docs.scipy.org/doc/numpy/reference/generated/numpy.array_equal.html np.array_equal(arange(5) == 2 , arange(5).__eq__(2)) isinstance([1,2], list) isinstance(arange(5), list) # what does that mean -- could still be list-like l1 = range(5) type(l1) l1[0], l1.__getitem__(0), l1[0] == l1.__getitem__(0) l1[::-1], l1.__getitem__(slice(None, None, -1)) ar1 = arange(5) ar1[3], ar1.__getitem__(3) ar1 == 2 ar1[ar1 == 2].shape ar1.__eq__(2) ar1.__getitem__(slice(2, 4, None)) slice(ar1.__eq__(2), None, None) ar1.__getitem__(ar1.__eq__(2)) ar1[:2], ar1.__getitem__(slice(2)) ar1 + 7 ar1.__add__(7) min(ar1 + 7) alphabet[:]