Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中使用numpy计算矩阵的列方式_Python_Numpy_Matrix - Fatal编程技术网

在python中使用numpy计算矩阵的列方式

在python中使用numpy计算矩阵的列方式,python,numpy,matrix,Python,Numpy,Matrix,通过下面的程序,我试图计算每列“0”、“1”、“2”和“3”的出现次数。程序未按预期运行。我在某个地方读到,应该对矩阵进行切片,以便按列计算发生率,但我不确定如何进行。该程序是用python中的numpy编写的。如何使用numpy实现它 import numpy as np a=np.array([[ 2,1,1,2,1,1,2], #t1 is horizontal [1,1,2,2,1,1,1], [2,1,1,1,1,2,1], [3,3,3,2,3,3,3], [3,3,2,3,3,3

通过下面的程序,我试图计算每列“0”、“1”、“2”和“3”的出现次数。程序未按预期运行。我在某个地方读到,应该对矩阵进行切片,以便按列计算发生率,但我不确定如何进行。该程序是用python中的numpy编写的。如何使用numpy实现它

import numpy as np 
a=np.array([[ 2,1,1,2,1,1,2], #t1 is horizontal
[1,1,2,2,1,1,1],
[2,1,1,1,1,2,1],
[3,3,3,2,3,3,3],
[3,3,2,3,3,3,2],
[3,3,3,2,2,2,3],
[3,2,2,1,1,1,0]])
print(a)
i=0
j=0
two=0
zero=0
one=0
three=0
r=a.shape[0]
c=a.shape[1]

for i in range(1,r):
#print(repr(a))
for j in range(1,c):
    #sele=a[i,j]
    if (a[i,j]==0):
        zero+=1
    if (a[i,j]==1):
        one+=1
    if (a[i,j]==2):
        two+=1
    if (a[i,j]==3):
        three+=1
    if i==c-1:
        #print(zero)
        print(one)
        i+=0 
        j=j+1
        #print(two)
        #print(three)   
    i=i+1
    #print(zero)`
我还想以以下方式打印:

    column:         0 1 2 3 4 5 6
    occurrences:  0 0 0 0 0 0 0 1
                  1 1 3 2 2 4 3 1
                  2 2 1 3 4 1 2 2
                  3 4 3 2 1 2 2 2

下面是使用列表功能的代码

import numpy as np 
inputArr=np.array([[ 2,1,1,2,1,1,2],
                [1,1,2,2,1,1,1],
                [2,1,1,1,1,2,1],
                [3,3,3,2,3,3,3],
                [3,3,2,3,3,3,2],
                [3,3,3,2,2,2,3],
                [3,2,2,1,1,1,0]
                ])

occurance = dict()
toFindList = [0,1,2,3]
for col in range(len(inputArr)):
    collist = inputArr[:, col]
    collist = (list(collist))
    occurance['col_' + str(col)] = {}
    for num in toFindList:
        occurcount = collist.count(num)
        occurance['col_' + str(col)][str(num)] = occurcount

for key, value in occurance.iteritems():
    print key, value
输出:

col_2 {'1': 2, '0': 0, '3': 2, '2': 3}
col_3 {'1': 2, '0': 0, '3': 1, '2': 4}
col_0 {'1': 1, '0': 0, '3': 4, '2': 2}
col_1 {'1': 3, '0': 0, '3': 3, '2': 1}
col_6 {'1': 2, '0': 1, '3': 2, '2': 2}
col_4 {'1': 4, '0': 0, '3': 2, '2': 1}
col_5 {'1': 3, '0': 0, '3': 2, '2': 2}

下面是使用列表功能的代码

import numpy as np 
inputArr=np.array([[ 2,1,1,2,1,1,2],
                [1,1,2,2,1,1,1],
                [2,1,1,1,1,2,1],
                [3,3,3,2,3,3,3],
                [3,3,2,3,3,3,2],
                [3,3,3,2,2,2,3],
                [3,2,2,1,1,1,0]
                ])

occurance = dict()
toFindList = [0,1,2,3]
for col in range(len(inputArr)):
    collist = inputArr[:, col]
    collist = (list(collist))
    occurance['col_' + str(col)] = {}
    for num in toFindList:
        occurcount = collist.count(num)
        occurance['col_' + str(col)][str(num)] = occurcount

for key, value in occurance.iteritems():
    print key, value
输出:

col_2 {'1': 2, '0': 0, '3': 2, '2': 3}
col_3 {'1': 2, '0': 0, '3': 1, '2': 4}
col_0 {'1': 1, '0': 0, '3': 4, '2': 2}
col_1 {'1': 3, '0': 0, '3': 3, '2': 1}
col_6 {'1': 2, '0': 1, '3': 2, '2': 2}
col_4 {'1': 4, '0': 0, '3': 2, '2': 1}
col_5 {'1': 3, '0': 0, '3': 2, '2': 2}

这将为您提供所需的输出格式:

def col_unique(a):
    return np.sum(np.dstack([np.in1d(a,i).reshape(a.shape) for i in np.unique(a)]), axis = 0).T

这将为您提供所需的输出格式:

def col_unique(a):
    return np.sum(np.dstack([np.in1d(a,i).reshape(a.shape) for i in np.unique(a)]), axis = 0).T