NumPy is considered to be the fundamental package for scientific computing in python. It is a Python library that provides a multidimensional array object, various derived objects (such as masked arrays and matrices), and an assortment of routines for fast operations on arrays, including mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical operations, random simulation and much more.

What is NumPy?

NumPy is a multi-dimensional array library, so that we can use it to store 1-dimensional array, 2-dimensional array, 3-dimensional array, and more.

How are Lists different from NumPy?

NumPy is fast because it uses fixed types and it stores the data in Binary format. Then it converts the 1 byte to 2 byte or 4 bytes to store it as Int16 or Int32.

Lists are slow because they store Object Value, Object Type, Reference Count and Size. In which Object Value is represented by 8 bytes, Object Type is also represented by 8 bytes, even Reference Count is also represented by 8 bytes and lastly size is represented by 4 bytes. So overall, to store a single integer type value in a list it takes 36 bytes.

Why is NumPy Faster? – Fixed Type

  • It uses fewer bytes of memory to store, computers can read less bytes quicker.
  • When iterating through objects in a NumPy array, there is no type checking.
  • It utilizes contiguous memory.
    That means, it stores the data in memory in continuous blocks rather than splitting into far away blocks.

Applications of NumPy

  1. MATLAB Replacement – Can do all Mathematical operations.
  2. Plotting (Matplotlib) – It can also do plotting.
  3. Backend (Pandas, connect 4, Digital Photography) – It is used to store all kinds of information which is being used by these backend libraries.
  4. Machine Learning

How to create NumPy arrays

First, we need to import NumPy and then we can create one-dimensional, two-dimensional or more.

Get Dimensions

“ndim” keyword helps us to get the dimension of an NumPy array.

Get Shape

“shape” keyword helps us to get the shape of an NumPy array.

Here, the 1st number tells us about the rows and the 2nd tells us about the elements inside the row.

Get Type

“dtype” keyword helps us to get the type of an NumPy array.

Get Size

“itemsize” keyword helps us to get the item size of an NumPy array.

Get Total Size

“size” keyword helps us to get the number of elements in an NumPy array. When multiplied with “itemsize”, it gives the total size of that array in the memory.

“nbytes” keyword can also be used to get the same information, without doing any computation.

Set type while initializing array

“dtype” parameter can be used while initializing an array to set the type of the NumPy array.

Accessing/Changing specific elements, row, columns, etc.

Get a Specific Element

We can print elements of an array using row and column. We can count from 0 from the start or we can count from -1 from the back.

Get a Specific Row

We can print the whole row using the ‘:’ operator in the column place.

Get a Specific Column

We can print the whole column using the ‘:’ operator in the row place.

Get elements from an array

We can print a set of numbers using [startindex:endindex:stepsize]

Set a Specific Element

We can update any element by specifying the row and column in the NumPy array.

Initializing Different Types of Arrays

‘zeros’ keyword is used to create an array with all ‘0’ for each element. In the above example, created a 2 x 2 matrix with each element as ‘0’.

‘ones’ keyword is used to create an array with all ‘1’ for each element. In the above example, created a 4 x 2 x 2 matrix with each element as ‘1’.

‘full’ keyword is used to create the array with any number mentioned in the 2nd parameter. In the above example, created a 2 x 2 matrix with each element as ‘99’.

Initializing Random Elements

‘random’ and ‘rand’ keywords can be used to create a random array of float values.

‘randint’ can be used to generate an random array of integers and can specify that numbers should be between n to m, n and m needs to be specified.

Identity Matrix

‘identity’ keyword can be used to create an identity matrix, and by its nature its going to be a square matrix.

Repeat Elements in array

‘repeat’ element can be used to repeat an element to number of times specified and axis can also be specified.

Copy Arrays

If we have to copy the NumPy array, and if we directly did b = a, it will set b as point to a. So, if we update any element in b, it will also update an array.

So, we can use the ‘copy ()’ function to copy the data from a to b.

Mathematics

Basic Mathematics

We can use direct mathematical operations to update the array elements.

We can do any mathematical operations using 2 NumPy arrays directly.

Trigonometry Operations

It also provides trigonometry operations, which can be directly applied on an array.

Linear Algebra

It provides multiplication methods when the matrix rows and columns are not the same.

Find Statistics

In NumPy, we can use ‘min’, ‘max’ keywords, to find minimum and maximum values from the mentioned axis.

Reorganizing Arrays

‘reshape’ keyword can be used to reorganize a NumPy array. 

Vertical Stack

‘vstack’ keyword can be used to stack the arrays vertically.

Horizontal Stack

‘hstack’ keyword can be used to stack the arrays horizontally.

Share This