Creation in NumPy
This lesson helps you learn how to create a NumPy array in different ways.
For using numpy, import the numpy library.
import numpy
Create an Array of Zeros
To create a numpy array containing zeros, write:
np.zeros(size)
To create an array of size 9 write:
np.zeros(9)
Here is how this array is stored in memory:
┌───┬───┬───┬───┬───┬───┬───┬───┬───┐
Z │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │
└───┴───┴───┴───┴───┴───┴───┴───┴───┘
Press + to interact
import numpy as npZ=np.zeros(9)print(Z)
Create an Array of Ones
To create a numpy array containing ones, write:
np.ones(size)
.
To create an array of size 9 write:
np.ones(9)
Here is how this array is stored in memory:
┌───┬───┬───┬───┬───┬───┬───┬───┬───┐
Z │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │
...