목록Numpy (57)
Note
How to swap two rows in a 2d numpy array? # Input arr = np.arange(9).reshape(3,3) # Solution arr[[1,0,2], :] # output array([[3, 4, 5], [0, 1, 2], [6, 7, 8]])
How to swap two columns in a 2d numpy array? # Input arr = np.arange(9).reshape(3,3) arr # Solution arr[:, [1,0,2]] # output array([[1, 0, 2], [4, 3, 5], [7, 6, 8]])
How to make a python function that handles scalars to work on numpy arrays? def maxx(x, y): """Get the maximum of two items""" if x >= y: return x else: return y pair_max = np.vectorize(maxx, otypes=[float]) a = np.array([5, 7, 9, 8, 6, 4, 5]) b = np.array([6, 3, 4, 8, 9, 7, 1]) pair_max(a, b) # output array([ 6., 7., 9., 8., 9., 7., 5.])
How to extract all numbers between a given range from a numpy array? a = np.arange(15) # 1 index = np.where((a >= 5) & (a =5, a= 5) & (a