Python Programming for Beginners Part — 6 (NumPy Arrays)

Dr. Virendra Kumar Shrivastava
6 min readSep 1, 2021

Hey guys!

In last week’s article, we discussed Python arrays. In this article, we are going to talk about NumPy Arrays with example. The NumPy arrays provides an efficient way to store and process multidimensional arrays. Let’s get ready to understand numpy arrays.

What is Python NumPy array?

NumPy (Numerical Python) is a Python package/library which is used for scientific computing. It contains a powerful n-dimensional array object and provides tools for integrating other language codes C, C++ etc. NumPy furnishes a simple and unique way to store and operate on dense data buffers. NumPy array presents an efficient and powerful mechanism to handle multi-dimensional array object. It is represented in the form of rows and columns.

Advantages of Python NumPy Array v/s Python Array Module

Python Array module allows you to store and process one dimensional array data of same type. For example, if we want to store marks of four subjects of a student, we can store in single array as:

Marks = array(‘i’, [40, 43, 54, 36])

The marks array contains only one row of elements. Therefore, it is called a one- dimensional array or single dimensional array.

But Python array does not capable to store and process two-dimensional array. If we want to store marks obtained by three students, each one in four subjects can be expressed as

Marks =array ( [ [40, 43, 44, 36],

[44, 40, 31, 46],

[41, 33, 34, 36] ] )

The above array has more than one row and column, that’s why it is called multidimensional array. In this representation, the first row represents first student marks in four subjects. Similarly, second row represents marks of second student and third row represents marks of third student in four subjects respectively.

An array that has several one-dimensional arrays is called a two-dimensional array. Similarly, a three-dimensional array that has several two-dimensional arrays is called a three-dimensional array.

NumPy Python array offers an efficient way to store and process multi-dimensional arrays. We use python NumPy array because of the following reasons:

· Fast execution

· Takes Less Memory

· Convenient

How to install NumPy?

To install NumPy package, go to your command prompt and type “pip install numpy”. Once the installation is completed, go to your IDE like Jupyter Notebook, and import numpy library as given below:

Import numpy as np

To check numpy version, we can execute the following statements,

Creating NumPy Arrays

Numpy array supports the following data types:

dtype property

We can access different datatypes in NumPy with the dtype property. For example,

We can check datatype of the numpy array using dtype. For example,

We can create numpy arrays in several ways. Some of the popular ways are

· The array() function

· The linespace() function

· The logspace() function

· The arange() function

· The zeros() function

· The ones() function

The array() function

We can create numpy array by using array() function as follows:

from numpy import *

arrayname = array([list of array elements], dtype = datatype)

For example,

The above example is a one-dimensional array. We can also create a two-dimensional array passing two or more one dimensional array to array function. For example,

The linspace() and logspace() function

The NumPy array allows us to create an array with evenly spaced points between start and end points. The syntax of linspace() function is given below:

linspace(start, end, n)

Here start is the starting point, end is the stopping point and n signifies the number of parts the elements should be split. The n may be omitted. The default value of n is 50. That means it will split into 50 equally spaced parts. Here start and end point is included. For example,

The logspace() function is similar to linespace(). The only difference is that it will create evenly spaced points on logarithmically space scale. For example,

The arange() function

The syntax of arange() function is given below:

arange(start, end, step)

The arange() function produces a group of array elements from the start to the end point. Here, the end element is not included in the array and step is the stepsize. It is like the range() function of the python. For example,

The zeros() and ones() function

The zeros() function is used to create a NumPy array with all zeros. The syntax of zeros() function is below:

zeros(n, datatype)

We can omit datatype. The default datatype is float in numpy. For example,

The ones() function is used to create a NumPy array with all ones. The syntax of ones() function is below:

ones(n, datatype)

We can omit datatype. The default datatype is the float in numpy. For example,

Operations on NumPy arrays

Accessing Arrays

The numpy one-dimensional array follows the same indexing pattern as python list. Numpy array index begins with 0 and ends at n-1. It also allows negative indexing and slicing operations.

We can also perform slice operation on numpy arrays. We can add the interval in the slice option and can also use blank inputs in slices to represent the end or beginning index of the array. For example,

Reshaping Arrays

NumPy arrays have an attribute called shape that returns a tuple containing a count of the indexes and elements in the array. For example,

We can reshape an array with the reshape() function. It allows us to convert one-dimensional arrays into multi-dimensional or vis versa. For example,

Conclusion

In this article, we have seen numpy arrays and operations on it in detail with examples. You can also access Jupyter notebook to perform tutorial on the numpy array concepts discussed in this article.

On winding up notes, feel free to share your comments. Your comments will surely help me to present contents in a better way. See you next week.

--

--

Dr. Virendra Kumar Shrivastava

Professor || Alliance College of Engineering and Design || Alliance University || Writer || Big Data Analytics