What is the method for creating random arrays in numpy?
numpy offers various methods to create random arrays, with some of the most commonly used methods being:
- numpy.random.rand: Generates an array of the specified dimensions with elements ranging from [0, 1) following a uniform distribution.
import numpy as np
arr = np.random.rand(3, 4) # 创建一个3行4列的数组
print(arr)
- numpy.random.randn: Generate an array with the specified dimensions, where the elements follow a standard normal distribution with a mean of 0 and a standard deviation of 1.
import numpy as np
arr = np.random.randn(3, 4) # 创建一个3行4列的数组
print(arr)
- numpy.random.randint generates an array of integers with specified dimensions and values within a specific range.
import numpy as np
arr = np.random.randint(1, 10, size=(3, 4)) # 创建一个3行4列的数组,元素取值在1到10之间
print(arr)
- numpy.random.random_sample: Generates an array of a given dimension with elements ranging between 0 and 1 (excluding 1) following a uniform distribution.
import numpy as np
arr = np.random.random_sample((3, 4)) # 创建一个3行4列的数组
print(arr)
These are just some of the random array creation methods provided by numpy, there are other methods such as numpy.random.random, numpy.random.choice, etc. The specific method to choose depends on your needs.