Creating Arrays
The most direct way to create an array is np.array.
The input is often a list. The result is a NumPy array.
Vectors
A one-dimensional array is often used as a vector:
The shape is (3,), which means one axis with three values.
Matrices
A two-dimensional array is often used as a matrix:
The shape is (2, 3): two rows, three columns.
For a regular two-dimensional numerical array, every row must have the same length. If one row has fewer values, there is no single rectangular shape.
Create vectors and matrices
Ready to run.
Useful constructors
NumPy can create common arrays:
np.arange(5) produces values from 0 up to 4, like range(5) but as an
array.
grid = np.zeros((2, 3))
The tuple (2, 3) is a shape.
Choose clear starting data
When learning, prefer tiny arrays whose values you can inspect by eye. If you cannot explain a shape with a small example, a larger example will not help.
Which expression creates a NumPy array from the values 1, 2, and 3?
Answer it first, then check.
Hint
Pass one Python list containing the three values to np.array.
Solution
Use:
np.array([1, 2, 3])
The list supplies the values; np.array constructs the NumPy array.