NumPy – A Library in Python | Beginners Guide to NumPy

Basics of NumPy

 

NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays. In 2005, Travis Oliphant created NumPy by incorporating features of the competing Numarray into Numeric, with extensive modifications. NumPy is open-source software and has many contributors.NumPy is an acronym of Numeric Python.

N-dimensional array in NumPy

The core functionality of NumPy is its “ndarray”, for n-dimensional array, data structure. An ndarray is a (usually fixed-size) multidimensional container of items of the same type and size. The number of dimensions and items in an array is defined by its shape , which is a tuple of N non-negative integers that specify the sizes of each dimension.Here is the given example of using ndarray in python:

Why use NumPy array over Python Lists?

NumPy arrays are faster and more compact than Python lists. An array consumes less memory and is convenient to use. NumPy uses much less memory to store data and it provides a mechanism of specifying the data types. This allows the code to be optimised even further.Numpy data structures perform better 

1. Size – Numpy data structures take up less space.

2. Performance – they have a need for speed and are faster than lists.

3. Functionality – SciPy and NumPy have optimized functions such as linear algebra operations built in

Attributes of NumPy Array

NumPy array (ndarray class) is the most used construct of NumPy in Machine Learning and Deep Learning. Let us look into some important attributes of this NumPy array.

(1) ndarray.ndim

ndim represents the number of dimensions (axes) of the ndarray.This array attribute returns the number of array dimensions.

For Instance- for this 2-dimensional array [ [3,4,6], [0,8,1]], value of ndim will be 2. This ndarray has two dimensions (axes) – rows (axis=0) and columns (axis=1).

Given below is an example of using ndim attribute in python.

(2) ndarray.shape

shape is a tuple of integers representing the size of the ndarray in each dimension.This array attribute returns a tuple consisting of array dimensions. It can also be used to resize the array.

For Instance – for this 2-dimensional array [ [3,4,6], [0,8,1]], value of shape will be (2,3) because this ndarray has two dimensions – rows and columns – and the number of rows is 2 and the number of columns is 3.

Given below is an example of using shape attribute in python.

Here, we have defined one list and then convert that list into the numpy array and then figure out print it’s shape.

(3) ndarray.size

size is the total number of elements in the ndarray. It is equal to the product of elements of the shape. e.g. for this 2-dimensional array [ [3,4,6], [0,8,1]], shape is (2,3), size will be product (multiplication) of 2 and 3 i.e. (2*3) = 6. Hence, the size is 6.

Given below is an example of using size attribute in python.

(4) ndarray.dtype

dtype tells the data type of the elements of a NumPy array. In NumPy array, all the elements have the same data type.

e.g. for this NumPy array [ [3,4,6], [0,8,1]], dtype will be int64.

Given below is an example of using dtype attribute in python.

(5) ndarray.itemsize

itemsize returns the size (in bytes) of each element of a NumPy array.The itemsize function is simply used to check for the length in bytes of one array item in the internal representation.

e.g. for this NumPy array [ [3,4,6], [0,8,1]], itemsize will be 8, because this array consists of integers and size of integer (in bytes) is 8 bytes.

Given below is an example of using itemsize attribute in python.

The Numpy package provides attributes for the numpy arrays. These attributes help us to know the shape, dimension, and other properties of a given numpy array (ndarray).

Tags: Learn basics of numpy, numpy programs, learn numpy from scratch, numpy tutorial, numpy vs python list, numpy arrays

%d bloggers like this: