目錄python
@數組
# 打印 numpy.genfromtxt 幫助文檔 print (help(numpy.genfromtxt)) # 打印range 幫助文檔 print(help(range))
示例app
import numpy # 讀入文本文件 # "world_alcohol.txt" == > 讀入的文件 # delimiter="," == > 文件內容之間的分割符 # dtype=str == > 讀入的文件的數據類型 # skip_header=1 == > 跳過第一行,即第一行數據不讀取 world_alcohol = numpy.genfromtxt("world_alcohol.txt", delimiter=",",dtype=str, skip_header=1) # 打印類型 print(type(world_alcohol)) # 打印讀入的內容 print (world_alcohol)
import numpy #The numpy.array() function can take a list or list of lists as input. When we input a list, we get a one-dimensional array as a result: vector = numpy.array([5, 10, 15, 20]) #When we input a list of lists, we get a matrix as a result: matrix = numpy.array([[5, 10, 15], [20, 25, 30], [35, 40, 45]]) # 一維數組 print(vector) # 二維數組 print(matrix)
結果
dom
Numpy中的行向量和列向量 - Suprecat的博客 - CSDN博客
https://blog.csdn.net/wintersshi/article/details/80489258python2.7
#We can use the ndarray.shape property to figure out how many elements are in the array vector = numpy.array([1, 2, 3, 4]) print(vector.shape) #For matrices, the shape property contains a tuple with 2 elements. matrix = numpy.array([[5, 10, 15], [20, 25, 30]]) print(matrix.shape)
結果ssh
(4,) (2, 3)
import numpy #Each value in a NumPy array has to have the same data type #NumPy will automatically figure out an appropriate data type when reading in data or converting lists to arrays. #You can check the data type of a NumPy array using the dtype property. numbers = numpy.array([1, 2, 3, 4]) print (numbers) numbers.dtype
結果函數
[1 2 3 4] dtype('int32')
import numpy vector = numpy.array([5, 10, 15, 20]) print(vector[0:3])
結果this
[ 5 10 15]
import numpy matrix = numpy.array([ [5, 10, 15], [20, 25, 30], [35, 40, 45] ]) # 讀取全部的行,第1列(共0,1,2列)的數 print(matrix[:,1])
結果spa
[10 25 40]
import numpy matrix = numpy.array([ [5, 10, 15], [20, 25, 30], [35, 40, 45] ]) # 讀取全部的行,第0,1(共0,1,2列)列的數據 print(matrix[:,0:2])
結果.net
[[ 5 10] [20 25] [35 40]]
matrix = numpy.array([ [5, 10, 15], [20, 25, 30], [35, 40, 45] ]) # 矩陣的行1,2,列 0 ,1(共0,1,2列) print(matrix[1:3,0:2])
結果
[[20 25] [35 40]]
import numpy #it will compare the second value to each element in the vector # If the values are equal, the Python interpreter returns True; otherwise, it returns False vector = numpy.array([5, 10, 15, 20]) vector == 10
結果
array([False, True, False, False], dtype=bool)
import numpy matrix = numpy.array([ [5, 10, 15], [20, 25, 30], [35, 40, 45] ]) matrix == 25
結果
array([[False, False, False], [False, True, False], [False, False, False]], dtype=bool)
import numpy # Compares vector to the value 10, which generates a new Boolean vector [False, True, False, False]. It assigns this result to equal_to_ten # 生成矩陣向量 vector = numpy.array([5, 10, 15, 20]) # 與 10 相等 進行布爾運算 equal_to_ten = (vector == 10) print equal_to_ten # 獲得原來的數據 print(vector[equal_to_ten])
結果
[False True False False] [10]
import numpy matrix = numpy.array([ [5, 10, 15], [20, 25, 30], [35, 40, 45] ]) # 對全部行的第1列與 25 進行相等布爾運算 second_column_25 = (matrix[:,1] == 25) # 布爾運算的結果 print(second_column_25) # 還原 原來 的列 print(matrix[:, second_column_25]) # 將布爾結果對 行 進行求數 print(matrix[second_column_25, :])
結果
[False True False] [[10] [25] [40]] [[20 25 30]]
import numpy matrix = numpy.array([ [5, 10, 15], [20, 25, 30], [35, 40, 45] ]) # 全部行的第1列(共0,1,2列)的數據與25進行布爾運算 second_column_25 = matrix[:, 1] == 25 print(second_column_25) # 數據第1行第一列(共0,1,2列),25 ===> 10 matrix[second_column_25, 1] = 10 print(matrix)
結果
[False True False] [[ 5 10 15] [20 10 30] [35 40 45]]
import numpy #We can also perform comparisons with multiple conditions vector = numpy.array([5, 10, 15, 20]) # 與10進行布爾運算 print(vector == 10) # 與 5進行布爾運算 print(vector == 5) # 與操做 equal_to_ten_and_five = (vector == 10) & (vector == 5) print(equal_to_ten_and_five)
結果
[False True False False] [ True False False False] [False False False False]
import numpy vector = numpy.array([5, 10, 15, 20]) # 與10進行布爾運算 print(vector == 10) # 與 5進行布爾運算 print(vector == 5) # 或操做 equal_to_ten_and_five = (vector == 10) | (vector == 5) print(equal_to_ten_and_five)
結果
[False True False False] [ True False False False] [ True True False False]
import numpy vector = numpy.array([5, 10, 15, 20]) # 與10進行布爾運算 print(vector == 10) # 與 5進行布爾運算 print(vector == 5) # 或操做 equal_to_ten_or_five = (vector == 10) | (vector == 5) # 或操做後的數據進行修改 # 5, 10 ==> 50 vector[equal_to_ten_or_five] = 50 print(vector)
結果
[False True False False] [ True False False False] [50 50 15 20]
import numpy # We can convert the data type of an array with the ndarray.astype() method. vector = numpy.array(["1", "2", "3"]) # 原來的類型 print(vector.dtype) print(vector) # 字符串 ==> 數據類型 vector = vector.astype(float) # 如今的類型 print(vector.dtype) print(vector)
結果
<U1 ['1' '2' '3'] float64 [1. 2. 3.]
import numpy vector = numpy.array([5, 10, 15, 20]) vector.min() print (vector.min())
結果
5
說明
axis=1,每一行求和
axis=0,每一列求和
import numpy # The axis dictates which dimension we perform the operation on #1 means that we want to perform the operation on each row, and 0 means on each column matrix = numpy.array([ [5, 10, 15], [20, 25, 30], [35, 40, 45] ]) print(matrix.sum(axis=1))
結果
[ 30 75 120]
import numpy matrix = numpy.array([ [5, 10, 15], [20, 25, 30], [35, 40, 45] ]) print(matrix.sum(axis=0))
結果
[60 75 90]
reshape,對一組向量修改爲新一矩陣分佈
import numpy as np a = np.array([ [6., 9., 1., 5.], [3., 6., 4., 0.], [3., 9., 5., 7.] ]) print("a\n", a) print("a.shape\n", a.shape) a.shape = (2,6) print("a.shape = (2,6)\n", a) a.shape = (1,12) print("a.shape = (1,12)\n", a)
結果
a [[6. 9. 1. 5.] [3. 6. 4. 0.] [3. 9. 5. 7.]] a.shape (3, 4) a.shape = (2,6) [[6. 9. 1. 5. 3. 6.] [4. 0. 3. 9. 5. 7.]] a.shape = (1,12) [[6. 9. 1. 5. 3. 6. 4. 0. 3. 9. 5. 7.]]
import numpy as np # 生成 10 到 30 之間的數據,不包括30,間距爲5的數據 print(np.arange( 10, 30, 5 )) # 生成 10 到 11 之間的數據,不包括11,間距爲0.2的數據 print(np.arange( 10, 11, 0.2))
結果
[10 15 20 25] [10. 10.2 10.4 10.6 10.8]
import numpy as np # 生成一組向量 # To create sequences of numbers print(np.arange(15)) # 查看矩陣的信息 print(np.arange(15).shape) # 修改爲新3行5列的矩陣數據 a = np.arange(15).reshape(3, 5) print(a) # 查看矩陣的信息 print(a.shape)
結果
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14] (15,) [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]] (3, 5)
import numpy as np # 生成一組向量 print(np.arange(15)) # 查看矩陣的維度信息 print(np.arange(15).ndim) # 修改爲新3行5列的矩陣數據 a = np.arange(15).reshape(3, 5) print(a) # 查看矩陣的維度信息 print(a.ndim)
結果
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14] 1 [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]] 2
import numpy as np # 生成一組向量 print(np.arange(15)) # 查看矩陣的信息 print(np.arange(15).dtype) print(np.arange(15).dtype.name)
結果
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14] int32 int32
import numpy as np # 生成一組向量 print(np.arange(15)) # 查看矩陣總共有多少元素 print(np.arange(15).size) # 修改爲新3行5列的矩陣數據 a = np.arange(15).reshape(3, 5) print(a) # 查看矩陣總共有多少元素 print(a.size)
結果
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14] 15 [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]] 15
import numpy as np # 3行4列 print(np.zeros ((3,4)) ) print(np.zeros ((3,4)).dtype )
結果
[[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]] float64
numpy.ones(shape, dtype=None, order='C') Parameters: # 形狀 shape : int or sequence of ints Shape of the new array, e.g., (2, 3) or 2. # 數據類型 dtype : data-type, optional The desired data-type for the array, e.g., numpy.int8. Default is numpy.float64. # 存儲順序 order : {‘C’, ‘F’}, optional, default: C Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory. Returns: out : ndarray Array of ones with the given shape, dtype, and order.
示例一
import numpy as np # 生成float64類型 print(np.ones(5))
結果
[1. 1. 1. 1. 1.]
示例二
import numpy as np # 生成 int 類型 print(np.ones((5,), dtype=int))
結果
[1, 1, 1, 1, 1]
示例三
import numpy as np # 生成2行3列的float64類型數據 s = (2,3) print( np.ones(s))
結果
[[1. 1. 1.] [1. 1. 1.]]
示例四
import numpy as np print(np.ones( (2,3,4), dtype=np.int32 ))
結果
[[[1 1 1 1] [1 1 1 1] [1 1 1 1]] [[1 1 1 1] [1 1 1 1] [1 1 1 1]]]
擴展閱讀
numpy 中ones,zeros函數 - mmr598146920的專欄 - CSDN博客
https://blog.csdn.net/mmr598146920/article/details/80539733
import numpy as np # 2行3列隨機數 print(np.random.random((2, 3)))
結果
[[0.69509627 0.04046586 0.26786661] [0.44120144 0.05091389 0.44784084]]
import numpy as np from numpy import pi # 在 0 到時 2*pi 區間(不包括右端點 2*pi)內,等間距生成 5 個數據 print(np.linspace( 0, 2*pi, 5 ))
結果
[0. 1.57079633 3.14159265 4.71238898 6.28318531]
import numpy as np from numpy import pi # 求三角函數sin值 print(np.sin(np.linspace( 0, 2*pi, 5 )))
結果
[ 0.0000000e+00 1.0000000e+00 1.2246468e-16 -1.0000000e+00 -2.4492936e-16]
import numpy as np # the product operator * operates elementwise in NumPy arrays a = np.array([20, 30, 40, 50]) b = np.arange(4) print("a:", a) print("b", b) # 矩陣相減 c = a - b print("c = a - b", c) # 矩陣減1 c = c - 1 print("c = c - 1", c) # 矩陣中的每個元素的平方 print("b ** 2", b ** 2) # 範圍判斷 print("a < 35", a < 35)
結果
a: [20 30 40 50] b [0 1 2 3] c = a - b [20 29 38 47] c = c - 1 [19 28 37 46] b ** 2 [0 1 4 9] a < 35 [ True True False False]
import numpy as np A = np.array([[1, 1], [0, 1]]) B = np.array([[2, 0], [3, 4]]) print('---A----') print(A) print('---B----') print(B) # 矩陣之間對應元素相乘 print('---A * B----') print(A * B)
結果
---A---- [[1 1] [0 1]] ---B---- [[2 0] [3 4]] ---A * B---- [[2 0] [0 4]]
import numpy as np # The matrix product can be performed using the dot function or method A = np.array([[1, 1], [0, 1]]) B = np.array([[2, 0], [3, 4]]) print('---A----') print(A) print('---B----') print(B) print('---A * B----') print(A * B) # 矩陣相乘 print('---A.dot(B)----') print(A.dot(B)) # 矩陣相乘 print('---np.dot(A, B)----') print(np.dot(A, B))
結果
---A---- [[1 1] [0 1]] ---B---- [[2 0] [3 4]] ---A.dot(B)---- [[5 4] [3 4]] ---np.dot(A, B)---- [[5 4] [3 4]]
import numpy as np B = np.arange(3) print(B) # 求天然指數e的次冪,B中的元素爲e每一冪次方 print(np.exp(B))
結果
[0 1 2] [1. 2.71828183 7.3890561 ]
import numpy as np B = np.arange(3) print(B) # 對B中的每個元素求根號,即1/2次冪 print(np.sqrt(B))
結果
[0 1 2] [0. 1. 1.41421356]
示例一
import numpy as np a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) np.floor(a)
結果
[-2., -2., -1., 0., 1., 1., 2.]
示例二
import numpy as np # Return the floor of the input a = 10 * np.random.random((3, 4)) print(a) a = np.floor(a) print(a)
結果
[[6.6143481 9.9613796 1.30947854 5.6078685 ] [3.10948678 6.83076618 4.92651686 0.15127964] [3.3036663 9.44427669 5.25021126 7.66229507]] [[6. 9. 1. 5.] [3. 6. 4. 0.] [3. 9. 5. 7.]]
import numpy as np # Return the floor of the input a = 10 * np.random.random((3, 4)) print(a) a = np.floor(a) print(a) print('--------') # 2維矩陣 ==> 1維向量,水平方向進行轉化 # flatten the array print(a.ravel() )
結果
[[6.6143481 9.9613796 1.30947854 5.6078685 ] [3.10948678 6.83076618 4.92651686 0.15127964] [3.3036663 9.44427669 5.25021126 7.66229507]] [[6. 9. 1. 5.] [3. 6. 4. 0.] [3. 9. 5. 7.]] -------- [6. 9. 1. 5. 3. 6. 4. 0. 3. 9. 5. 7.]
說明
在這種方式下,行向量沒法直接轉化爲列向量
具體的轉化方式見:
[numpy] np.newaxis 如何將行向量轉換成列向量 - doufuxixi的博客 - CSDN博客
https://blog.csdn.net/doufuxixi/article/details/80356946
import numpy as np # Return the floor of the input a = 10 * np.random.random((3, 4)) print(a) # 向下取整 a = np.floor(a) print("a = np.floor(a)") print(a) # a 的轉置 print("--------a.T") print(a.T) print('--------b = a.ravel()') # a 水平方向鋪平 b = a.ravel() print(b) # b 的轉置 print('--------b.T') print(b.T)
結果
[[6.8977933 6.5823566 2.70240107 4.48524208] [0.96507135 4.58781425 6.2868975 7.39792458] [6.18095442 4.62072618 5.73384294 8.45966937]] a = np.floor(a) [[6. 6. 2. 4.] [0. 4. 6. 7.] [6. 4. 5. 8.]] --------a.T [[6. 0. 6.] [6. 4. 4.] [2. 6. 5.] [4. 7. 8.]] --------b = a.ravel() [6. 6. 2. 4. 0. 4. 6. 7. 6. 4. 5. 8.] --------b.T [6. 6. 2. 4. 0. 4. 6. 7. 6. 4. 5. 8.]
import numpy as np a = np.floor(10 * np.random.random((2, 2))) b = np.floor(10 * np.random.random((2, 2))) print('---a') print(a) print('---b') print(b) print('---np.vstack((a, b)') print(np.vstack((a, b)))
結果
---a [[4. 3.] [8. 6.]] ---b [[8. 2.] [6. 0.]] ---np.vstack((a, b) [[4. 3.] [8. 6.] [8. 2.] [6. 0.]]
import numpy as np a = np.floor(10 * np.random.random((2, 2))) b = np.floor(10 * np.random.random((2, 2))) print('---a') print(a) print('---b') print(b) print('---np.hstack((a, b)') print(np.hstack((a, b)))
結果
---a [[1. 9.] [3. 6.]] ---b [[9. 6.] [5. 0.]] ---np.hstack((a, b) [[1. 9. 9. 6.] [3. 6. 5. 0.]]
import numpy as np x = np.arange(16.0).reshape(2, 8) print(x) # 等個數分割爲2份 print(np.hsplit(x, 2)) # 等個數分割爲4份 print(np.hsplit(x, 4))
結果
[[ 0. 1. 2. 3. 4. 5. 6. 7.] [ 8. 9. 10. 11. 12. 13. 14. 15.]] [array([[ 0., 1., 2., 3.], [ 8., 9., 10., 11.]]), array([[ 4., 5., 6., 7.], [12., 13., 14., 15.]])] [array([[0., 1.], [8., 9.]]), array([[ 2., 3.], [10., 11.]]), array([[ 4., 5.], [12., 13.]]), array([[ 6., 7.], [14., 15.]])]
結果( 從新排版後)
[[ 0. 1. 2. 3. 4. 5. 6. 7.] [ 8. 9. 10. 11. 12. 13. 14. 15.]] [array([[ 0., 1., 2., 3.], [ 8., 9., 10., 11.]]), array([[ 4., 5., 6., 7.], [12., 13., 14., 15.]])] [array([[0., 1.], [8., 9.]]), array([[ 2., 3.], [10., 11.]]), array([[ 4., 5.], [12., 13.]]), array([[ 6., 7.], [14., 15.]])]
import numpy as np x = np.arange(16.0).reshape(8, 2) print(x) # 等數量分割爲2份 print(np.vsplit(x, 2)) # 等數量分割爲4份 print(np.vsplit(x, 4))
結果
[[ 0. 1.] [ 2. 3.] [ 4. 5.] [ 6. 7.] [ 8. 9.] [10. 11.] [12. 13.] [14. 15.]] [array([[0., 1.], [2., 3.], [4., 5.], [6., 7.]]), array([[ 8., 9.], [10., 11.], [12., 13.], [14., 15.]])] [array([[0., 1.], [2., 3.]]), array([[4., 5.], [6., 7.]]), array([[ 8., 9.], [10., 11.]]), array([[12., 13.], [14., 15.]])]
結果( 從新排版後)
[[ 0. 1.] [ 2. 3.] [ 4. 5.] [ 6. 7.] [ 8. 9.] [10. 11.] [12. 13.] [14. 15.]] [array([ [0., 1.], [2., 3.], [4., 5.], [6., 7.]]), array([ [ 8., 9.], [10., 11.], [12., 13.], [14., 15.]])] [array([[0., 1.], [2., 3.]]), array([[4., 5.], [6., 7.]]), array([[ 8., 9.], [10., 11.]]), array([[12., 13.], [14., 15.]])]
說明
複製就是使用「=」。使用「=」的時候,其實是傳遞的是對象的引用,當對象發生修改的時候,複製體也會發生同等的改變,不管何種改變。
import numpy as np #Simple assignments make no copy of array objects or of their data. a = np.arange(12) b = a # a and b are two names for the same ndarray object print (b is a) # b的形狀改變,a也跟着改變 b.shape = 3,4 print (a.shape) print (id(a)) print (id(b))
結果
True (3, 4) 1974659569504 1974659569504
說明
view至關於傳引用,view和原始數據共享一份數據,修改一個會影響另外一個。
import numpy as np a = np.arange(12) # The view method creates a new array object that looks at the same data. c = a.view() # 判斷c a 是否共用一個內存id print("c is a") print(c is a) # 查看a,c 的id print("id(a)") print(id(a)) print("id(c)") print(id(c)) # 對c 的形狀進行修改 c.shape = 2, 6 # a的形狀不發生改變 print("a.shape") print(a.shape) print("c.shape") print(c.shape) # 對c 的元素進行修改,a中的對應位置的元素也跟着修改 c[0, 4] = 1234 print(a) print("a") print(c) print("c")
結果
c is a False id(a) 34629952 id(c) 36175504 a.shape (12,) c.shape (2, 6) [ 0 1 2 3 1234 5 6 7 8 9 10 11] a [[ 0 1 2 3 1234 5] [ 6 7 8 9 10 11]] c
拓展閱讀
numpy中的copy和view - 大澤之國 - CSDN博客
https://blog.csdn.net/z0n1l2/article/details/83116775
import numpy as np a = np.arange(12) #The copy method makes a complete copy of the array and its data. # 徹底拷貝 d = a.copy() print (d is a) # 修改d 中的元素 d[0] = 9999 print (d) print (a)
結果
[9999 1 2 3 4 5 6 7 8 9 10 11] [ 0 1 2 3 4 5 6 7 8 9 10 11]
拓展閱讀
numpy中的copy和view - 大澤之國 - CSDN博客
https://blog.csdn.net/z0n1l2/article/details/83116775
python複製,淺拷貝,深拷貝理解 - Young的博客 - CSDN博客
https://blog.csdn.net/dearyangjie/article/details/71533615
Python中複製,淺拷貝,深拷貝的區別詳解 - H845165367的博客 - CSDN博客
https://blog.csdn.net/H845165367/article/details/79687387
import numpy as np # data = 100*np.sin(np.arange(20)).reshape(5, 4) # data = np.floor(data) # 5行4列 data = np.array( [[ 0., 84., 90., 14.,], [ -76., -96., -28., 65.,], [ 98., 41., -55., -100.,], [ -54., 42., 99., 65.,], [ -29., -97., -76., 14.,]] ) print("data") print(data) # 垂直方向上,找出最大值的位置 # 結果:[2 0 3 1],分別對應第0列的第2個,第1列的第0個,第2列的3個,第3列的第1個(這裏是與第3個等值,故取靠前的數值) ind = data.argmax(axis=0) print("ind") print(ind) # 在1行中,還原最大值的數據 data_max = data[ind, range(data.shape[1])] print("data_max") print(data_max)
結果
data [[ 0. 84. 90. 14.] [ -76. -96. -28. 65.] [ 98. 41. -55. -100.] [ -54. 42. 99. 65.] [ -29. -97. -76. 14.]] ind [2 0 3 1] data_max [98. 84. 99. 65.]
import numpy as np a = np.arange(0, 40, 10) print(a) # 對a中的數據進行重複,以a爲單位生成新的4行3列的矩陣 # 因爲a是1行4列的數據,所以最終生成 4*3 =12列數據 b = np.tile(a, (4, 3)) print(b) print(b.shape)
結果
[ 0 10 20 30] [[ 0 10 20 30 0 10 20 30 0 10 20 30] [ 0 10 20 30 0 10 20 30 0 10 20 30] [ 0 10 20 30 0 10 20 30 0 10 20 30] [ 0 10 20 30 0 10 20 30 0 10 20 30]] (4, 12)
示例一,axis=1
import numpy as np a = np.array([[4, 3, 5], [1, 2, 1]]) print(a) # 方式一 print('方式一--------b = np.sort(a, axis=1)') b = np.sort(a, axis=1) print(b) print('--------a') print(a) # 方式二 print('方式二--------a.sort(axis=1)') a.sort(axis=1) print(a)
結果
[[4 3 5] [1 2 1]] 方式一--------b = np.sort(a, axis=1) [[3 4 5] [1 1 2]] --------a [[4 3 5] [1 2 1]] 方式二--------a.sort(axis=1) [[3 4 5] [1 1 2]]
示例二,axis=0
import numpy as np a = np.array([[4, 3, 5], [1, 2, 1]]) print(a) # 方式一 print('方式一--------b = np.sort(a, axis=0)') b = np.sort(a, axis=0) print(b) print('--------a') print(a) # 方式二 print('方式二--------a.sort(axis=0)') a.sort(axis=0) print(a)
結果
[[4 3 5] [1 2 1]] 方式一--------b = np.sort(a, axis=0) [[1 2 1] [4 3 5]] --------a [[4 3 5] [1 2 1]] 方式二--------a.sort(axis=0) [[1 2 1] [4 3 5]]
argsort()函數是將x中的元素從小到大排列,提取其對應的index(索引),而後輸出到y
import numpy as np a = np.array([4, 3, 1, 2]) j = np.argsort(a) print('--------') print(j) print('--------') print(a[j])
結果
-------- [2 3 1 0] -------- [1 2 3 4]