Create an array with zeros. |
np.zeros((3,8))
Output: array([[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.]]) |
Create an array with ones. |
np.ones((3,8))
Output: array([[1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1.]]) |
Create an array with even-spaced step values. |
np.arange(1, 30, 5)
Output: array([ 1, 6, 11, 16, 21, 26]) |
Create an evenly-spaced array with a specified number of values. |
np.linspace(0,4,8)
Output: array([0. , 0.57142857, 1.14285714, 1.71428571, 2.28571429,
2.85714286, 3.42857143, 4. ]) |
Create an entire array with a constant value. |
np.full((4,4),9)
Output: array([[9, 9, 9, 9],
[9, 9, 9, 9],
[9, 9, 9, 9],
[9, 9, 9, 9]]) |
Create an identity matrix. |
np.identity(3)
Output: array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]]) |
Create an identity matrix with m*n dimension. |
np.eye(3,5)
Output: array([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.]]) |
Create an identity matrix with the m*n dimension with step shift. |
np.eye(3, k=1)
Output: array([[0., 1., 0.],
[0., 0., 1.],
[0., 0., 0.]]) |
Create a diagonal matrix. |
np.diag([1,1,1])
Output: array([[1, 0, 0],
[0, 1, 0],
[0, 0, 1]]) |
Create an array with random integers. |
np.random.randint(2,8,3)
Output: array([2, 6, 3]) |
Create an array between [0, 1). |
np.random.rand(5,2)
Output: array([[0.52620975, 0.27200853],
[0.04753095, 0.38419669],
[0.29254718, 0.66309665],
[0.09115936, 0.62305064],
[0.7984203 , 0.39769068]]) |
Create an array with a mean 0 and a standard deviation of 1. |
np.random.randn(2,3)
Output: array([[-0.98863765, 0.33808866, 0.07083797],
[-0.58465781, -1.53241981, -1.03018067]]) |
Create a random array in the [0.0, 1.0) interval |
np.random.random_sample(10)
Output: array([0.33360613, 0.54360933, 0.61117749, 0.52978991, 0.70083391,
0.61659811, 0.87412798, 0.62451739, 0.03659576, 0.12249504])
np.random.random(10)
Output: array([0.79213708, 0.65407051, 0.70394522, 0.72384937, 0.80480269,
0.51195845, 0.00852462, 0.571088 , 0.59704964, 0.74560852]) |