Issue
I always get find myself in state of confusion when dealing with a multi-dimensional array. Imaging having the following array of arrays, where each array contains feature importance scores (3-features), for each class in the dataset (5-classes). The dataset contains 4 samples in all.
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]]])
I am interested in computing the mean absolute
value of feature importances across the classes (overrall). Ideally the resultant arrar should be a rank 1 (3,)
since there are three features:
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
Solution
I think you need to calculate the absolute values for each element in the array and then take the mean along the axis representing the classes. Here's how you can do it:
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)
This will output the desired result:
[0.75 1.02 0.88]
Answered By - ayekaunic
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.