Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by
Expecting that you have a huge arrangement of estimations and are utilizing some plotting capacity/function that takes XY-values as information/input. The capacity should plot the quantiles of the estimations against the relating quantiles of some dispersion (ordinary, uniform...).                                                

The subsequent plot allows us at that point to assess in our estimation/measurement follows the expected conveyance or not.

Both R and Matlab give instant capacities to this, yet I am considering what the cleanest technique for executing it in Python would be.
closed

4 Answers

0 votes
by (25.7k points)
selected by
 
Best answer
In Python, you can utilize the scipy.stats module to calculate the quantiles of a dataset and compare them with the quantiles of a desired distribution. Here's a general approach to achieve this:

Import the necessary libraries:

import numpy as np

import scipy.stats as stats

import matplotlib.pyplot as plt

Generate or obtain your dataset of measurements:

# Example dataset of measurements

data = np.random.normal(loc=0, scale=1, size=1000)

Calculate the quantiles of the dataset:

quantiles_data = np.linspace(0, 1, num=100)  # Quantiles range from 0 to 1

data_quantiles = np.quantile(data, quantiles_data)

Define the desired distribution and calculate its quantiles:

# Example: Normal distribution

distribution = stats.norm(loc=0, scale=1)

distribution_quantiles = distribution.ppf(quantiles_data)

Plot the quantiles:

plt.scatter(distribution_quantiles, data_quantiles)

plt.plot([np.min(distribution_quantiles), np.max(distribution_quantiles)],

         [np.min(distribution_quantiles), np.max(distribution_quantiles)],

         color='red', linestyle='--')

plt.xlabel('Quantiles of Desired Distribution')

plt.ylabel('Quantiles of Data')

plt.title('Quantile-Quantile Plot')

plt.show()

This code generates a scatter plot where the x-axis represents the quantiles of the desired distribution, and the y-axis represents the quantiles of the dataset. The red dashed line represents the ideal relationship where the quantiles of the data perfectly align with the quantiles of the desired distribution.

By observing the plot, you can assess whether the distribution of your measurements follows the expected distribution or deviates from it.
0 votes
by (26.4k points)
edited by

I think you can utilize scipy.stats.probplot. Check the documentation for more information.

import numpy as np 

import pylab 

import scipy.stats as stats

measurements = np.random.normal(loc = 20, scale = 5, size=100)   

stats.probplot(measurements, dist="norm", plot=pylab)

pylab.show()

Output:

   

Looking for a good python tutorial course? Join the python certification course and get certified.

For more details, do check out the below video tutorial...

          

0 votes
by (15.4k points)

To plot the quantiles of a dataset against the corresponding quantiles of a desired distribution in Python, you can use the probplot function from the scipy.stats module. Here's a concise example:

import scipy.stats as stats
import matplotlib.pyplot as plt

# Example dataset of measurements
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Define the desired distribution
distribution = stats.norm(loc=0, scale=1)

# Create a quantile-quantile plot
stats.probplot(data, dist=distribution, plot=plt)

# Display the plot
plt.show()

This code generates a quantile-quantile plot (probplot) by passing the dataset (data) and the desired distribution (distribution) as arguments to the function. The plot displays the quantiles of the dataset against the quantiles of the desired distribution. By examining the plot, you can assess whether the dataset follows the expected distribution or deviates from it.
0 votes
by (19k points)
You can use the probplot function from the scipy.stats module to plot the quantiles of a dataset against the corresponding quantiles of a desired distribution in Python. Here's the shortest way to achieve this:

import scipy.stats as stats

import matplotlib.pyplot as plt

data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

stats.probplot(data, plot=plt)

plt.show()

In this concise code, the probplot function is called directly on the dataset (data) with the plot parameter set to plt, which uses the current Matplotlib figure. Finally, plt.show() is called to display the quantile-quantile plot.

This short implementation allows you to quickly visualize and assess the adherence of your dataset to the expected distribution.

Related questions

Browse Categories

...