This post will give you a better hands on with creating numpy array. At the end of the post, you will have clarity on different ways of creating numpy arrays with helpful visualizations. If you are a beginner in Data Analytics or Data Science field, you must have in depth understanding of numpy package of python.
Before we start, let’s understand what numpy is all about.
What exactly is a Python NumPy Array?
NumPy provides enriched set of functionalities over traditional array or list. It is useful in performing certain operations on array like linear algebraic operations, mathematical aggregations, logical operations as well as slicing and dicing the array.
Any one who is mainly dealing with any Data Analysis, Machine Learning or Deep Learning related task in python, learning numpy is a very first step for them.
Here are some of the glimpse about numpy arrays,
- Python numpy array is an efficient multi-dimensional container of values of same numeric type
- It is a powerful wrapper of n-dimensional arrays in python which provides convenient way of performing data manipulations
- This library contains methods and functionality to solve the math problems using linear algebra
- Operations on numpy arrays are very fast as it is natively written in C language
- Many libraries of python data ecosystem (like pandas, scipy, sklearn etc..) are using numpy as a base library
In this tutorial we’ll mainly focus on various ways of creating numpy array with python3.
Firstly, we will start with the installation step itself.
How to Install NumPy with Python 3?
If you are using standalone Python 3 distribution, you can use following command to install numpy package
pip install numpy
Generally Anaconda Distribution comes with numpy package. But in case it is not there, you can use following command to install numpy with Anaconda 3 Distribution.
conda install numpy
Ways of creating numpy arrays?
There are mainly two ways to create numpy arrays.
- One way to make numpy array is using python list or nested list
- We can also use some numpy built-In methods
Creating numpy array from python list or nested lists
You can create numpy array casting python list. Simply pass the python list to np.array()
method as an argument and you are done. This will return 1D numpy array or a vector.
In case you want to create 2D numpy array or a matrix, simply pass python list of list to np.array()
method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | # Import NumPy and Check Version import numpy as np np.__version__ #>> 1.14.0 # Creating Numpy 1D Array (or Vector) from Python List arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) arr #>> array([1, 2, 3, 4, 5, 6, 7, 8, 9]) # Check the data type type(arr) #>> numpy.ndarray # Check the shape arr.shape #>> (9,) # Creating Numpy 2D Array (or Matrix) from Python List of List arr = np.array([[1, 2, 3], [4, 5, 6], [7 ,8, 9]]) arr #>> array([[1, 2, 3], #>> [4, 5, 6], #>> [7, 8, 9]]) # Check the shape arr.shape #>> (3, 3) # Create 2D Array using nested lists with range function in python np.array([range(i, i + 3) for i in [1, 2, 3]]) #>> array([[1, 2, 3], #>> [2, 3, 4], #>> [3, 4, 5]]) |
In above snippet, shape variable will return a shape of the numpy array.
Creating numpy array using built-in Methods
Let’s go through some of the common built-in methods for creating numpy array.
NumPy arange() Method
- Most commonly used method to create 1D Array
- It uses Pythons built-in range function to create a NumPy Vector
- Method takes start, stop, step as parameters
- Return evenly spaced values within a given interval

1 2 3 4 5 6 7 8 9 10 11 | # Using start, stop same as range() funtion np.arange(0, 10) #>> array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) # Skip start if starting from 0 np.arange(10) #>> array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) # All multipliers of 10 between 0 and 50 using step size 10 np.arange(0, 50, 10) #>> array([ 0, 10, 20, 30, 40]) |
NumPy zeros() and ones() Methods
We can create numpy arrays(1D, 2D or nD) of zeros and ones using np.zeros() and np.ones() methods.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | # Create 1D Array of size 10 of all 0's np.zeros(10) #>> array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]) # Create 3x3 matrix of 0's np.zeros((3, 3)) #>> array([[0., 0., 0.], #>> [0., 0., 0.], #>> [0., 0., 0.]]) # Create 1D Array of size 10 of all 1's np.ones(10) #>> array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]) # Create 3x3 matrix of 1's np.ones((3, 3)) #>> array([[1., 1., 1.], #>> [1., 1., 1.], #>> [1., 1., 1.]]) # By default, it creates the float64 type arrays np.ones((3, 3)).dtype #>> dtype('float64') # Create 3x3 integer matrix of 1's np.ones((3, 3), dtype=int) #>> array([[1, 1, 1], #>> [1, 1, 1], #>> [1, 1, 1]]) # Check datatype np.ones((3, 3), dtype=int).dtype #>> dtype('int32') |
NumPy linspace() Method
- np.linspace() creates an array with equally spaced numbers over a specified interval between two numbers
- Accepts arguments start, stop and numbers
- It will only create 1D Array or Vector

