Arrays derived from NumPy functions

If you need a vector or a matrix characterized by a particular numeric series (zeros, ones, ordinal numbers, and particular statistical distributions), NumPy functions provide you with quite a large range of choices.

First, creating a NumPy array of ordinal values (integers) is straightforward if you use the arange function, which returns integer values in a given interval (usually from zero) and reshapes its results:

In: import numpy as npIn: ordinal_values = np.arange(9).reshape(3,3)    ordinal_valuesOut: array([[0, 1, 2],            [3, 4, 5],            [6, 7, 8]])

If the array has to be reversed in the order of values, use the following command:

In: np.arange(9)[::-1]Out: array([8, 7, 6, 5, 4, 3, 2, 1, 0])  

If the integers ...

Get Python Data Science Essentials - Third Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.