How to calculate probability in normal distribution given mean, std in Python? I can always explicitly code my own function according to the definition like the OP in this question did: Calculating Probability of a Random Variable in a Distribution in Python

Just wondering if there is a library function call will allow you to do this. In my imagine it would like this:

```nd = NormalDistribution(mu=100, std=12)
p = nd.prob(98)
```

There is a similar question in Perl: How can I compute the probability at a point given a normal distribution in Perl?. But I didn't see one in Python.

Numpy has a random.normal function, but it's like sampling, not exactly what I want.

Apr 5, 2022 1,624 views

## 1 answer to this question.

This is how you do it.

```>>> import scipy.stats
>>> scipy.stats.norm(0, 1)
<scipy.stats.distributions.rv_frozen object at 0x928352c>
>>> scipy.stats.norm(0, 1).pdf(0)
0.3989422804014327
>>> scipy.stats.norm(0, 1).cdf(0)
0.5
>>> scipy.stats.norm(100, 12)
<scipy.stats.distributions.rv_frozen object at 0x928352c>
>>> scipy.stats.norm(100, 12).pdf(98)
0.032786643008494994
>>> scipy.stats.norm(100, 12).cdf(98)
0.43381616738909634
>>> scipy.stats.norm(100, 12).cdf(100)
0.5```

[One thing to keep in mind — this is just a suggestion — the parameter passing is a little broad. Because of how the code is written, if you type scipy.stats.norm(mean=100, std=12) instead of scipy.stats.norm(100, 12) or scipy.stats.norm(loc=100, scale=12), it will accept it but silently disregard the extra keyword arguments and return the default (0,1).]

Another Way:
Scipy.stats is a good library for Python. To provide a different perspective, you can compute it directly using

```import math
def normpdf(x, mean, sd):
var = float(sd)**2
denom = (2*math.pi*var)**.5
num = math.exp(-(float(x)-float(mean))**2/(2*var))
return num/denom```

You can catch

```>>> normpdf(7,5,5)
0.07365402806066466
>>> norm(5,5).pdf(7)
0.073654028060664664```

Elevate Your Expertise with Our Machine Learning Certification Program!

• 5,480 points

## How to calculate ctc probability for given input and expected output?

The loss for a batch is defined ...READ MORE

## How to simulate first passage time probability in python for a random walk?

To begin with, you're now computing fp ...READ MORE

## How to load a model from an HDF5 file in Keras?

Hi@akhtar, If you stored the complete model, not ...READ MORE

## How to get a regression summary in scikit-learn like R does?

In sklearn, there is no R type ...READ MORE

## how do i change string to a list?

suppose you have a string with a ...READ MORE

## how can i randomly select items from a list?

You can also use the random library's ...READ MORE

+1 vote

## how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE