每行内,并且2D numpy的阵列的各列内计数非零元素 [英] counting non-zero elements within each row and within each column of a 2D numpy array

查看:2137
本文介绍了每行内,并且2D numpy的阵列的各列内计数非零元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有主要包含非零值的numpy的矩阵,但偶尔会包含一个零值。我需要能够:结果
1。)计数每行中的非零值,并把该计成,我可以在后续操作中使用,可能是通过迭代过程结果,过程中通过行索引迭代和执行计算的可变
2。)计数在每列中的非零值,并把该计成,我可以在后续操作中使用,也许通过列索引迭代并且在迭代过程执行计算一个变量

例如,有一件事我需要做的是总结每一行,然后每行中非零值的个数除以每一行总和,并报告每个行索引的单独的结果。然后我需要总结每个列,然后除以非零值的数目列总和之列,也报告对于每列索引的独立的结果。我需要做其他事情,但它们应该很容易后,我知道如何做到这一点我在这里列出的东西。

在code,我有工作如下。你可以看到,我创造为零的数组,然后从一个CSV文件填充它。一些行的将包含值的所有列,但其他的行仍然有剩余的一些最后一栏的,这样就产生上述问题的一些零。

下面的

最后5行code都从这个论坛的另一个帖子,而那些最后5行code返回行/列指数零点的打印清单。但我不知道如何使用该结果信息建立上述非零的行数和列非零计数。谁能帮我这个?

  ANOVAInputMatrixValues​​Array =零([LEN(TestIDs),9],浮动)
J = 0
在范围Ĵ(0,len个(TestIDs)):
    TestID = STR(TestIDs [J]。)
    ReadOrWrite ='读'
    文件名=查找inputfilename
    目录= GetCurrentDirectory(参数返回正确的目录)
    inputfile中=开(目录,'R')
    读卡器= csv.reader(inputfile中)
    m = 0的
    在读者排:
        如果m&10 9:
            如果行[0] ='TestID'!
                ANOVAInputMatrixValues​​Array [(J-1)中,m] =行[2]
                M + = 1
    inputfile.close()IndicesOfZeros =指数(ANOVAInputMatrixValues​​Array.shape)
LOCS = IndicesOfZeros [:,ANOVAInputMatrixValues​​Array == 0]
PTS = hsplit(LOCS,LEN(LOCS [0]))
在分点:
    打印(','。加入(STR(P [0])在PT P))


解决方案

 导入numpy的是NP一个= np.array([[1,0,1],
              [2,3,4],
              [0,0,7]])列=(一个!= 0)的.sum(0)
行=(一个!= 0)的.sum(1)

变量(A!= 0)是相同的形状,原始的 A 的数组,它包含所有非零元素。

的.sum(x)功能在轴 X 总结的元素。 真/假元素的总和元素的数量。

变量包含非零数(元!= 0)中的每个值列/你原来的数组的行:

 列= np.array([2,1,3)
行= np.array([2,3,1])

修改:整code可能看起来像这样(在你原来的code一些简化):

  ANOVAInputMatrixValues​​Array =零([LEN(TestIDs),9],浮动)
对于j,TestID在历数(TestIDs):
    ReadOrWrite ='读'
    文件名=查找inputfilename
    目录= GetCurrentDirectory(参数返回正确的目录)
    #使用目录或文件名来获取CSV文件?
    开放(目录,'R')为csvfile:
        ANOVAInputMatrixValues​​Array [J ,:] = loadtxt(csvfile,评论='TestId',分隔符=';',usecols =(2))[:9]nonZeroCols =(ANOVAInputMatrixValues​​Array!= 0)的.sum(0)
nonZeroRows =(ANOVAInputMatrixValues​​Array!= 0)的.sum(1)

编辑2

要获得所有列/行的平均值,使用以下命令:

  colMean = a.sum(0)/(A!= 0)的.sum(0)
rowMean = a.sum(1)/(一个!= 0)的.sum(1)

你想要什么,如果有一列/行没有非零元素呢?那么我们就可以适应code键解决这样的问题。

I have a numpy matrix that contains mostly nonzero values, but that occasionally will contain a zero value. I need to be able to:
1.) count the non-zero values in each row, and put that count into a variable that I can use in subsequent operations, perhaps by iterating through row indices and performing the calculations during the iterative process
2.) count the non-zero values in each column, and put that count into a variable that I can use in subsequent operations, perhaps by iterating through column indices and performing the calculations during the iterative process

For example, one thing I need to do is to sum each row and then divide each row sum by the number of non-zero values in each row, reporting a separate result for each row index. And then I need to sum each column and then divide the column sum by the number of non-zero values in the column, also reporting a separate result for each column index. I need to do other things as well, but they should be easy after I figure out how to do the things that 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 populating it from a csv file. Some of the rows will contain values for all the columns, but other rows will still have some zeros remaining in some of the last columns, thus creating the problem described above.

The last 5 lines of code below are from another posting on this forum, and those last 5 lines of code return a printed list of row/column indices for the zeros. But I do not know how to use that resulting information to create the nonzero row counts and nonzero column counts described above. Can anyone help me with this?

ANOVAInputMatrixValuesArray=zeros([len(TestIDs),9],float)
j=0
for j in range(0,len(TestIDs)):
    TestID=str(TestIDs[j])
    ReadOrWrite='Read'
    fileName=inputFileName
    directory=GetCurrentDirectory(arguments that return correct directory)
    inputfile=open(directory,'r')
    reader=csv.reader(inputfile)
    m=0
    for row in reader:
        if m<9:
            if row[0]!='TestID':
                ANOVAInputMatrixValuesArray[(j-1),m]=row[2]
                m+=1
    inputfile.close()

IndicesOfZeros = indices(ANOVAInputMatrixValuesArray.shape) 
locs = IndicesOfZeros[:,ANOVAInputMatrixValuesArray == 0]
pts = hsplit(locs, len(locs[0]))
for pt in pts:
    print(', '.join(str(p[0]) for p in pt))

解决方案

import numpy as np

a = np.array([[1, 0, 1],
              [2, 3, 4],
              [0, 0, 7]])

columns = (a != 0).sum(0)
rows    = (a != 0).sum(1)

The variable (a != 0) is an array of the same shape as original a and it contains True for all non-zero elements.

The .sum(x) function sums the elements over the axis x. Sum of True/False elements is the number of True elements.

The variables columns and rows contain the number of non-zero (element != 0) values in each column/row of your original array:

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

EDIT: The whole code could look like this (with a few simplifications in your original code):

ANOVAInputMatrixValuesArray = zeros([len(TestIDs), 9], float)
for j, TestID in enumerate(TestIDs):
    ReadOrWrite = 'Read'
    fileName = inputFileName
    directory = GetCurrentDirectory(arguments that return correct directory)
    # use directory or filename to get the CSV file?
    with open(directory, 'r') as csvfile:
        ANOVAInputMatrixValuesArray[j,:] = loadtxt(csvfile, comments='TestId', delimiter=';', usecols=(2,))[:9]

nonZeroCols = (ANOVAInputMatrixValuesArray != 0).sum(0)
nonZeroRows = (ANOVAInputMatrixValuesArray != 0).sum(1)

EDIT 2:

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

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

What do you want to do if there are no non-zero elements in a column/row? Then we can adapt the code to solve such a problem.

这篇关于每行内,并且2D numpy的阵列的各列内计数非零元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