Create array#
Here is discussed in details different approaches to create a array in numpy.
import numpy as np
From function#
You can set a custom rule for creating a numpy array by using np.fromfunction
. The first argument specifies a function that takes the index of the row and the index of the column and must return elements that typically depend on the indexes.
The following cell shows the process of generating a sequential array using this method.
np.fromfunction(lambda i, j: i*3 + j, (4,4))
array([[ 0., 1., 2., 3.],
[ 3., 4., 5., 6.],
[ 6., 7., 8., 9.],
[ 9., 10., 11., 12.]])