Problem: from two input arrays I want to output an array with a frequency of True values (from input_2) corresponding to each value of input_1.
import numpy as np
from scipy.stats import itemfreq
input_1 = np.array([3,6,6,3,6,4])
input_2 = np.array([False, True, True, False, False, True])
In this example, the output I want is:
output_1 = np.array([0,2,2,0,2,1])
My current approach involves editing input_1, so only values matching True are left:
locs=np.where(input_2==True,input_1,0)
Then, counting the frequency of each answer, creating a dictionary and replacing the corresponding enter_1 keys with values (true frequencies).
loc_freq = itemfreq(locs)
dic = {}
for key,val in loc_freq:
dic[key]=val
print dic
for k, v in dic.iteritems():
input_1[input_1==k]=v
which outputs [3,2,2,3,2,1].
The problem here is twofold: 1) it still does not do anything with keys that are not in the dictionary (and therefore should be replaced with 0). For example, how can I turn 3s into 0s? 2) It seems very inefficient / inefficient. Is there a better way to approach this?