A simple problem, but I can't get it to work. I want to calculate the percentage of the number that appears in the list of arrays, and output this percentage accordingly. I have a list of arrays that looks like this:
import numpy as np
listvalues = []
arr1 = np.array([0, 0, 2])
arr2 = np.array([1, 1, 2, 2])
arr3 = np.array([0, 2, 2])
listvalues.append(arr1)
listvalues.append(arr2)
listvalues.append(arr3)
listvalues
>[array([0, 0, 2]), array([1, 1, 2, 2]), array([0, 2, 2])]
Now I count the occurrences using collections, which returns a list of collections. Contrast:
import collections
counter = []
for i in xrange(len(listvalues)):
counter.append(collections.Counter(listvalues[i]))
counter
>[Counter({0: 2, 2: 1}), Counter({1: 2, 2: 2}), Counter({0: 1, 2: 2})]
The result I'm looking for is an array with 3 columns representing values from 0 to 2 and len (listvalues) of the rows. Each cell should be filled with a percentage of this value in the array:
# Result
66.66 0 33.33
0 50 50
33.33 0 66.66
So, 0 arises 66.66% in array 1, 0% in array 2 and 33.33% in array 3, etc.
What would be the best way to achieve this? Many thanks!