提问人:Amina Umar 提问时间:11/14/2023 更新时间:11/14/2023 访问量:52
3D Numpy 数组所有列的平均绝对值
Mean absolute value along all columns of a 3D Numpy array
问:
在处理多维数组时,我总是发现自己处于混乱状态。 具有以下数组数组的成像,其中每个数组包含数据集中每个类(5 个类)的特征重要性分数(3 个特征)。该数据集总共包含 4 个样本。
arr = np.random.randn(5,4,3).round(1)
arr
array([[[ 0.7, -0.1, 0.6], # class 0 feature importances
[-0.8, -0.7, 1.4],
[ 1.4, -0.1, 1.4],
[-1.8, -1.2, -1.6]],
[[-0.3, 2.1, 0.5], # class 1 feature importances
[-1.2, 1.4, -0.4],
[ 0. , -1. , 0.8],
[-0.8, 2.3, 0.3]],
[[ 0.2, 0.6, -0.1], # class 2 feature importances
[-1.8, -0.2, 1.2],
[-0.5, 0.5, 1. ],
[ 1.3, 0.4, -2.6]],
[[-1. , 0.8, -0.4], # class 3 feature importances
[ 1.2, 1.5, -0.5],
[ 0.1, -0.5, 0.8],
[ 2.5, -1.6, -0.6]],
[[-1.2, 0.3, -0.9], # class 4 feature importances
[ 1. , -1. , -0.5],
[ 0.3, 1.4, 0.5],
[-2.3, 0.6, 0.2]]])
我对计算跨类的特征重要性的值感兴趣(overrall)。理想情况下,生成的 arrar 应该是 1 级,因为有三个特征:mean absolute
(3,)
Feature1 = sum( abs(0.7,-0.8, 1.4, -1.8, -0.3, -1.2, 0.0, -0.8, 0.2, -1.8, -0.5, 1.3,
-1.0, 1.2, 0.1, 2.5, -1.2, 1.0, 0.3,, -2.3) ) / 12 # n = 12
答:
0赞
Vishal Balaji
11/14/2023
#1
如果这样做,您将以 2D 格式获得所需的整个数组。你可以重塑它。arr[:,:,0]
arr[:,:,0].reshape(-1)
>>> array([ 0.7, -0.8, 1.4, -1.8, -0.3, -1.2, 0. , -0.8, 0.2, -1.8, -0.5, 1.3, -1. , 1.2, 0.1, 2.5, -1.2, 1. , 0.3, -2.3])
您可以对此运行任何您想要的操作。
这是您运行的操作
np.sum(np.abs(arr[:,:,0].reshape(-1))) / 12
>>> 1.7000000000000002
这是绝对平均值
np.mean(np.abs(arr[:,:,0].reshape(-1)))
>>> 1.02
0赞
ayekaunic
11/14/2023
#2
我认为您需要计算数组中每个元素的绝对值,然后沿表示类的轴取平均值。这是你如何做到的:
import numpy as np
arr = np.random.randn(5, 4, 3).round(1)
# Compute the mean absolute value along the axis representing the classes
mean_absolute_values = np.mean(np.abs(arr), axis=0)
# Compute the mean absolute value across all classes for each feature
result = np.mean(mean_absolute_values, axis=0)
print(result)
这将输出所需的结果:
[0.75 1.02 0.88]
评论