0

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 

1 Answer 1

0

If you do arr[:,:,0], you will get the whole array that you want in 2D format. Which you can just reshape.

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])

You can just run whatever operation you want on this.

This is the operation you ran

np.sum(np.abs(arr[:,:,0].reshape(-1))) / 12
>>> 1.7000000000000002

This is the absolute mean value

np.mean(np.abs(arr[:,:,0].reshape(-1)))
>>> 1.02
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.