注意:以下代碼是基於python3.5.0編寫的
import numpy as np # ----------------------找最大值------------------------- data = np.sin(np.arange(20)).reshape(5,4) ind = data.argmax(axis=0) #按列找最大值的索引值 print(ind) #輸出的是每列最大值的索引 data_max = data[ind, range(data.shape[1])] print(data_max) #根據索引值輸出最大值 print(all(data_max == data.max(axis=0))) # data.max(axis=0)直接尋找最大值,和通過索引找最大值效果一樣,返回值爲true # ----------------------tile複製----------------------------- a = np.arange(0, 40, 10) b = np.tile(a, (3, 5)) #把a複製3行5列 # -------------------------sort排序--------------------------- a = np.array([[4, 3, 5], [1, 2, 1]]) b = np.sort(a, axis=1) #把a按行排序賦值給b,另一種寫法a.sort(axis=1) a = np.array([4, 3, 1, 2]) j = np.argsort(a) #把數組a進行排序,並把索引值賦值給j print(a[j]) #通過索引值j輸出排序好的值