Decode Numpy Array

Have you ever wondered if Python provides a default list which can be used to store the list of items, even if it has many dynamic handling with all types of functions which we can think of in the list of items from inserting to sorting to filterration. But still most on high computation tasks with lists of items in Python people choose Numpy. The most of data science related packages use Numpy rather than list data structure πŸ€”.

The answer lies in understanding how a numpy array works internally, and what more numpy provides such that it is very optimised to handle the. 

Let’s get started πŸƒ.

What is numpy?

Numpy is the fundamental package for scientific computing with Python. Indeed it uses almost all high computation tasks in Python. It provides a default way to work with numbers with efficacy. From working on 1-d data to n-d data with all types of computations.

Couple of known Python package which use numpy πŸ‘€:

  • Pandas
  • SciPy
  • Scikit-learn
  • TensorFlow
  • PyTorch
  • Matplotlib

….. And list go on and on

Hear is a code example of basic numpy array:

import numpy as np


x=np.array([1,2,3]) # x = [1 2 3]
y=np.append(x,4) # y = [1 2 3 4]

How does a Numpy array work?

As per docs: what is numpy?

At the core of the NumPy package, is the ndarray object. This encapsulates n-dimensional arrays of homogeneous data types, with many operations being performed in compiled code for performance. There are several important differences between NumPy arrays and the standard Python sequences:

The array in Numpy is called ndarray. As the name suggests it will be an n-dimensional array. Can be mapped with Matrix in normal terms.

One the main thing we can notice is that it is homogeneous data types with fixed size. Same as the C array if you remember.Because the size of the array is fixed with the same data types of all items it give numpy to buy pass many default validation and calculator Python list do.we can assume numpy array as array in C.

This allows the operation of arrays way faster. Advantages Numpy has because of fixed size 🐱.

  • The size of the array is fixed which allows memory allocation fast.
  • The data type is the same which allows the Numpy array to map directly with the C array.
  • Overall operations become fast as size and data type is fixed.

Let’s understand the whole process with a diagram.

  • We have a 1-D numpy array x with size (3,1). In memory there will be 3 slots with size(int) at one time only, this will reduce to dynamic asinine memory allocation like Python list.
  • Now for adding a new value at the last position we can do that with x[2]=3, which is just O(1).
  • And if we now append a new value to x, the new array with size (4,1) will be created with 4 slots with size(int). And all values from x will be copied from x to y β˜€οΈ.

This is how a Numpy array works. The data type is fixed which provides many default mathematical operations with less interaction which make it very optimal to perform operations related to data sciences.

TLDR;

Many things in your life will fix if you make yourself more controlled with rules, such as numpy fixed data type and size for speed.

Bye πŸ‘‹ …..

Stunning monochrome portrait of a meerkat standing alert and observant in Munich, Germany.
Scroll to Top