Menu

Numpy

What is NumPy?

- NumPy is a Python library used for working with arrays.
- It also has functions for working in domain of linear algebra, fourier transform, and matrices.
- NumPy was created in 2005 by Travis Oliphant. It is an open source project and you can use it freely.
- NumPy stands for Numerical Python.

At First we downlode the library of Numpy

pip install numpy

How to import NumPy?

import numpy as np

That's how we can import numpy and we givw the short from of numpy ,i.e, np

How to convert a List to an Array using NumPy?

import numpy as np
a = [1,2,3,4,5]
arr = np.array(a)
print(arr)

Output:

[1 2 3 4 5]

Finding Sum, Mean, Varience, Max or Min element of Array using NumPy

import numpy as np
a = [1,2,3,4,5]
arr = np.array(a)

summCodeHighlighter)
print("The sum of the given numbers in the arrays are:", summ)

avg = np.mean(arr)
print("The average of the given numbers in the arrays are:", avg)

varience = np.var(arr)
print("The varience of the given numbers in the arrays are:", varience)

maxx = np.max(arr)
print("The maximum number present in the list is:", maxx)

minn = np.min(arr)
print("The minumum number present in the list is:", minn)

Output:

The sum of the given numbers in the arrays are: 15
The average of the given numbers in the arrays are: 3.0
The varience of the given numbers in the arrays are: 2.0
The maximum number present in the list is: 5
The minumum number present in the list is: 1

Finding Dimension and Shape of Array using NumPy

import numpy as np
a = [1,2,3,4,5]
arr = np.array(a)

dimdim = np.ndim(arr) # to find the dimensioCodeHighlightery
print("The dimension of the array is:", dim)

shape = np.shape(arr) # to find the shape of the array
print("The shape of the array is:", shape)

Output:

The dimension of the array is: 1
The shape of the array is: (5,)

shape (5,) means 5 element and 1 dimentional

Change the shape of the array (e.g: Array to Matrix) of Array using NumPy

import numpy as np
a = np.array([1,2,3,4,5,6,7,8,9]) # in one line make the array
a = a.reshape(3,3) # change the shape of the array
print("The matrix from by the array is:", a)

Output:

The matrix from by the array is: [[1 2 3]
[4 5 6]
[7 8 9]]

Finding Dimension, Shape, Trace, Transpose of Matrix using NumPy

import numpy as np
a = np.array([1,2,3,4,5,6,7,8,9])

dim = np.ndim(a)
print("The dimension of the matCodeHighlighter)

shape = np.shape(a)
print("The shape of the matrix is:", shape)

trace = np.trace(a)
print("The trace of the matrix is:", trace)

transpose = np.transpose(a)
print("The transpose of the matrix is:", transpose)

Output:

The dimension of the matrix is: 2
The shape of the matrix is: (3, 3)
The trace of the matrix is: 15
The transpose of the matrix is: [[1 4 7]
[2 5 8]
[3 6 9]]

Dot and Cross Product of two Vectors using NumPy

import numpy as np

A = np.array([1,2,3])
B = np.aCodeHighlighter

print("First vector is:", A)
print("Second vector is:", B)

sc_product = np.dot(A,B) # dot product of two matrix
print("The dot product of these the two vectors is:", sc_product)

vec_product = np.cross(A,B) # cross product of two matrix
print("The cross product of these the two vectors are:", vec_product)

Output:

First vector is: [1 2 3]
Second vector is: [7 8 9]
The dot product of these the two vectors is: 50
The cross product of these the two vectors are: [-6 12 -6]

Multiplication of two Matrices using NumPy

import numpy as np

arr1 = np.array([1,2,3,4,5,6,7,8,9])
m1 = arr1.reshape(3,3)
print("The first maCodeHighlighter)

arr2 = np.array([9,8,7,6,5,4,3,2,1])
m2 = arr2.reshape(3,3)
print("The second matrix is:", m2)

m1m2 = np.dot(m1,m2)
print("The multiplication of these two matrix is:", m1m2)

Output:

The first matrix is: [[1 2 3]
[4 5 6]
[7 8 9]]
The second matrix is: [[9 8 7]
[6 5 4]
[3 2 1]]
The multiplication of these two matrix is: [[ 30 24 18]
[ 84 69 54]
[138 114 90]]

Inverse & Determinant of Matrix using NumPy

import numpy as np

arr = [1,2,-1,2,1,4,3,3,4]
arr = np.array(arr)
M = arCodeHighlighter)
print("The Matrix is:", M)
invM = np.linalg.inv(M)
print("The Inverse of the matrix is:", invM)
detM = round(np.linalg.det(M))
print("The Determinant of the Matrix is:", detM)

Output:

The Matrix is: [[ 1 2 -1]
[ 2 1 4]
[ 3 3 4]]
The Inverse of the matrix is: [[ 2.66666667 3.66666667 -3. ]
[-1.33333333 -2.33333333 2. ]
[-1. -1. 1. ]]
The Determinant of the Matrix is: -3

Solution of three equation with three variables by matrix multiplication method using NumPy

import numpy as np

# three equations are (solve for x, y and z)
# x + 2y - z = 1
# 2CodeHighlighter2
# 3x + 3y + 4z = 1

A = np.array([1,2,-1,2,1,4,3,3,4])
B = np.array([1,2,1])

X = np.linalg.solve(A,B)
print("The solution of the equation is: ", X)

Output:

The solution of the equation is: [[ 7.]
[-4.]
[-2.]]

Need Help?

If any problem occurs, feel free to contact me for assistance.

Leave a Comment

Comments

    No comments yet. Be the first to comment!