목록Numpy (57)
Note
How to compute the min-by-max for each row for a numpy array 2d? # Input np.random.seed(100) a = np.random.randint(1,10, [5,3]) a # Solution np.apply_along_axis(lambda x: np.min(x)/np.max(x), arr=a, axis=1) # output array([ 0.44444444, 0.125 , 0.5 , 1. , 0.11111111])
How to find the maximum value in each row of a numpy array 2d? # Input np.random.seed(100) a = np.random.randint(1,10, [5,3]) a # 1 np.amax(a, axis=1) # 2 np.apply_along_axis(np.max, arr=a, axis=1) # output array([9, 8, 6, 3, 9])
How to rank items in a multidimensional array using numpy? # Input: np.random.seed(10) a = np.random.randint(20, size=[2,5]) print(a) # Solution print(a.ravel().argsort().argsort().reshape(a.shape)) # output [[ 9 4 15 0 17] [16 17 8 9 0]] [[4 2 6 0 8] [7 9 3 5 1]]
How to rank items in an array using numpy? np.random.seed(10) a = np.random.randint(20, size=10) print('Array: ', a) # Solution print(a.argsort().argsort()) print('Array: ', a) #> Array: [ 9 4 15 0 17 16 17 8 9 0] #> [4 2 6 0 8 7 9 3 5 1] #> Array: [ 9 4 15 0 17 16 17 8 9 0]
How to create groud ids based on a given categorical variable? # Input: url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data' species = np.genfromtxt(url, delimiter=',', dtype='str', usecols=4) np.random.seed(100) species_small = np.sort(np.random.choice(species, size=20)) species_small array(['Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa', '..