목록Numpy (57)
Note
How to reshape an array? arr = np.arange(10) arr.reshape(2, -1) output array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]])
How to replace items that satisfy a condition without affecting the original array? arr = np.arange(10) out = np.where(arr % 2 == 1, -1, arr) arr #> array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) out #> array([ 0, -1, 2, -1, 4, -1, 6, -1, 8, -1])
How to replace items that satisfy a condition with another value in numpy array? arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) Output array([ 0, -1, 2, -1, 4, -1, 6, -1, 8, -1]) # input arr[arr % 2 == 1] = -1 arr # output array([ 0, -1, 2, -1, 4, -1, 6, -1, 8, -1])
How to extract items that satisfy a given condition from 1D array? # Input arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) # Solution arr[arr % 2 == 1]