python - counting non-zero elements within each row and within each column of a 2D numpy array -


I have an oval matrix that has mostly noose values ​​but sometimes it has zero value I need to do this Is: 1. 1.) Calculate the non-zero values ​​in each line and put that calculation in a variable which I can use in later operations, perhaps through row index again and during counting Repetition process 2.) In each column Count the non-zero values ​​and put that computation into one variable which I can use in later operations, possibly through column index and then calculates during the repetition process

For example, I need one thing that combines each line and then divides each row in the number of non-zero values ​​by each row, each line is displayed Report a different result for the order. And then I have to add each column and then divide the column amount by the number of non-zero values ​​in the column, reporting a different result for each column index. I also need to fix other things, but I should be easy even after finding out what I am listing here.

The code I am working with is below. You can see that I am creating an array of zeros and then I am popping it with a CSV file. Some rows will contain values ​​for all columns, but some of the last columns in the other rows will have fewer zeros, thus the problem described above will arise.

The last 5 lines of code below are returned from any other posting on this forum, and the last 5 lines of code return a printed list of one row / column indices for zero. But I do not know how to use the resultant information to generate the non-zero line calculation and the number of noozo columns described above can someone help me with this

  ANOVAInputMatrixValuesArray (0, Lane (TestIDs) for J in J = 0 range: TestID = str (TestIDs [J]) ReadOrWrite = 'Read' inputfile = (Directory, (TestIDs), 9] 'R') reader = csv.reader (inputfile) meter = 0 open in the reader for the line fileName = inputFileName directory = GetCurrentDirectory Learn Returned): If the meter is for & lt; 9: If the line [0] = 'TestID': ANOVAInputMatrixValuesArray [(J-1), m] = line [2] meter + = 1 inputfile.close () IndicesOfZeros = Index (ANOVAInputMatrixValuesArray.shape) locs = IndicesOfZeros [:, ANOVAInputMatrixValuesArray == 0] Points = hsplit (locs, lane (locs [0])) for pt in digits: print (',' .join (pt) for pt (p [0]) pt  
  import npy as np a = np.array ([[1, 0, 1], [2, 3, 4], [0, 0, 7]] column = (a! = 0) .sum (0) rows = (a! = 0) .sum (1)  

is an array of the same size as the variable (a! = 0) the original a and for all the code in it True is -zero elements

.sum (x) function tells elements on axes x . The true / incorrect element is the sum of true elements

variable column and rows In each column / row of your original array, there are non-zero (element! = 0) values:

  column = np.array ([2, 1, 3]) rows = np. The array ([2, 3, 1])  

Edit : The whole code (with some simplification in its original code) might look like this:

  ANOVAInputMatrixValuesArray = Zero (TestIDs, 9], Boat), TestID Enumerator for Jammu Use T (Testaidi): Reed Oregret = 'read' filename = Inputfailnam directory = GetCurrentDirectory (argues that the right directory) # directory or filename to get CSV file? [: 9] nonZeroCols = (ANOVAInputMatrixValuesArray; ANOVAInputMatrixValuesArray [j,:] = loadtxt (', usecols = (2,) csvfile, comment =' testId ', delimiter =): open (directory,' r ') as csvfile With! = 0) .sum (0) nonZeroRows = (ANOVAInputMatrixValuesArray! = 0) .sum (1)  

2 edit :

To get the meaning of all the columns / rows, use the following:

  colMean = a.sum (0) / (a! = 0) .sum (0) RowMean = a.sum (1) / (a! = 0) .sum (1)  

If there are no zero-zero elements in a column / row, what do you want to do Are you So we can customize the code to solve this problem.


Comments