All Questions
Tagged with probability python
1,023
questions
494
votes
16
answers
461k
views
Find the similarity metric between two strings
How do I get the probability of a string being similar to another string in Python?
I want to get a decimal value like 0.9 (meaning 90%) etc. Preferably with standard Python and library.
e.g.
...
124
votes
10
answers
316k
views
How to calculate probability in a normal distribution given mean & standard deviation?
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 ...
72
votes
10
answers
117k
views
Multivariate normal density in Python?
Is there any python package that allows the efficient computation of the PDF (probability density function) of a multivariate normal distribution?
It doesn't seem to be included in Numpy/Scipy, and ...
62
votes
11
answers
180k
views
Normalizing a list of numbers in Python
I need to normalize a list of values to fit in a probability distribution, i.e. between 0.0 and 1.0.
I understand how to normalize, but was curious if Python had a function to automate this.
I'd ...
43
votes
2
answers
82k
views
scikit-learn return value of LogisticRegression.predict_proba
What exactly does the LogisticRegression.predict_proba function return?
In my example I get a result like this:
array([
[4.65761066e-03, 9.95342389e-01],
[9.75851270e-01, 2.41487300e-02],
[...
38
votes
6
answers
50k
views
How can I make a random choice according to probabilities stored in a list (weighted random distribution)?
Given a list of probabilities like:
P = [0.10, 0.25, 0.60, 0.05]
(I can ensure that the sum of all the variables in P is always 1)
How can I write a function that randomly returns a valid index, ...
36
votes
7
answers
53k
views
How do I simulate flip of biased coin?
In unbiased coin flip H or T occurs 50% of times.
But I want to simulate coin which gives H with probability 'p' and T with probability '(1-p)'.
something like this:
def flip(p):
'''this ...
36
votes
3
answers
52k
views
How to get a classifier's confidence score for a prediction in sklearn?
I would like to get a confidence score of each of the predictions that it makes, showing on how sure the classifier is on its prediction that it is correct.
I want something like this:
How sure is ...
30
votes
2
answers
111k
views
Plotting probability density function by sample with matplotlib [closed]
I want to plot an approximation of probability density function based on
a sample that I have; The curve that mimics the histogram behaviour. I can
have samples as big as I want.
30
votes
9
answers
13k
views
How to do weighted random sample of categories in python
Given a list of tuples where each tuple consists of a probability and an item I'd like to sample an item according to its probability. For example, give the list [ (.3, 'a'), (.4, 'b'), (.3, 'c')] I'd ...
28
votes
3
answers
76k
views
Defining a white noise process in Python
I need to draw samples from a white noise process in order to implement a particular integral numerically.
How do I generate this with Python (i.e., numpy, scipy, etc.)?
25
votes
12
answers
6k
views
Python - Is a dictionary slow to find frequency of each character?
I am trying to find a frequency of each symbol in any given text using an algorithm of O(n) complexity. My algorithm looks like:
s = len(text)
P = 1.0/s
freqs = {}
for char in text:
try:
...
25
votes
4
answers
13k
views
Is there a Python equivalent to R's sample() function?
I want to know if Python has an equivalent to the sample() function in R.
The sample() function takes a sample of the specified size from the elements of x using either with or without replacement.
...
23
votes
2
answers
21k
views
Fitting distributions, goodness of fit, p-value. Is it possible to do this with Scipy (Python)?
INTRODUCTION: I'm a bioinformatician. In my analysis which I perform on all human genes (about 20 000) I search for a particular short sequence motif to check how many times this motif occurs in each ...
23
votes
1
answer
3k
views
sampling multinomial from small log probability vectors in numpy/scipy
Is there a function in numpy/scipy that lets you sample multinomial from a vector of small log probabilities, without losing precision? example:
# sample element randomly from these log probabilities
...
22
votes
2
answers
7k
views
PyMC3 Bayesian Linear Regression prediction with sklearn.datasets
I've been trying to implement Bayesian Linear Regression models using PyMC3 with REAL DATA (i.e. not from linear function + gaussian noise) from the datasets in sklearn.datasets. I chose the ...
21
votes
3
answers
30k
views
How to compute the probability of a value given a list of samples from a distribution in Python?
Not sure if this belongs in statistics, but I am trying to use Python to achieve this. I essentially just have a list of integers:
data = [300,244,543,1011,300,125,300 ... ]
And I would like to know ...
21
votes
12
answers
21k
views
Probability distribution in Python
I have a bunch of keys that each have an unlikeliness variable. I want to randomly choose one of these keys, yet I want it to be more unlikely for unlikely (key, values) to be chosen than a less ...
19
votes
2
answers
25k
views
How does the predict_proba() function in LightGBM work internally?
This is in reference to understanding, internally, how the probabilities for a class are predicted using LightGBM.
Other packages, like sklearn, provide thorough detail for their classifiers. For ...
15
votes
15
answers
13k
views
Is this a good or bad 'simulation' for Monty Hall? How come? [closed]
Through trying to explain the Monty Hall problem to a friend during class yesterday, we ended up coding it in Python to prove that if you always swap, you will win 2/3 times. We came up with this:
...
14
votes
3
answers
15k
views
How to generate random numbers with predefined probability distribution?
I would like to implement a function in python (using numpy) that takes a mathematical function (for ex. p(x) = e^(-x) like below) as input and generates random numbers, that are distributed according ...
13
votes
6
answers
17k
views
How to choose keys from a python dictionary based on weighted probability? [duplicate]
I have a Python dictionary where keys represent some item and values represent some (normalized) weighting for said item. For example:
d = {'a': 0.0625, 'c': 0.625, 'b': 0.3125}
# Note that sum([v ...
13
votes
2
answers
4k
views
multinomial pmf in python scipy/numpy
Is there a built-in function in scipy/numpy for getting the PMF of a Multinomial? I'm not sure if binom generalizes in the correct way, e.g.
# Attempt to define multinomial with n = 10, p = [0.1, 0.1,...
13
votes
1
answer
1k
views
How to structure a program to work with minesweeper configurations
EDIT: This was a while ago and I've since got it working, if you'd like to see the code it's included at github.com/LewisGaul/minegaulerQt.
I'm trying to write a program to calculate probabilities ...
12
votes
10
answers
19k
views
How do I simulate biased die in python?
I want to simulate N-sided biased die?
def roll(N,bias):
'''this function rolls N dimensional die with biasing provided'''
# do something
return result
>> N=6
>> bias=( 0....
12
votes
3
answers
13k
views
Python: How to get the convolution of two continuous distributions?
Let X, Y be 2 random variables, with probability density functions pdf1 and pdf2.
Z = X + Y
Then the probability density function of Z is given by the convolution of pdf1 and pdf2. Since we can't ...
12
votes
4
answers
671
views
How to update a matrix of probabilities
I am trying to find/figure out a function that can update probabilities.
Suppose there are three players and each of them get a fruit out of a basket: ["apple", "orange", "...
11
votes
5
answers
40k
views
How to calculate conditional probability of values in dataframe pandas-python?
I want to calculate conditional probabilites of ratings('A','B','C') in ratings column.
company model rating type
0 ford mustang A coupe
1 chevy camaro B ...
11
votes
1
answer
52k
views
Distribution plot of an array
I have a numpy array containing float values in [-10..10]. I would like to plot a distribution-graph of the values, like this (here it is done for a binomial random variable) :
For example I would ...
11
votes
2
answers
4k
views
Creating Probability/Frequency Axis Grid (Irregularly Spaced) with Matplotlib
I'm trying to create a frequency curve plot, and I'm having trouble manipulating the axis to get the plot I want.
Here is an example of the desired grid/plot I am trying to create:
Here is what I ...
11
votes
2
answers
9k
views
Python plotting percentile contour lines of a probability distribution
Given a probability distribution with unknown functional form (example below), I like to plot "percentile-based" contour lines, i.e.,those that correspond to regions with an integral of 10%, 20%, ..., ...
8
votes
4
answers
5k
views
Computing a binomial probability for huge numbers
I want to compute binomial probabilities on python. I tried to apply the formula:
probability = scipy.misc.comb(n,k)*(p**k)*((1-p)**(n-k))
Some of the probabilities I get are infinite. I checked ...
8
votes
6
answers
9k
views
Random rounding to integer in Python
I am looking for a way to round a floating point number up or down to the next integer based on a probability derived from the numbers after the decimal point. For example the floating number 6.1 can ...
8
votes
2
answers
11k
views
Generating random numbers with a given probability density function
I want to specify the probability density function of a distribution and then pick up N random numbers from that distribution in Python. How do I go about doing that?
8
votes
1
answer
17k
views
Calculating Probability of a Random Variable in a Distribution in Python
Given a mean and standard-deviation defining a normal distribution, how would you calculate the following probabilities in pure-Python (i.e. no Numpy/Scipy or other packages not in the standard ...
8
votes
1
answer
8k
views
Python - modelling probability
I have a simple problem. I need a way to make a function which generates 0s in p percent cases and 1s in all other cases. I tried doing it with random.random() like this:
p = 0.40
def generate():
...
8
votes
4
answers
4k
views
Rosalind: Mendel's first law
I'm trying to solve the problem at http://rosalind.info/problems/iprb/
Given: Three positive integers k, m, and n, representing a population
containing k+m+n organisms: k individuals are ...
8
votes
4
answers
6k
views
Calculating pdf of Dirichlet distribution in python
I'd like to calculate the pdf for the Dirichlet distribution in python, but haven't been able to find code to do so in any kind of standard library. scipy.stats includes a long list of distributions ...
8
votes
1
answer
215
views
Probability that a formula fails in IEEE 754
On my computer, I can check that
(0.1 + 0.2) + 0.3 == 0.1 + (0.2 + 0.3)
evaluates to False.
More generally, I can estimate that the formula (a + b) + c == a + (b + c) fails roughly 17% of the time ...
7
votes
6
answers
14k
views
Is Pythons random.randint statistically random?
So I'm testing an calculating the probabilities of certain dice rolls, for a game.
The base case if that rolling one 10sided die.
I did a million samples of this, and ended up with the following ...
7
votes
3
answers
5k
views
Python, SimPy: How to generate a value from a triangular probability distribution?
I want to run a simulation that uses as parameter a value generated from a triangular probability distribution with lower limit A, mode B and and upper limit C. How can I generate this value in Python?...
7
votes
3
answers
24k
views
Calculate moments (mean, variance) of distribution in python
I have two arrays. x is the independent variable, and counts is the number of counts of x occurring, like a histogram. I know I can calculate the mean by defining a function:
def mean(x,counts):
...
7
votes
3
answers
8k
views
How to properly sample truncated distributions?
I am trying to learn how to sample truncated distributions. To begin with I decided to try a simple example I found here example
I didn't really understand the division by the CDF, therefore I ...
7
votes
2
answers
2k
views
Solving inverse problems with PyMC
Suppose we're given a prior on X (e.g. X ~ Gaussian) and a forward operator y = f(x). Suppose further we have observed y by means of an experiment and that this experiment can be repeated indefinitely....
7
votes
4
answers
4k
views
Python equivalent for MATLAB's normplot?
Is there a python equivalent function similar to normplot from MATLAB?
Perhaps in matplotlib?
MATLAB syntax:
x = normrnd(10,1,25,1);
normplot(x)
Gives:
I have tried using matplotlib & numpy ...
7
votes
5
answers
13k
views
Create constrained random numbers?
CLEANED UP TEXT:
How can I create m=5 random numbers that add upp to, say n=100. But, the first random number is say, 10 < x1 < 30, the second random nr is 5 < x2 < 20, the third random ...
7
votes
1
answer
7k
views
How can I sample a multivariate log-normal distribution in Python?
Using Python, how can I sample data from a multivariate log-normal distribution? For instance, for a multivariate normal, there are two options. Let's assume we have a 3 x 3 covariance matrix and a 3-...
7
votes
1
answer
3k
views
Probabalistic String Matching in Python
I'm in the process of writing a bot that places bets on the website Betfair using their Python API. I want to place bets on football (soccer) matches when they are in-play.
I've coded an XML feed to ...
6
votes
2
answers
29k
views
Plotting frequency distributions in python
I have a graph stored in an adjacency list format. I randomly select a bunch of nodes and note the number of neighbors each of them have. I now want to plot the distribution, and the way I do it right ...
6
votes
4
answers
18k
views
Python: Selecting numbers with associated probabilities [duplicate]
Possible Duplicates:
Random weighted choice
Generate random numbers with a given (numerical) distribution
I have a list of list which contains a series on numbers and there associated ...