목록Numpy (57)
Note
How to remove from one array those items that exist in another? a = np.array([1,2,3,4,5]) b = np.array([5,6,7,8,9]) np.setdiff1d(a,b) # output array([1, 2, 3, 4])
How to get the common items between two python numpy arrays? a = np.array([1,2,3,2,3,4,3,4,5,6]) b = np.array([7,2,10,2,7,4,9,4,9,8]) np.intersect1d(a,b)
How to generate custom sequences in numpy without hardcoding? a = np.array([1,2,3]) np.r_[np.repeat(a, 3), np.tile(a, 3)] # output array([1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3])
How to stack two arrays horizontally? a = np.arange(10).reshape(2,-1) b = np.repeat(1, 10).reshape(2,-1) # Answers # 1: np.concatenate([a, b], axis=1) # 2: np.hstack([a, b]) # 3: np.c_[a, b] # output array([[0, 1, 2, 3, 4, 1, 1, 1, 1, 1], [5, 6, 7, 8, 9, 1, 1, 1, 1, 1]])
How to stack two arrays vertically? a = np.arange(10).reshape(2,-1) b = np.repeat(1, 10).reshape(2,-1) # Answers # 1: np.concatenate([a, b], axis=0) # 2: np.vstack([a, b]) # 3: np.r_[a, b] # output array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]])