Numpy Session 3 Code

Numy Session 3 Code For Video Click Fahad Hussain CS


#### Linear Algebra in numpy

import numpy as np 

A = np.array([[6, 1, 1], 

[4, -2, 5], 

[2, 8, 7]]) 


print("Rank of A:", np.linalg.matrix_rank(A)) 

print("\nTrace of A:", np.trace(A)) 

print("\nDeterminant of A:", np.linalg.det(A)) 

print("\nInverse of A:\n", np.linalg.inv(A)) 

print("\nMatrix A raised to power 3:\n", np.linalg.matrix_power(A, 3))


import numpy as np 

# coefficients 

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

# constants 

b = np.array([8, 18]) 


print("Solution of linear equations:", np.linalg.solve(a, b))

# The Dot Product is written using a central dot:

# a · b


# This means the Dot Product of a and b

# We can calculate the Dot Product of two vectors this way:

# dot product magnitudes and angle

# a · b = |a| × |b| × cos(?)


import numpy as np

product = np.dot(5, 4)

print("Dot Product of scalar values  : ", product)

vector_a = 12

vector_b = 4

 # a · b = |a| × |b| × cos(?)

product = np.dot(vector_a, vector_b)

print(product)


# This function returns the dot product of the two vectors. If the first argument is complex, then its conjugate is used for calculation. 

# If the argument id is multi-dimensional array, it is flattened.


import numpy as np

 

vector_a = 2+ 3j

vector_b = 4 + 5j

 

product = np.vdot(vector_a, vector_b)

print("Dot Product  : ", product)


#Inner product of two arrays.

a = np.array([1,4,0])

b = np.array([1,5,0])

np.inner(a, b)


#Compute the outer product of two vectors

x = np.array(['a', 'b', 'c'], dtype=object)

np.outer(x, [1, 2, 3])


# Matrix or vector norm.

# This function is able to return one of eight different matrix norms, or one of an infinite

# number of vector norms (described below), depending on the value of the ord parameter.


from numpy import linalg as LA

a = np.arange(9) - 4

print(a)


b = a.reshape((3, 3))

print(b)


print(LA.norm(a))

print(LA.norm(b))

print(LA.norm(b, 'fro'))

print(LA.norm(a, np.inf))

print(LA.norm(b, np.inf))

print(LA.norm(a, -np.inf))

print(LA.norm(b, -np.inf))


# Compute the condition number of a matrix.

# This function is capable of returning the condition number using one of Eight different norms, 


from numpy import linalg as LA

a = np.array([[1, 0, -1], [0, 1, 0], [1, 0, 1]])

a


print(LA.cond(a))

print(LA.cond(a, 'fro'))

print(LA.cond(a, np.inf))

print(LA.cond(a, -np.inf))

print(LA.cond(a, 1))

print(LA.cond(a, -1))

print(LA.cond(a, 2))

print(LA.cond(a, -2))


from numpy.linalg import multi_dot

# Prepare some data

A = np.random.random((10000, 100))

B = np.random.random((100, 1000))

C = np.random.random((1000, 5))

D = np.random.random((5, 333))

# the actual dot multiplication

print(multi_dot([A, B, C, D]))


from numpy import linalg as fahad

 

# Creating an array using array 

# function

a = np.array([[1, -2j], [2j, 5]])

 

print("Array is :",a)

 

# calculating an eigen value

# using eigh() function

c, d = fahad.eigh(a)

 

print("Eigen value is :", c)

print("Eigen value is :", d)


from numpy import linalg as fahad

 

# Creating an array using diag 

# function

a = np.diag((1, 2, 3))

 

print("Array is :",a)

 

# calculating an eigen value

# using eig() function

c, d = fahad.eig(a)

 

print("Eigen value is :",c)

print("Eigen value is :",d)

No comments:

Post a Comment

Fell free to write your query in comment. Your Comments will be fully encouraged.