python-scipyHow can I use Python, SciPy and stats.rvs to generate random variables?
Python, SciPy and stats.rvs can be used together to generate random variables. To do this, the following steps can be taken:
-
Import the required libraries.
import numpy as np import scipy.stats as stats
-
Generate random variables using stats.rvs.
# Generate a random variable with a normal distribution normal_dist = stats.rvs(size=1000, loc=0, scale=1)
Generate a random variable with a uniform distribution
uniform_dist = stats.rvs(size=1000, loc=0, scale=10)
3. Use the generated variables to plot a histogram.
```python
import matplotlib.pyplot as plt
# Plot the normal distribution
plt.hist(normal_dist, bins=20, color='c')
plt.axvline(normal_dist.mean(), color='b', linestyle='solid', linewidth=2)
plt.axvline(normal_dist.mean() + normal_dist.std(), color='b', linestyle='dashed', linewidth=2)
plt.axvline(normal_dist.mean() - normal_dist.std(), color='b', linestyle='dashed', linewidth=2)
plt.show()
# Plot the uniform distribution
plt.hist(uniform_dist, bins=20, color='c')
plt.axvline(uniform_dist.mean(), color='b', linestyle='solid', linewidth=2)
plt.axvline(uniform_dist.mean() + uniform_dist.std(), color='b', linestyle='dashed', linewidth=2)
plt.axvline(uniform_dist.mean() - uniform_dist.std(), color='b', linestyle='dashed', linewidth=2)
plt.show()
Helpful links
More of Python Scipy
- How can I check if a certain version of Python is compatible with SciPy?
- How can I use Python and SciPy to find the zeros of a function?
- How can I install and use SciPy on Ubuntu?
- How can I use Python and Numpy to parse XML data?
- How can I use Python and SciPy to generate a Voronoi diagram?
- How do I install and use Python-Scipy on Ubuntu 20.04?
- How do I use Python and SciPy to create a tutorial PDF?
- How do I use Python Numpy to read and write Excel (.xlsx) files?
- How do I use the trapz function in Python SciPy?
- How can I use Python Scipy to zoom in on an image?
See more codes...