在 NumPy 中返回沿给定轴的屏蔽数组元素的标准偏差

要返回掩码数组元素的标准偏差,请使用ma. MaskedArray.std()在 Numpy 中。使用轴参数设置轴。

返回数组元素的标准偏差,即分布分布的度量。默认情况下为展平数组计算标准偏差,否则在指定轴上计算。

轴参数是计算标准偏差的一个或多个轴。默认是计算展平数组的标准差。如果这是一个整数元组,则在多个轴上执行标准偏差,而不是像以前那样在单个轴或所有轴上执行。

dtype 是用于计算标准偏差的类型。对于整数类型的数组,默认值为 float64,对于浮点类型的数组,它与数组类型相同。

脚步

首先,导入所需的库 -

import numpy as np

importnumpy.maas ma

使用该方法创建一个包含 int 元素的数组-numpy.array()

arr = np.array([[55, 85, 68, 84], [67, 33, 39, 53], [29, 88, 51, 37], [56, 45, 99, 85]])

print("Array...\n", arr)

print("\nArray type...\n", arr.dtype)

获取数组的维度 -

print("Array Dimensions...\n",arr.ndim)

创建一个屏蔽数组并将其中一些屏蔽为无效 -

maskArr = ma.masked_array(arr, mask =[[1, 1, 0, 0], [ 0, 0, 1, 0], [0, 0, 0, 1], [0, 1, 0, 0]])

print("\nOur Masked Array\n", maskArr)

print("\nOur Masked Array type...\n", maskArr.dtype)

获取 Masked Array 的尺寸 -

print("\nOur Masked Array Dimensions...\n",maskArr.ndim)

获取蒙面阵列的形状 -

print("\nOur Masked Array Shape...\n",maskArr.shape)

获取 Masked Array 的元素数量 -

print("\nElements in the Masked Array...\n",maskArr.size)

要返回掩码数组元素的标准偏差,请使用 ma. 在 Numpy 中。使用轴参数设置轴 -MaskedArray.std()

res = maskArr.std(axis = 0)

print("\nResult..\n.", res)

示例

import numpy as np

importnumpy.maas ma

# Create an array with int elements using the numpy.array() method

arr = np.array([[55, 85, 68, 84], [67, 33, 39, 53], [29, 88, 51, 37], [56, 45, 99, 85]])

print("Array...\n", arr)

print("\nArray type...\n", arr.dtype)

# Get the dimensions of the Array

print("\nArray Dimensions...\n",arr.ndim)

# Create a masked array and mask some of them as invalid

maskArr = ma.masked_array(arr, mask =[[1, 1, 0, 0], [ 0, 0, 1, 0], [0, 0, 0, 1], [0, 1, 0, 0]])

print("\nOur Masked Array\n", maskArr)

print("\nOur Masked Array type...\n", maskArr.dtype)

# Get the dimensions of the Masked Array

print("\nOur Masked Array Dimensions...\n",maskArr.ndim)

# Get the shape of the Masked Array

print("\nOur Masked Array Shape...\n",maskArr.shape)

# Get the number of elements of the Masked Array

print("\nElements in the Masked Array...\n",maskArr.size)

# To return the standard deviation of the masked array elements, use the ma.MaskedArray.std() in Numpy

# The axis is set using the axis parameter

res = maskArr.std(axis = 0)

print("\nResult..\n.", res)

输出结果
Array...

[[55 85 68 84]

[67 33 39 53]

[29 88 51 37]

[56 45 99 85]]

Array type...

int64

Array Dimensions...

2

Our Masked Array

[[-- -- 68 84]

[67 33 -- 53]

[29 88 51 --]

[56 -- 99 85]]

Our Masked Array type...

int64

Our Masked Array Dimensions...

2

Our Masked Array Shape...

(4, 4)

Elements in the Masked Array...

16

Result..

. [15.965240019770729 27.5 19.871811414385174 14.854853303438128]

以上是 在 NumPy 中返回沿给定轴的屏蔽数组元素的标准偏差 的全部内容, 来源链接: utcz.com/z/297120.html

回到顶部