Numpy
(Numerical Python) is a python library that provides multidimentional array. Numpy has ndarray at its core which encapsulated n-dimentional array of the homogeneous data type. In the this library, we can differenciate between array and list and while calculating the execution time, we found numpy executes more than 180 times faster than list. It can be use to calculate eigen values and eigenvectors of a matrices. Numpy also contais in-build function to interpret linear algebra which are shown below along with the examples.
You can install numpy package in VS Code using the following command in terminal but first you should have pip install in the same path of your python file.
TO import NumPy module in your, use the following code:
You can implement array in NumPy by passing list inside the argument(paranthesis).
import numpy as np
a1 = np.array([1,2,3])
a2 = a1 * np.array([1,2,3])
print(a1,a2) # output = [1 2 3] [1 4 9]
There are 6 general mechanisms for creating arrays:
- Conversion from other Python structures (i.e. lists and tuples)
- Intrinsic NumPy array creation functions (e.g. arange, ones, zeros, etc.)
- Replicating, joining, or mutating existing arrays
- Reading arrays from disk, either from standard or custom formats
- Creating arrays from raw bytes through the use of strings or buffers
- Use of special library functions (e.g., random)
dot = 0
for i in range(len(l1)):
dot += l1[i] * l2[i]
print(dot)
dot = np.dot(a1,a2)
print(dot) # output = 36
import numpy as np
# speed test or executionSpeed
from timeit import default_timer as timer
a = np.random.randn(1000)
b = np.random.randn(1000)
A = list(a)
B = list(b)
T = 1000
def dot1():
dot = 0
for i in range(len(A)):
dot += A[i]*B[i]
return dot
def dot2():
dot = np.dot(a,b)
start = timer()
for t in range(T):
dot1()
end = timer()
t1 = end-start
start = timer()
for t in range(T):
dot2()
end = timer()
t2 = end-start
print("\n\nList calculation:",t1)
print("np.dot", t2)
print("ratio:", t1/t2)
print(" ")
shape
--> NumPy arrays have an attribute called shape that returns a tuple with each index having the number of corresponding element, which means that the shape function will give the dimention of the dataset in NumPy.
.reshape(rows, columns)
--> Reshape will make a matrix form from the arrange. So, it depends upon if arrange has proper elements or not.
import numpy as np
a = np.arrange(8,14)
b = a.shape # shape gives the no of rows and no of column
c = b.reshape(2,4)
print(c)
# outputs: [ 6 7 8 9 10 11 12 13] # print(a)
[[ 6 7 8 9]
[10 11 12 13]]
print(a[0,:]) # outputs: [1 6 2]
print(a[:,0]) # outputs [1,3]
print(a.T) # transpose array, outputs: [[1 3]
# [6 4]
# [2 3]]
This section covers the ndim, ndmin,size, dig, and dtype attributes of an array.
This will give digonals matrix of a. In this function, when 1D array is passed, it will create a n x n diagonal matrix
When we pass multi-dimentional array in diag, it will take a diagonal elements to make diagonal matrix which can be seen from the picture below:
c = np.diag(a) # this will give digonals matrix of a
print(np.diag(a))
d = np.arange(1,9)
print(d)
print(d.shape)
e = d.reshape(2,4) # this will shape the matrix into 2x4 matrix
print(e)
np.arrange(starting, ending)
--> takes two attributes and display the range values while printing.
Numpy is widely used in mathematics and this python library makes it easy to calculate eigenValue and eigenVectosrs
import numpy as np
a = np.array([[1,2],[3,4]])
eigenvalues, eigenvectors = np.linalg.eig(a)
print(eigenvalues)
print(eigenvectors)
import numpy as np
from timeit import default_timer as timer
import timer_for_linalg # calling timer_for_linalg to print the time taken by each methods
T = 1
A = np.array([[1,1],[1.5,4.0]])
b = np.array([2200,5050])
# as we have formula,
# Ax = b => x = inverse(A)*b
def dot():
answer = np.linalg.inv(A).dot(b)
print(answer)
def solve():
answer = np.linalg.solve(A,b)
print(answer)
from linear_system import dot, solve
from timeit import default_timer as timer
T =1
start = timer()
for t in range(T):
dot()
end = timer()
t1 = end - start
start = timer()
for t in range(T):
solve()
end = timer()
t2 = end - start
print("Time taken by dot:", t1)
print("Time taken by solve:", t2)
print("Timing ratio =",t1/t2)