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 use Python and SciPy to write a WAV file?
- How do I create a 2D array of zeros using Python and NumPy?
- How can I use Python Scipy to zoom in on an image?
- How do I use Python Numpy to read and write Excel (.xlsx) files?
- How do I use the scipy ttest_ind function in Python?
- How do I create a numpy array of zeros using Python?
- How can I use Python and SciPy to perform a Short-Time Fourier Transform?
- How can I use Python and SciPy to solve an ordinary differential equation?
- How do I use Python XlsxWriter to write a NumPy array to an Excel file?
- How can I use Python and SciPy to read and write WAV files?
See more codes...