np.arange(b)b elements, starting from zeronp.arange(a,b)a and ends at b - 1np.arange(a,b,c)a, increases in increments of c, stops at b - c.np.linspace(a,b,c)a and ending at b with c elementsc defaults to 50np.zeros( (a,b) )(a,b) filled with zerosnp.ones( (a,b) )(a,b) filled with onesnp.full( (a,b),c)(a,b) filled with cnp.zeros_like(A)A but filled with zerosnp.ones_like(A)A but filled with onesnp.full_like(A,b)A but filled with bnp.copy()b = a will make it so that changes to b propagate back to ab = a.copy() will make b not pass any changes backwards.For 1d arrays:
A[1:] will give all but the first element of AA[:-1] will give all but the last element of AA[::2]] will give every other element of AX = (A[1:] + A[:-1]) / 2 will create a new array X that interpolates between the values of A A[1] + A[0], A[2] + A[1], … , A[-1] + A[-2] and then halving the results.for 2d arrays:
B[0][0] will give the element in the first row & column of BB[0,0]] does the sameB[1:] will give all but the first row of BB[:][1:] will do the sameB[:-1] will give all but the last row of BB[:,1:] will give all but the first column of BB[a:b:c] will give rows a through b in steps of cB[::-1]] will reverse the order of rows in the arrayB[::,a,b,c]] will give columns a through b in steps of cB[::,::-1]] will reverse the order of columns in the arrayB[::-1,::-1] will reverse both the order of rows and columns in the array simultaneously (mirroring on the off-diagonal)B = A>5 will create an array B the same size as A. Elements of B will be True where the matching element of A is greater than 5, and False otherwiseA[A>5] will return only the elements of A which are larger than 5. B = A>5 followed by A[B] does the same thing as A[A>5]A[A>5 & A<8] does not work and gives an error ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()A meet the conditions or if we want to know if all of the elements in A meet the conditions. In practice, we want neither.B = A>5 then C=A<8 followed by A[B & C] behaves as intended, returning elements of A that are greater than 5 but less than 8.A[(A>5) & (A<8)] works as it evaluates each clause separately and then combines them.map function, which lets you apply an operation to each element in an array. It returns an iterable object, so you need to convert it to a Python listdef filter(value): return value > 5 & value < 8 A[list(map(filter,A))]
Suppose you want to show a subset of data in a plot, where X values are between 100 and 200.
lims = (X >100) & (X < 200)
ax.plot(X[lims],Y[lims])
np.loadtxt(a.txt) will try to load data from a.txt, using whitespace as a separatordelimiter = “,” will indicate that a , separates data entriesskiprows = n will skip the first n rows in the fileusecols = (a,b,c,…) will keep only data from the columns in the list (a,b,c,…)np.genfromtxt(a.txt) will also load data from a.txt using whitespace as a separator, but handles missing values more gracefullyskip_header = n is used instead of skiprowsC = np.append(A,B) will create C, which extends A by adding B data to the end of it.C = np.hstack(A,B) will do the sameC = np.vstack(A,B) will create a 2D array C by making rows of A and BA and B are the same lengthnp.squeeze(A) will remove singleton dimensions of array Anp.squeeze([A])[0] will give you the 0th element of A[A][0] will just give you the array A