Calculating column wise for a matrix using numpy in python -


by following program, trying calculate number of occurance of '0','1','2',and '3' each column. program not working desired. read somewhere slicing of matrix should done computing occurance column wise not sure how it. program written using numpy in python. how using numpy?

import numpy np  a=np.array([[ 2,1,1,2,1,1,2], #t1 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]  in range(1,r): #print(repr(a)) 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)` 

also want print in following manner:

    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 

here code using list functionality

import numpy 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] col in range(len(inputarr)):     collist = inputarr[:, col]     collist = (list(collist))     occurance['col_' + str(col)] = {}     num in tofindlist:         occurcount = collist.count(num)         occurance['col_' + str(col)][str(num)] = occurcount  key, value in occurance.iteritems():     print key, value 

output:

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} 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

python - Error: Unresolved reference 'selenium' What is the reason? -

asp.net ajax - Jquery scroll to element just goes to top of page -