Skip to main content

12.6) More Ways Of Filling An Array


NumPy has a function called arange() equivalent to the range() function, but which creates an array of integers or floats:

>>> np.arange(10)     # array of integers
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.arange(10.0)   # array of floats
array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.])

NumPy also has its own implementation of the random functions. For example, you can create a specifically shaped array of random numbers with the random.random() function:

>>> a = np.random.random((2,3))
>>> a
array([[ 0.55205346,  0.71637091,  0.11048732],
       [ 0.34418206,  0.52542643,  0.64194073]])