%pylab inline dat = [3, -1, 0.5, 4, 2] plot(dat) # plotting points A = (0,1) B = (1,0) C = (2,1) # x = [A[0], B[0], C[0]] x = map(lambda i: i[0], [A,B,C]) y = [i[1] for i in [A,B,C]] plot(x, y, 'bo-') axis([-0.1, 2.1, -0.1, 1.1]); # note ; at the end x = range(11) y1 = [i**2 for i in x] y2 = [i**2 for i in reversed(x)] plot(x, y1, 'ro-') plot(x, y2, 'gs--') xlabel('x') ylabel('y') title('$f(x) \sim x^2$') grid() x = range(5) y = x + 4 y = 2 * x # not what we expect for a math expression y x = array(range(5)) # covert a list to array object y = 2*x + 1 print(y) type(y) x = arange(11) # numpy built-in plot(x, x**2, 'ro-') plot(x, (10-x)**2, 'gs-') x = arange(-1, 1.1, 0.1) # arange works with floats plot(x, x**2) x = linspace(-pi, pi, 256) # nb of points instead of sampling plot(x, sin(x), label=r'$\sin(x)$') plot(x, cos(x), label=r'$\cos(x)$' ) legend(loc='upper left') import random def rand_gauss(N): return [random.gauss(0,1) for _ in xrange(N)] sample_size = 500 d1 = rand_gauss(sample_size) d2 = np.random.randn(sample_size) subplot(1,2,1) hist(d1, label='gauss') subplot(1,2,2) hist(d2, color='red') %timeit rand_gauss(sample_size) %timeit np.random.randn(sample_size) x_lst = range(10) # regular list x_lst[0:-1:2] # [start:stop:step] x_arr = arange(10) x_arr[0:-1:2] print x_lst x_lst[0] = 'hi' print x_lst x_arr[0] = 'hi' x_arr[0] = 10.5 # danger zone x_arr def a_info(arr): print 'dtype:', arr.dtype # data type print 'nbytes:', arr.nbytes # nb of bytes print 'ndim:', arr.ndim print 'shape:', arr.shape print 'size:', arr.size a_info(x_arr) len(x_arr) == x_arr.size y = arange(3, dtype='float') a_info(y) # type casting a_info(y.astype('complex')) M = array( [[2.0, 1, 3], [0.1, 0.2, 0.3]] ) M a_info(M) M[0,0] M[0] # select 1st row M[1,:] # select 2d row M[:, -1] # select last column # assignment works the same way M[:,-1] = 100 M M[0:, 1:] # all rows, 2 last columns print arange(-1.0, 1.0, 0.2) # low, high (excl), step print linspace(-1.0, 1.0, 9) # low, high (incl), nb_of_points print zeros((3,3)) print diag((3.,2,1)) print np.random.rand(2, 4) # random numbers from a uniform distribution between [0, 1[ print np.random.randn(2, 4) # randome numbers from a normal distribution (mu=0, sigma=1) x = linspace(-1, 1, 5) print 'x:', x c = (x >= 0) print 'c:', c print c.dtype print 'x[c]:', x[c] y = exp(x) print 'y[c]', y[c] c = (x >= -0.5) * ( x <= 0.5) x[c] def a_meth(arr): print 'min:', arr.min() print 'max:', arr.max() print 'mean:', arr.mean() print 'std:', arr.std() x = randn(100)*2.3 + 1.7 a_meth(x) v = arange(6) print 2 * v - 5 # linear transform v2 = v[::-1] print v + v2 # element-wise sum print (1 - np.exp(v)) # vectorized function M = (rand(4,3) * 10).astype('int') print 2*M v = array([0,1,2]) print M * v # multiply every row by vector M = randn(10, 3) save('data/M.npy', M) # dump an array into a binary file (similar to MATLAB's .mat) N = load('data/M.npy') (N - M).sum() savetxt('data/M.csv', M, fmt='%.3f', delimiter='\t') !head data/M.csv N = genfromtxt('data/M.csv') N (N-M).sum()