what is numpy in python - Python

what is numpy in python

what is numpy in python

Numpy stands for Numerical python or Numeric Python is an open source module of python that offer functions for array and martrix computations.

Numpy was created in 2005 by Travis Oliphant. In order to use numpy we import it in our program by using the following command

import numpy as np.

Numpy is a python library used for working with arrays.

Creating of Numpy

1) array( ) function

syntax-

<array name>=<numpy_obj>.array(<list/tuple>)

import numpy as np

n=[10,20,30,40,50]

print(n)

print(type(n))#list

arr=np.array(n)

print(arr)

print(type(arr))#nd array

How to install numpy

pip install numpy

2) arange(<start>,<stop>,<step>)

np.arange(2,10,2)

Dimensions of Array

NumPy is a Python module that supports creation and manipulation of variety of arrays create in various ways. One of the variations could be dimensions of array. It’s possible to create  multidimensional arrays in NumPy. Applying the NumPy function ndim() to any existing an we get the dimension of the array.

Syntax

<NumPy obj>.ndim(<array_name>)

Example

np.ndim(a)




1) Zero-Dimensional Array

When we create an array with single element it is called zero-dimensional array. When single element assigned to a name then that name becomes a variable and not an array. When single walue is assigned to a name using NumPy function ‘array()’ the name becomes an array For example:

x=42

In the above examplex is an ordinary variable having an integer values 42.

x= np.array(42).

In the above example ‘x’ is an array having single element hence it is a zero- dimension array.

The following program illustrates the creation of 0-dimention array.

import numpy as np

x= np.array(42)

print(“x: “,x)

print(“The dimension of x:”, np.ndim(x))

2) Single-dimensional Array

An array containing two or more elements and the elements are arranged either in a row or column it is called Single dimensional array.

arr-np.array([1,2,3,4,5])

print(“Dimension=” np.ndim(arr))

3) Multi-dimensional Array

Arrays of NumPy are not limited to one dimension. They are of arbitrary dimension. We create them by passing nested lists (or tuples) to the array() method of NumPy. Every nested List adds one dimension to the array. Example:-

arr=np.array([<list>]) #1-D array

arr = np.array([[<list>], [<list>]])#2-Darray

arr= np.array([[[<list>], [<list>]],#3-darray

           [[<list>], [<list>]],

                                   [[<list>], [<list>]]])

Python Full Course Tutorial Link

https://www.youtube.com/playlist?list=PLFcjwFGBg31etmVLqLzhC4jyNBcnWCPTQ

The following program creates and prints 1D, 2D and 3D array of integers. It also uses array property ndim to print their dimensions.

import numpy as np

A1= np.array([1,2,3,4,5])

print(“1D Array: “,A1)

print(“Dimension:”,A1.ndim)

print(“=”*25)

A2= np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

print(“2D Array: \n”, A2)

print(“Dimension: “, A2.ndim)

print(“=”*25)

A3= np.array([[[5, 2], [7,4]],

           [[1, 3],[6,8]),

                        [[7,9],[5,4]])

print(“3D Array: \n”, A3)

print(“Dimension: “, A3.ndim)




Shape of Array

The shape of an array is represented as a tuple of integers. These integers denote the lengths of the corresponding array dimensions. In other words, the “shape” of an array is a tuple of integers where each integer represents the number of elements per axis. In the following example of array, the shape of the array is equal to (6, 3), i.e. we have 6 rows and 3 columns.

56           65            98

77           88           65

11           22           33

44           55           66

87           98           46

89           65           54

There is a NumPy function with the name ‘shape()’ which can be used to determine the shape of an already created array. The general format of the function is:

Syntax

<numpy_obj>.shape(<array name>)

Example

A= np.array([[1,2,3], [4,5,6]])

np.shape(A)

(2,3)

NumPy array property ‘shape’ can also be used to change the of an already created array. One thing must be taken care of that in any shape, the number of elements in the array must remain the same. Suppose there is an array with 12 elements in the shape (3,4) then we can reshape it to (4,3) or (2, 6) or (6, 2) because in all the shapes the number of elements is equals to 12.

To change the shape of an already created array, we just have to assign new shape to this property as follows:

The following program creates an array of integers of size 6.3. It demonstrates the use of NumPy function shape() and an array property shape to get the dimension of the array. The program also uses array property ‘shape’ to change the shape of an existing array.

import numpy as np

A = np.array([[76, 33, 77],

[77, 79, 49],

[82,37,97),

[63, 89, 93],

[23, 78, 92],

[68, 91, 78]])

print(np.shape(A)

print(A.shape)

print(A)

print(“Shape changed to 3*6”)

A.shape=(3,6)

print(A.shape)

print(A)

print(“Shape changed to 2*9”)

A.shape=(2,9)

print(A.shape)

print(A)

Also Check Our Latest Upload

top digital marketing agency in delhi

Local and Global Variables in JavaScript

Leave a Comment