a = scipy.array([[1,2,3],[4,5,6]]) b = scipy.array([i*i for i in range(100) if i%2==1]) c = b.tolist() # convert array back to Python list
a = scipy.zeros(100) # a 100-element array of float zeros b = scipy.zeros((2,8), int) # a 2x8 array of int zeros c = scipy.zeros((N,M,L), complex) # a NxMxL array of complex zeros
a = scipy.ones(10, int) # a 10-element array of int ones b = scipy.pi * scipy.ones((5,5)) # a useful way to fill up an array with a specified value
id = scipy.eye(10,10, int) # 10x10 identity matrix (1's on diagonal) offdiag = scipy.eye(10,10,1)+scipy.eye(10,10,-1) # off diagonal elements = 1
b = scipy.transpose(a) # reverse dimensions of a (even for dim > 2) b = a.T # equivalent to scipy.transpose(a) c = scipy.swapaxes(a, axis1, axis2) # swap specified axes
a = scipy.arange(start, stop, increment) # like Python range, but with (potentially) real-valued arrays b = scipy.linspace(start, stop, num_elements) # create array of equally-spaced points based on specifed number of points
a = scipy.random.random((100,100)) # 100x100 array of floats uniform on [0.,1.) b = scipy.random.randint(0,10, (100,)) # 100 random ints uniform on [0, 10), i.e., not including the upper bound 10 c = scipy.random.standard_normal((5,5,5)) # zero-mean, unit-variance Gaussian random numbers in a 5x5x5 array
elem = a[i,j,k] # equiv. to a[i][j][k] but presumably more efficient
last_elem = a[-1] # the last element of the array
i = scipy.array([0,1,2,1]) # array of indices for the first axis j = scipy.array([1,2,3,4]) # array of indices for the second axis a[i,j] # return array([a[0,1], a[1,2], a[2,3], a[1,4]]) b = scipy.array([True, False, True, False]) a[b] # return array([a[0], a[2]]) since only b[0] and b[2] are True
section = a[10:20, 30:40] # 10x10 subblock starting at [10,30]
asection = a[10:, 30:] # missing stop index implies until end of array bsection = b[:10, :30] # missing start index implies until start of array
x = a[:, 0] # get everything in the 0th column (missing start and stop) y = a[:, 1] # get everything in the 1st column
tail = a[-10:] # grab the last 10 elements of the array slab = b[:, -10:] # grab a slab of width 10 off the "side" of the array interior = c[1:-1, 1:-1, 1:-1] # slice out everything but the outer shell
c = a + b # add a and b element-wise (must be same shape) d = e * f # multiply e and f element-wise (NOT matrix multiplication) g = -h # negate every element of h y = (x+1)%2 # swap 0's and 1's in binary array x z = w > 0.0 # return boolean array indicating which elements are > 0.0 logspace = 10.**scipy.linspace(-6.0, -1.0, 50) # 50 equally-spaced-in-log points between 1.e-06 and 1.0e-01
y = scipy.sin(x) # sin of every element of x w = scipy.sin([i*i for i in range(100) if i%2==1]) # conversion from list to array as part of function application z = scipy.exp((0.+1.j) * theta) # exp(i * theta) where i = sqrt(-1) = 0.+1.j
s = scipy.sum(a) # sum all elements in a, returning a scalar s0 = scipy.sum(a, axis=0) # sum elements along specified axis (=0), returning an array of remaining shape, e.g., a = scipy.ones((10,20,30)) s0 = scipy.sum(a, axis=0) # s0 has shape (20,30)
m = scipy.mean(a, axis) # compute mean along the specified axis (over entire array if axis=None) s = scipy.std(a, axis) # compute standard deviation along the specified axis (over entire array if axis=None)
s0 = scipy.cumsum(a, axis=0) # cumulatively sum over 0 axis, returning array with same shape as a s0 = scipy.cumsum(a) # cumulatively sum over 0 axis, returning 1D array of length shape[0]*shape[1]*...*shape[dim-1]
Many of these work both as separate functions (scipy.blah(a)) as well as array methods (a.blah()).