I have a conjunctive probability mass function array, with shape, for example (1,2,3,4,5,6) and I want to calculate the probability table, conditional to a value for some of the dimensions (export the cpts), for decision-making purposes.
The code I came up with at the moment is the following (the input is the dictionary "vdict" of the form {'variable_1': value_1, 'variable_2': value_2 ... } )
for i in vdict:
dim = self.invardict.index(i) # The index of the dimension that our Variable resides in
val = self.valdict[i][vdict[i]] # The value we want it to be
d = d.swapaxes(0, dim)
**d = array([d[val]])**
d = d.swapaxes(0, dim)
...
So, what I currently do is:
- I translate the variables to the corresponding dimension in the cpt.
- I swap the zero-th axis with the axis I found before.
- I replace whole 0-axis with just the desired value.
I put the dimension back to its original axis.
Now, the problem is, in order to do step 2, I have (a.) to calculate a subarray and (b.) to put it in a list and translate it again to array so I'll have my new array.
Thing is, stuff in bold means that I create new objects, instead of using just the references to the old ones and this, if d is very large (which happens to me) and methods that use d are called many times (which, again, happens to me) the whole result is very slow.
So, has anyone come up with an idea that will subtitude this little piece of code and will run faster? Maybe something that will allow me to calculate the conditionals in place.
Note: I have to maintain original axis order (or at least be sure on how to update the variable to dimensions dictionaries when an axis is removed). I'd like not to resort in custom dtypes.