import numpy as np lst = [10, 20, 30, 40] arr = np.array([10, 20, 30, 40]) lst[0] arr[0] arr[-1] arr[2:] lst[-1] = 'a string inside a list' lst arr[-1] = 'a string inside an array' arr.dtype arr[-1] = 1.234 arr np.zeros(5, dtype=float) np.zeros(3, dtype=int) np.zeros(3, dtype=complex) print '5 ones:', np.ones(5) a = np.empty(4) a.fill(5.5) a np.arange(5) print "A linear grid between 0 and 1:" print np.linspace(0, 1, 4) print "A logarithmic grid between 10**1 and 10**3:" print np.logspace(1, 3, 4) np.random.randn(5) norm10 = np.random.normal(10, 3, 5) norm10 mask = norm10 > 9 mask print 'Values above 9:', norm10[mask] print 'Resetting all values above 9 to 0...' norm10[mask] = 0 print norm10 lst2 = [[1, 2], [3, 4]] arr2 = np.array([[1, 2], [3, 4]]) arr2 print lst2[0][1] print arr2[0,1] np.zeros((2,3)) np.random.normal(10, 3, (2, 4)) arr = np.arange(8).reshape(2, 4) print arr arr = np.arange(8) arr2 = arr.reshape(2, 4) arr[0] = 1000 print arr print arr2 print 'Slicing in the second row:', arr2[1, 2:4] print 'All rows, third column :', arr2[:, 2] print 'First row: ', arr2[0] print 'Second row: ', arr2[1] print 'Data type :', arr.dtype print 'Total number of elements :', arr.size print 'Number of dimensions :', arr.ndim print 'Shape (dimensionality) :', arr.shape print 'Memory used (in bytes) :', arr.nbytes print 'Minimum and maximum :', arr.min(), arr.max() print 'Sum and product of all elements :', arr.sum(), arr.prod() print 'Mean and standard deviation :', arr.mean(), arr.std() print 'For the following array:\n', arr2 print 'The sum of elements along the rows is :', arr2.sum(axis=1) print 'The sum of elements along the columns is :', arr2.sum(axis=0) np.zeros((3,4,5,6)).sum(2).shape print 'Array:\n', arr2 print 'Transpose:\n', arr2.T arr1 = np.arange(4) arr2 = np.arange(10, 14) print arr1, '+', arr2, '=', arr1+arr2 print arr1, '*', arr2, '=', arr1*arr2 1.5 * arr1 print np.arange(3) print np.arange(3) + 5 np.ones((3, 3)) + np.arange(3) np.arange(3).reshape((3, 1)) + np.arange(3) arr1 = np.ones((2, 3)) arr2 = np.ones((2, 1)) arr1 + arr2 arr1 = np.ones((2, 3)) arr2 = np.ones(2) arr1 + arr2 arr1 = np.ones((2, 3)) arr2 = np.ones((2, 1)) arr1 + arr2 A = np.arange(1, 9).reshape((2, 4)) B = np.arange(1, 3) A + B.reshape((2, 1)) print B.shape print B[:, np.newaxis].shape A + B[:, np.newaxis] x = np.linspace(0, 2*np.pi, 100) y = np.sin(x) v1 = np.array([2, 3, 4]) v2 = np.array([1, 0, 1]) print v1, '.', v2, '=', np.dot(v1, v2) A = np.arange(6).reshape(2, 3) print A, 'x', v1, '=', np.dot(A, v1) print np.dot(A, A.T) print np.dot(A.T, A) arr = np.arange(10).reshape(2, 5) np.savetxt('test.out', arr, fmt='%.2e', header="My dataset") !cat test.out arr2 = np.loadtxt('test.out') print arr2 np.save('test.npy', arr2) # Now we read this back arr2n = np.load('test.npy') # Let's see if any element is non-zero in the difference. # A value of True would be a problem. print 'Any differences?', np.any(arr2-arr2n) np.savez('test.npz', arr, arr2) arrays = np.load('test.npz') arrays.files np.savez('test.npz', array1=arr, array2=arr2) arrays = np.load('test.npz') arrays.files print 'First row of first array:', arrays['array1'][0] # This is an equivalent way to get the same field print 'First row of first array:', arrays.f.array1[0]