# Calculate poisson probability percentage

When you use the POISSON function in Excel (or in OpenOffice Calc), it takes two arguments:

- an integer
- an 'average' number

and returns a float.

In Python (I tried RandomArray and NumPy) it returns an array of random poisson numbers. What I really want is the percentage that this event will occur (it is a constant number and the array has every time different numbers - so is it an average?).

for example:

```
print poisson(2.6,6)
```

returns `[1 3 3 0 1 3]`

(and every time I run it, it's different).

The number I get from calc/excel is 3.19 (`POISSON(6,2.16,0)*100`

).

Am I using the python's poisson wrong (no pun!) or am I missing something?

Asked by:

**Brad116**| Posted: 28-01-2022

# Answer 1

`scipy`

has what you want

```
>>> scipy.stats.distributions
<module 'scipy.stats.distributions' from '/home/coventry/lib/python2.5/site-packages/scipy/stats/distributions.pyc'>
>>> scipy.stats.distributions.poisson.pmf(6, 2.6)
array(0.031867055625524499)
```

It's worth noting that it's pretty easy to calculate by hand, too.

Answered by:**Dainton704**| Posted: 01-03-2022

# Answer 2

It is easy to do by hand, but you can overflow doing it that way. You can do the exponent and factorial in a loop to avoid the overflow:

```
def poisson_probability(actual, mean):
# naive: math.exp(-mean) * mean**actual / factorial(actual)
# iterative, to keep the components from getting too large or small:
p = math.exp(-mean)
for i in xrange(actual):
p *= mean
p /= i+1
return p
```

Answered by: **Maria361**| Posted: 01-03-2022

# Answer 3

This page explains why you get an array, and the meaning of the numbers in it, at least.

Answered by:**Dainton726**| Posted: 01-03-2022

# Similar questions

python - How to calculate percentage probability using a pivot table in pandas

I am working with a dataframe with two columns. One column is coded for either "control" or "treatment" while the other column is "true" or "false". I was wondering how I could use a pivot table and/or groupby to calculate the probability of true or false given either the control or treatment group.

python - How to get the probability percentage in keras predicting model CNN

Here, I am getting the data as [0 1 0 0] or [0 0 0 1],---
I get it that it is telling me that [0 1 0 0] is label2,[0 0 0 1] is label4, [1 0 0 0] is label1, [0 0 1 0] is label3.
import pickle
from keras.preprocessing.sequence import pad_sequences
MAX_SEQUENCE_LENGTH = 1000
MAX_NB_WORDS = 20000
with open ('textsdata', 'rb') as fp:
texts = pickle.load(fp)
tokenizer = Tokenizer(num_words=MAX_NB_WORDS)
to...

python - How do i get percentage of probability of each GMM?

I trained GMM model using 2 classes. And I also tested some sample data from trained GMM model. Finally, I want to get a probability for each class's gmm.
I used sklearn GaussianMixture function named predict_proba. But it shows in array. How can I get it in percentage?
for i in range(len(models)):
gmm = models[i] #checking with each model one by one
scores = np.array(gmm.score(vector))
...

python - How to generate pseudo random string based on percentage probability?

I am new to python programming and currently working on python random module to generate pseudo random string based on probability.
I need to get random string based on probability that there should be 50% probability to get string "hello".
I have tried online to find solutions but not any luck and wasted much of time.
Please let me know if anyone has idea regarding my problem.

python - Assigning specific probability (e.g., 75%) that an item will appear using arrays to ensure exact percentage each trial

I am creating an oddball paradigm for an infant EEG study. The experiment shows a 7 minute clip of fantasia. During the video every 6-10 seconds a pair of vibrations are sent out (triggered by an auditory beep sound) with an ISI of 700ms between them and a duration of 100ms for each vibration. For the oddball experiment we’d like to set an expectation of paired stimulation by having x amount of pairs appear first (e.g., 5)...

python - How to calculate percentage probability using a pivot table in pandas

I am working with a dataframe with two columns. One column is coded for either "control" or "treatment" while the other column is "true" or "false". I was wondering how I could use a pivot table and/or groupby to calculate the probability of true or false given either the control or treatment group.

python - How to create probability density function graph using csv dictreader, matplotlib and numpy?

I'm trying to create a simple probability density function(pdf) graph using data from one column of a csv file using csv dictreader, matplotlib and numpy...
Is there an easy way to use CSV DictReader combined with numpy arrays? Below is code that doesn't work. The error message is TypeError: len() of unsized object, which I'm guessing is related to the fact that my data is not in numpy array format? Also my data ...

python - Probability exercise returning different result that expected

As an exercise I'm writing a program to calculate the odds of rolling 5 die with the same number. The idea is to get the result via simulation as opposed to simple math though. My program is this:
# rollFive.py
from random import *
def main():
n = input("Please enter the number of sims to run: ")
hits = simNRolls(n)
hits = float(hits)
n = float(n)
prob = hits/n
print "The odds of...

python - Pythonic way to select list elements with different probability

This question already has answers here:

list - Python, probability

My code is next:
with open("test.txt") as f_in:
for line in f_in:
for char in line:
frequencies[char] += 1
list= [(count, char) for char, count in frequencies.iteritems()]
This code open test.txt, read every line and "list" sign into form for example: [(3, 'a'),.........]. This means that in whole text file, there are three a and s...

python - True or false output based on a probability

Is there a standard function for Python which outputs True or False probabilistically based on the input of a random number from 0 to 1?
example of what I mean:
def decision(probability):
...code goes here...
return ...True or False...
the above example if given an input of, say, 0.7 will return True with a 70% probability and false with a 30% probability

numpy - Python: Matplotlib - probability plot for several data set

I have several data sets (distribution) as follows:
set1 = [1,2,3,4,5]
set2 = [3,4,5,6,7]
set3 = [1,3,4,5,8]
How do I plot a scatter plot with the data sets above with the y-axis being the probability (i.e. the percentile of the distribution in set: 0%-100% ) and the x-axis being the data set names?
in JMP, it is called 'Quantile Plot'.
Something like image attached:

Randomly selecting an object with a weighted probability in Python

This question already has answers here:

python - Scipy - Inverse Sampling Method from custom probability density function

I am trying to perform an inverse sampling from a custom probability density function (PDF). I am just wondering if this even possible, i.e. integrating the PDF, inverting the result and then solving it for a given uniform number. The PDF has the shape f(x, alpha, mean(x))=(1/Gamma(alpha+1)(x))((x*(alpha+1)/mean(x))^(alpha+1))exp(-(alpha+1)*(x/mean(x)) where x > 0. From the shape the only values sub-150 are relevant, and ...

scale - python additive probability

I've created a python 2.7 tkinter module which uses scale widget data (aquatic or terrestrial) to influence a tuple which selects between animals. The module sorts and displays the three animals (deer, eel, turtles) in ranked descending order upon clicking 'Submit' and activating the associated command.
from Tkinter import (N, S, E, W, BOTH, BOTTOM, END, FLAT, HORIZONTAL, LEFT, NO, RAISED, RIGHT, TOP, YES,...

python - Build in function for plotting bayes decision boundary given the probability function

Is there a function in python, that plots bayes decision boundary if we input a function to it? I know there is one in matlab, but I'm searching for some function in python. I know that one way to achieve this is to iterate over the points, but I am searching for a built-in function.
I have bivariate sample points on the axis, and I want to plot the decision boundary in order to classify them.

# Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python