Creating NumPy Array for Beginners

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.

  1. One way to make numpy array is using python list or nested list
  2. 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.

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
NumPy arange() Method
NumPy arange() Method

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.

NumPy zeros() and ones() Methods
NumPy zeros() and ones() Methods

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
NumPy linspace() Method
NumPy linspace() Method

 

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
NumPy eye() and full() Methods
NumPy eye() and full() Methods
 

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,

  1. Firstly, np.random.random() method, which gives you an array of uniformly distributed random values between 0 and 1 (arrays of float values)
  2. Secondly, np.random.normal() method, which gives you an array of normally distributed random values (arrays of float values)
  3. And finally, np.random.randint() method, which gives you an array of random integers in the interval of any two integer numbers
NumPy random Array Creation Methods
NumPy random Array Creation Methods
Congratulations, we have just gained the basic understanding of creating numpy array with this short python numpy tutorial !

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

Your email address will not be published. Required fields are marked *