# Solution: defone_hot_encodings(arr): uniqs = np.unique(arr) out = np.zeros((arr.shape[0], uniqs.shape[0])) for i, k inenumerate(arr): out[i, k-1] = 1 return out
# Solution: output = [np.argwhere(np.unique(species_small) == s).tolist()[0][0] for val in np.unique(species_small) for s in species_small[species_small==val]]
# Solution: For Loop version output = [] uniqs = np.unique(species_small)
for val in uniqs: # uniq values in group for s in species_small[species_small==val]: # each element in group groupid = np.argwhere(uniqs == s).tolist()[0][0] # groupid output.append(groupid)
numpy.ravel(a, order='C') # Return a contiguous flattened array. A 1-D array, containing the elements of the input, is returned. A copy is made only if needed.
# Solution # No direct way to implement this. Just a version of a workaround. numeric_column = iris[:,1].astype('float') # sepalwidth grouping_column = iris[:,4] # species
# List comprehension version [[group_val, numeric_column[grouping_column==group_val].mean()] for group_val in np.unique(grouping_column)]
# For Loop version output = [] for group_val in np.unique(grouping_column): output.append([group_val, numeric_column[grouping_column==group_val].mean()])
defgen_strides(a, stride_len=5, window_len=5): n_strides = ((a.size-window_len)//stride_len) + 1 # return np.array([a[s:(s+window_len)] for s in np.arange(0, a.size, stride_len)[:n_strides]]) return np.array([a[s:(s+window_len)] for s in np.arange(0, n_strides*stride_len, stride_len)])
a = np.array([[1, 2, 3, 4], # Create the following rank 2 array with shape (3, 4) [5, 6, 7, 8], [9,10,11,12]])
b = a[:2, 1:3] # Use slicing to pull out the subarray consisting of the first 2 rows and columns 1 and 2; print(b) # b is the following array of shape (2, 2): # [[2 3] # [6 7]]
# A slice of an array is a view into the same data, so modifying it # will modify the original array. print(a[0, 1]) # Prints "2" b[0, 0] = 77# b[0, 0] is the same piece of data as a[0, 1] print(a[0, 1]) # Prints "77"
# Two ways of accessing the data in the middle row of the array. # Mixing integer indexing with slices yields an array of lower rank, # while using only slices yields an array of the same rank as the # original array: row_r1 = a[1, :] # Rank 1 view of the second row of a row_r2 = a[1:2, :] # Rank 2 view of the second row of a print(row_r1, row_r1.shape) # Prints "[5 6 7 8] (4,)" print(row_r2, row_r2.shape) # Prints "[[5 6 7 8]] (1, 4)"
# We can make the same distinction when accessing columns of an array: col_r1 = a[:, 1] col_r2 = a[:, 1:2] print(col_r1, col_r1.shape) # Prints "[ 2 6 10] (3,)" print(col_r2, col_r2.shape) # Prints "[[ 2] # [ 6] # [10]] (3, 1)"
bool_idx = (a > 2) # Find the elements of a that are bigger than 2; # this returns a numpy array of Booleans of the same # shape as a, where each slot of bool_idx tells # whether that element of a is > 2.
# We use boolean array indexing to construct a rank 1 array # consisting of the elements of a corresponding to the True values # of bool_idx print(a[bool_idx]) # Prints "[3 4 5 6]"
# We can do all of the above in a single concise statement: print(a[a > 2]) # Prints "[3 4 5 6]"
# We will add the vector v to each row of the matrix x, # storing the result in the matrix y x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]]) v = np.array([1, 0, 1]) vv = np.tile(v, (4, 1)) # Stack 4 copies of v on top of each other print(vv) # Prints "[[1 0 1] # [1 0 1] # [1 0 1] # [1 0 1]]" y = x + vv # Add x and vv elementwise print(y) # Prints "[[ 2 2 4 # [ 5 5 7] # [ 8 8 10] # [11 11 13]]"
广播通常会使你的代码更简洁,效率更高,因此你应该尽可能地使用它。
改变数组的形状
问题:如何将一维数组转换为 2 行的 2 维数组
1 2 3 4 5 6 7
arr = np.arange(10) arr.reshape(2, -1) # Setting to -1 automatically decides the number of cols # > array([[0, 1, 2, 3, 4], # > [5, 6, 7, 8, 9]])
numpy.reshape(a, newshape, order='C') # Gives a new shape to an array without changing its data.
# i, j contain the row numbers and column numbers of 600 elements of iris_x np.random.seed(100) iris_2d[np.random.choice((i), 20), np.random.choice((j), 20)] = np.nan