Home:ALL Converter>Average non-zero elements in a numpy array (2D)

Average non-zero elements in a numpy array (2D)

Ask Time:2021-09-22T05:39:34         Author:Jesh Kundem

Json Formatter

I have a numpy 2D array, something similar to follows, and I am looking to average the non zero values along the columns.

0 0 0 0
0 2 5 0
1 5 8 1
1 5 8 2
0 2 5 0
0 0 0 0 

Output:

[1/2 14/4 26/4 3/2]

This question has a solution for numpy matrix, but I have a numpy array. Any thoughts would be greatly appreciated.

Author:Jesh Kundem,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/69275843/average-non-zero-elements-in-a-numpy-array-2d
yy