Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Determine probabilites based on an array python

Let’s say I have and array.shape = (296,3)

Where the first collumn contais 0 or 1, the second collumn contains 0,1 or 2 and the final collumn contains also 0,1 or 2.

I want to know how I can calculate all the probabilites for each one of the 18 possible combinations (2x3x3) of the 3 collumns. Possible sequences are [0,0,0];[0,0,1];etc… .

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use numpy.unique to collect the unique sequences, along with their counts.

>>> (unique, counts) = numpy.unique(data, return_counts=True, axis=0)
>>> unique
array([0, 1, 1],
      [0, 1, 2],
      [1, 1, 1],
       ...])
>>> counts
array([2, 2, 1, ...])

Therefore the probability of the i-th element from uniques is its corresponding value from counts over the number of rows from your original data. In other words for example a count of 28 over 296 rows would be a probability of ~9.46%.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading