返回两个带掩码的三维 NumPy 数组的外积


要返回两个三维掩码数组的外积,请在 Python NumPy 中使用 **ma.outer()** 方法。第一个参数是输入向量。如果输入不是一维的,则会将其展平。第二个参数是第二个输入向量。如果输入不是一维的,则会将其展平。

掩码数组是标准 numpy.ndarray 和掩码的组合。掩码要么是 nomask(表示关联数组的没有值无效),要么是一个布尔数组,它决定关联数组的每个元素的值是否有效。

步骤

首先,导入所需的库:

import numpy as np

使用 numpy.arange() 方法创建 Array1,这是一个包含整型元素的三维数组:

arr1 = np.arange(24).reshape((2,3,4))
print("Array1...
", arr1) print("
Array type...
", arr1.dtype)

创建掩码数组 1:

arr1 = ma.array(arr1)

掩码数组 1:

arr1[0, 0, 1] = ma.masked

显示掩码数组 1:

print("
Masked Array1...
",arr1)

使用 numpy.arange() 方法创建另一个包含整型元素的三维数组 Array2:

arr2 = np.arange(24).reshape((2,3,4))
print("
Array2...
", arr2) print("
Array type...
", arr2.dtype)

创建掩码数组 2:

arr2 = ma.array(arr2)

掩码数组 2:

arr2[0, 1, 2] = ma.masked
arr2[1, 2, 2] = ma.masked

显示掩码数组 2:

print("
Masked Array2...
",arr2)

要返回两个掩码数组的外积,请在 Python NumPy 中使用 ma.outer() 方法。

print("
Result of outer product (3D arrays)...
",np.ma.outer(arr1, arr2))

示例

import numpy as np
import numpy.ma as ma

# Array 1
# Creating a 3D array with int elements using the numpy.arange() method
arr1 = np.arange(24).reshape((2,3,4))
print("Array1...
", arr1) print("
Array type...
", arr1.dtype) # Get the dimensions of the Array print("
Array Dimensions...
",arr1.ndim) # Get the shape of the Array print("
Our Array Shape...
",arr1.shape) # Get the number of elements of the Array print("
Elements in the Array...
",arr1.size) # Create a masked array arr1 = ma.array(arr1) # Mask Array1 arr1[0, 0, 1] = ma.masked # Display Masked Array 1 print("
Masked Array1...
",arr1) # Array 2 # Creating another 3D array with int elements using the numpy.arange() method arr2 = np.arange(24).reshape((2,3,4)) print("
Array2...
", arr2) print("
Array type...
", arr2.dtype) # Get the dimensions of the Array print("
Array Dimensions...
",arr2.ndim) # Get the shape of the Array print("
Our Array Shape...
",arr2.shape) # Get the number of elements of the Array print("
Elements in the Array...
",arr2.size) # Create a masked array arr2 = ma.array(arr2) # Mask Array2 arr2[0, 1, 2] = ma.masked arr2[1, 2, 2] = ma.masked # Display Masked Array 2 print("
Masked Array2...
",arr2) # To return the outer product of two masked arrays, use the ma.outer() method in Python Numpy print("
Result of outer product (3D arrays)...
",np.ma.outer(arr1, arr2))

输出

Array1...
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]

[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]

Array type...
int64

Array Dimensions...
3

Our Array Shape...
(2, 3, 4)

Elements in the Array...
24

Masked Array1...
[[[0 -- 2 3]
[4 5 6 7]
[8 9 10 11]]

[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]

Array2...
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]

[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]

Array type...
int64

Array Dimensions...
3

Our Array Shape...
(2, 3, 4)

Elements in the Array...
24

Masked Array2...
[[[0 1 2 3]
[4 5 -- 7]
[8 9 10 11]]

[[12 13 14 15]
[16 17 18 19]
[20 21 -- 23]]]

Result of outer product (3D arrays)...
[[0 0 0 0 0 0 -- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -- 0]
[-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --]
[0 2 4 6 8 10 -- 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 -- 46]
[0 3 6 9 12 15 -- 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 -- 69]
[0 4 8 12 16 20 -- 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 -- 92]
[0 5 10 15 20 25 -- 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 --
115]
[0 6 12 18 24 30 -- 42 48 54 60 66 72 78 84 90 96 102 108 114 120 126 --
138]
[0 7 14 21 28 35 -- 49 56 63 70 77 84 91 98 105 112 119 126 133 140 147
-- 161]
[0 8 16 24 32 40 -- 56 64 72 80 88 96 104 112 120 128 136 144 152 160
168 -- 184]
[0 9 18 27 36 45 -- 63 72 81 90 99 108 117 126 135 144 153 162 171 180
189 -- 207]
[0 10 20 30 40 50 -- 70 80 90 100 110 120 130 140 150 160 170 180 190
200 210 -- 230]
[0 11 22 33 44 55 -- 77 88 99 110 121 132 143 154 165 176 187 198 209
220 231 -- 253]
[0 12 24 36 48 60 -- 84 96 108 120 132 144 156 168 180 192 204 216 228
240 252 -- 276]
[0 13 26 39 52 65 -- 91 104 117 130 143 156 169 182 195 208 221 234 247
260 273 -- 299]
[0 14 28 42 56 70 -- 98 112 126 140 154 168 182 196 210 224 238 252 266
280 294 -- 322]
[0 15 30 45 60 75 -- 105 120 135 150 165 180 195 210 225 240 255 270 285
300 315 -- 345]
[0 16 32 48 64 80 -- 112 128 144 160 176 192 208 224 240 256 272 288 304
320 336 -- 368]
[0 17 34 51 68 85 -- 119 136 153 170 187 204 221 238 255 272 289 306 323
340 357 -- 391]
[0 18 36 54 72 90 -- 126 144 162 180 198 216 234 252 270 288 306 324 342
360 378 -- 414]
[0 19 38 57 76 95 -- 133 152 171 190 209 228 247 266 285 304 323 342 361
380 399 -- 437]
[0 20 40 60 80 100 -- 140 160 180 200 220 240 260 280 300 320 340 360
380 400 420 -- 460]
[0 21 42 63 84 105 -- 147 168 189 210 231 252 273 294 315 336 357 378
399 420 441 -- 483]
[0 22 44 66 88 110 -- 154 176 198 220 242 264 286 308 330 352 374 396
418 440 462 -- 506]
[0 23 46 69 92 115 -- 161 184 207 230 253 276 299 322 345 368 391 414
437 460 483 -- 529]]

更新于:2022年2月22日

231 次浏览

启动您的 职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.