1 2 3 4 5 6 7 8 9 10 | # Create 1D Array of 10 evenly spaced points between 0 and 1 np.linspace(0, 1, 10) #>> array([0. , 0.11111111, 0.22222222, 0.33333333, 0.44444444, #>> 0.55555556, 0.66666667, 0.77777778, 0.88888889, 1. ]) # Create 8 evenly spaced points between 0 and 2 np.linspace(0, 2, 8) #>> array([0. , 0.28571429, 0.57142857, 0.85714286, 1.14285714, #>> 1.42857143, 1.71428571, 2. ]) |
NumPy: Creating Identity Matrix and Constant Array
- NumPy provides
eye()
method for creating identity matrix - In linear algebra, identity matrix is the NxN matrix with diagonal values are 1’s and 0 as other values
- For creating constant array we can use
full()
method of NumPy

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | # Creating 5x5 Identity Matrix np.eye(5, 5) #>> array([[1., 0., 0., 0., 0.], #>> [0., 1., 0., 0., 0.], #>> [0., 0., 1., 0., 0.], #>> [0., 0., 0., 1., 0.], #>> [0., 0., 0., 0., 1.]]) # Creating 1D NumPy Array of constant value 4 of shape (10,) np.full(10, 4) #>> array([4, 4, 4, 4, 4, 4, 4, 4, 4, 4]) # Creating 2D NumPy Array of constant value 4 of shape (3, 3) np.full((3, 3), 4) #>> array([[4, 4, 4], #>> [4, 4, 4], #>> [4, 4, 4]]) # Creating 3D NumPy Array of constant value 4 of shape (2, 2, 2) np.full((2, 2, 2), 4) #>> array([[[4, 4], #>> [4, 4]], #>> #>> [[4, 4], #>> [4, 4]]]) |
NumPy Random Initialized Arrays
NumPy library also supports methods of randomly initialized array values which is very useful in Neural Network training. All Deep Learning algorithms require randomly initialized weights during its training phase. We will discuss it in detail in upcoming Deep Learning related posts as it is not in our scope of this python numpy tutorial.
There are three ways to randomly initialize numpy arrays,
- Firstly,
np.random.random()
method, which gives you an array of uniformly distributed random values between 0 and 1 (arrays of float values) - Secondly,
np.random.normal()
method, which gives you an array of normally distributed random values (arrays of float values) - And finally,
np.random.randint()
method, which gives you an array of random integers in the interval of any two integer numbers

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | # Creating 1D Array of of shape (5,) with uniformly distributed random values between 0 and 1 np.random.random(5) #>> array([0.57832239, 0.8110782, 0.37285948, 0.65374923, 0.90808699]) # Creating 2D Array of of shape (5,5) with uniformly distributed random values between 0 and 1 np.random.random((5, 5)) #>> array([[0.5027019 , 0.0745165 , 0.94207658, 0.30235957, 0.01208522] #>> [0.86256952, 0.1979702 , 0.97533179, 0.17622831, 0.209948 ] #>> [0.41219764, 0.79882974, 0.38440959, 0.68467785, 0.80231865] #>> [0.62699369, 0.78961645, 0.05672165, 0.64370669, 0.11544882] #>> [0.59662287, 0.34238417, 0.1635091 , 0.62211215, 0.63459644]]) # Creating NumPy Array of Normally (Gaussian) distributed random values # Mean is 0 and Standard Deviation is 1 # 5x5 Matrix with Gaussian Distribution np.random.normal(0, 1, (5, 5)) #>> array([[-0.7802504 , 1.87389075, 0.83799145, -0.56858593, -1.68466328] #>> [-0.94157002, -0.26274747, 0.94935139, -0.82193679, 0.14073614] #>> [ 0.42273374, -0.84091773, -0.43898656, 0.97128851, 0.18128921] #>> [-1.37321462, -0.92096084, 1.51166843, -0.49543166, -1.93318495] #>> [-0.09461266, -0.41025144, 0.04797989, -0.69660561, 0.74597832]]) # Mean is 1 and Standard Deviation is 1 # 5x5 Matrix with Gaussian Distribution np.random.normal(1, 1, (5, 5)) #>> array([[ 0.16970685, 1.68095406, 2.7896238 , 1.15928132, 0.17791617] #>> [ 2.97243402, 0.828194 , 2.54592047, 2.40000972, 1.38799276] #>> [ 1.99503848, 1.45462598, -0.04715345, 0.29139532, 1.01401455] #>> [ 2.70219581, 0.06573982, 0.49560764, 1.68866321, 1.90871447] #>> [ 2.54277994, -0.39430304, 0.64921057, 0.45841184, 2.05533808]]) # Create NxN array of random integers in the interval [a, b) # 5x5 Random Integer Array between 10 and 98 np.random.randint(10, 99, (5, 5)) #>> array([[10, 47, 27, 62, 87] #>> [43, 59, 95, 44, 12] #>> [63, 85, 14, 37, 38] #>> [90, 72, 50, 79, 47] #>> [72, 60, 79, 68, 46]]) |
I am also planning to place a Jupyter Notbook of full numpy tutorial on my github soon. I am coming with more tutorials on numpy array slicing dicing, aggregations, broadcasting so till that time, stay tuned and keep practicing !!!!!
Got any questions or suggestions? Please let me know by posting them in comment box below.
Leave a Reply