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 do I create a 2D array of zeros using Python and NumPy?
- How do I create a zero matrix using Python and Numpy?
- How do I use the NumPy transpose function in Python?
- How do I create a numpy array of zeros using Python?
- How can I check if a certain version of Python is compatible with SciPy?
- How do I use Python and SciPy to create a tutorial PDF?
- How can I use Python and SciPy to find the zeros of a function?
- How do I use the scipy ttest_ind function in Python?
- How do I use the trapz function in Python SciPy?
- How do I create an array of zeros with the same shape as an existing array using Python and NumPy?
See more codes...