访问多维Numpy数组不同列的程序
先决条件: Numpy 模块
下面的文章讨论了我们如何访问多维 Numpy 数组的不同列。在这里,我们使用 Slicing 方法来获得所需的功能。
示例1:(访问Numpy数组的第一列和最后一列)
Python3
# Importing Numpy module
import numpy as np
# Creating a 3x3 Numpy array
arr = np.array([[11, 20, 3],
[89, 5, 66],
[71, 88, 39]])
print("Given Array :")
print(arr)
# Access the First and Last column of array
res_arr = arr[:,[0,2]]
print("\nAccessed Columns :")
print(res_arr)
Python3
# Importing Numpy module
import numpy as np
# Creating a 4x4 Numpy array
arr = np.array([[1, 20, 3, 1],
[40, 5, 66, 7],
[70, 88, 9, 11],
[80, 100, 50, 77]])
print("Given Array :")
print(arr)
# Access the Middle and Last column of array
res_arr = arr[:,[1,3]]
print("\nAccessed Columns :")
print(res_arr)
Python3
# Importing Numpy module
import numpy as np
# Creating a 3d (3X4X4) Numpy array
arr = np.array([[[21, 20, 3, 1],
[40, 5, 66, 7],
[70, 88, 9, 11],
[80, 100, 50, 77]],
[[65, 120, 53, 73],
[49, 50, 56, 11],
[81, 88, 34, 22],
[564,56, 76, 99]],
[[45, 85, 38, 455],
[40, 53, 69, 6],
[50, 528, 654, 11],
[54, 87, 78, 77]]])
print("Given Array :")
print(arr)
# Access the Last two columns of array
res_arr = arr[2,:,[2,3]]
print("\nAccessed Columns :")
print(res_arr)
Python3
# Importing Numpy module
import numpy as np
# Creating a 4D Numpy array
arr = np.array([
[
[
[1,2],
[3,4]
],
[
[5,6],
[7,8]
]
],
[
[
[9,10],
[11,12]
],
[
[13,14],
[15,16]
]
]
])
print("Given Array :")
print(arr)
# Access the First three columns of array
res_arr = arr[0,0,:,[0]]
print("\nAccessed Columns :")
print(res_arr)
输出:
Given Array :
[[11 20 3]
[89 5 66]
[71 88 39]]
Accessed Columns :
[[11 3]
[89 66]
[71 39]]
示例2:(访问Numpy数组的中间和最后一列)
蟒蛇3
# Importing Numpy module
import numpy as np
# Creating a 4x4 Numpy array
arr = np.array([[1, 20, 3, 1],
[40, 5, 66, 7],
[70, 88, 9, 11],
[80, 100, 50, 77]])
print("Given Array :")
print(arr)
# Access the Middle and Last column of array
res_arr = arr[:,[1,3]]
print("\nAccessed Columns :")
print(res_arr)
输出:
Given Array :
[[ 1 20 3 1]
[ 40 5 66 7]
[ 70 88 9 11]
[ 80 100 50 77]]
Accessed Columns :
[[ 20 1]
[ 5 7]
[ 88 11]
[100 77]]
例3:(访问Numpy数组的最后两列)
蟒蛇3
# Importing Numpy module
import numpy as np
# Creating a 3d (3X4X4) Numpy array
arr = np.array([[[21, 20, 3, 1],
[40, 5, 66, 7],
[70, 88, 9, 11],
[80, 100, 50, 77]],
[[65, 120, 53, 73],
[49, 50, 56, 11],
[81, 88, 34, 22],
[564,56, 76, 99]],
[[45, 85, 38, 455],
[40, 53, 69, 6],
[50, 528, 654, 11],
[54, 87, 78, 77]]])
print("Given Array :")
print(arr)
# Access the Last two columns of array
res_arr = arr[2,:,[2,3]]
print("\nAccessed Columns :")
print(res_arr)
输出:
Given Array :
[[[ 21 20 3 1]
[ 40 5 66 7]
[ 70 88 9 11]
[ 80 100 50 77]]
[[ 65 120 53 73]
[ 49 50 56 11]
[ 81 88 34 22]
[564 56 76 99]]
[[ 45 85 38 455]
[ 40 53 69 6]
[ 50 528 654 11]
[ 54 87 78 77]]]
Accessed Columns :
[[ 38 69 654 78]
[455 6 11 77]]
示例 4:(访问 4D Numpy 数组的第一列)
蟒蛇3
# Importing Numpy module
import numpy as np
# Creating a 4D Numpy array
arr = np.array([
[
[
[1,2],
[3,4]
],
[
[5,6],
[7,8]
]
],
[
[
[9,10],
[11,12]
],
[
[13,14],
[15,16]
]
]
])
print("Given Array :")
print(arr)
# Access the First three columns of array
res_arr = arr[0,0,:,[0]]
print("\nAccessed Columns :")
print(res_arr)
输出:
Given Array :
[[[[ 1 2]
[ 3 4]]
[[ 5 6]
[ 7 8]]]
[[[ 9 10]
[11 12]]
[[13 14]
[15 16]]]]
Accessed Columns :
[[1 3]]