Linear Algebra for Data Science

Hi, welcome to the course of Linear Algebra for Data Science and Machine Learning. The whole playlist here you can download the code including the PPT Slides that was used in the video session.

Do Subscribe and share to others. Fahad Hussain CS


Click to Download the PPT SLIDE.


Tutorial 01:

There is No Code of this video, for better understanding watch the video.


Tutorial 02:

There is No Code of this video, for better understanding watch the video.


Tutorial 03:

There is No Code of this video, for better understanding watch the video.


Tutorial 04:

There is No Code of this video, for better understanding watch the video.


Tutorial 05:

There is No Code of this video, for better understanding watch the video.


Tutorial 06:

There is No Code of this video, for better understanding watch the video.


Tutorial 07:

There is No Code of this video, for better understanding watch the video.


Tutorial 08:

There is No Code of this video, for better understanding watch the video.


Tutorial 09:

There is No Code of this video, for better understanding watch the video.


Tutorial 10:

There is No Code of this video, for better understanding watch the video.


Tutorial 11:

There is No Code of this video, for better understanding watch the video.


Tutorial 12:

There is No Code of this video, for better understanding watch the video.


Tutorial 13:

There is No Code of this video, for better understanding watch the video.


Tutorial 14:

Here is the code of above all theory based videos of Vectors, which we have discussed so far, kindly watch the above all videos first for better understanding about;

import numpy as np

list1 = [1, 2, 3]

list2 = [10,20,30]


vector1 = np.array(list1)

vector2 = np.array(list2)

print("Horizontal Vector")

print(vector1)

print("Vertical Vector")

print(vector2)


addition = vector1 + vector2

print("Vector Addition       : " + str(addition))


subtraction = vector1 - vector2

print("Vector Subtraction   : " + str(subtraction))


multiplication = vector1 * vector2

print("Vector Multiplication : " + str(multiplication))


scalar = 2

print("Scalar  : " + str(scalar))

scalar_mul = vector1 * scalar

print("Scalar Multiplication : " + str(scalar_mul))


division = vector1 / vector2

print("Vector Division       : " + str(division))


# Dot Product

dot_product = vector1.dot(vector2)

print("Dot Product   : " + str(dot_product))


#Cross Product

cross = np.cross(vector1, vector2)

print(cross)


# Vector norm

mport numpy as np        # import necessary dependency with alias as np

from numpy.linalg import norm

arr=np.array([1,3,5])           #formation of an array using numpy library

l1=norm(arr,1)                 # here 1 represents the order of the norm to be calculated

print(l1)



import numpy as np             # import necessary dependency with alias as np

from numpy.linalg import norm

arr=np.array([1,3,5])               #formation of an array using numpy library

l2 =norm(arr,2)                        # here 2 represents the order of the norm to be calculated

print(l2)


import numpy as np                    # import necessary dependency with alias as np

from numpy.linalg import norm

arr=np.array([1,3,5])                   #formation of an array using numpy library

maxnorm =norm(arr,inf)             # here inf represents the order of the norm

print(maxnorm)



#Vector Projection

import numpy as np

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

v = np.array([5, 6, 2]) 

# finding norm of the vector v

v_norm = np.sqrt(sum(v**2))


# find dot product using np.dot()

proj_of_u_on_v = (np.dot(u, v)/v_norm**2)*v

print("Projection of Vector u on Vector v is: ", proj_of_u_on_v)




# Linear Combination of vector

import numpy as np

x = np.array([[0, 0, 1],

              [0, 1, 0],

              [1, 0, 0]])

y = ([3.65, 1.55, 3.42])

scalars = np.linalg.solve(x, y)

scalars



# linearly dependent and independent vectors

import sympy 

import numpy as np

matrix = np.array([

  [0, 5, 1],

  [0, 10, 0]

])


_, indexes = sympy.Matrix(matrix).T.rref()  # T is for transpose

print(indexes)


print(matrix[indexes,:])


if len(indexes) == 2:

    print("linearly independant")

else:

    print("linearly dependant")


Tutorial 15:

There is No Code of this video, for better understanding watch the video.


Tutorial 16:

There is No Code of this video, for better understanding watch the video.


Tutorial 17:

There is No Code of this video, for better understanding watch the video.


Tutorial 18:

There is No Code of this video, for better understanding watch the video.


Tutorial 19:

There is No Code of this video, for better understanding watch the video.


Tutorial 20:

There is No Code of this video, for better understanding watch the video.


Tutorial 21:

There is No Code of this video, for better understanding watch the video.


Tutorial 22:

There is No Code of this video, for better understanding watch the video.


Tutorial 23:

There is No Code of this video, for better understanding watch the video.


Tutorial 24:

There is No Code of this video, for better understanding watch the video.


Tutorial 25:

There is No Code of this video, for better understanding watch the video.



Tutorial 26:

Here is the code of above all theory based videos of Matrix, which we have discussed so far, kindly watch the above all videos first for better understanding about;

import numpy

x = numpy.array([

        [1, 2], 

        [4, 5]

        ])

y = numpy.array([[7, 8], [9, 10]])



print ("The element wise addition of matrix is : ")

print (numpy.add(x,y))


print ("The element wise subtraction of matrix is : ")

print (numpy.subtract(x,y))


print ("The element wise division of matrix is : ")

print (numpy.divide(x,y))


print ("The element wise multiplication of matrix is : ")

print (numpy.multiply(x,y))


print ("The element wise square root is : ")

print (numpy.sqrt(x))


print ("The summation of all matrix element is : ")

print (numpy.sum(y))


print ("The column wise summation of all matrix is : ")

print (numpy.sum(y,axis=0))

# using sum(axis=1) to print summation of all columns of matrix

print ("The row wise summation of all matrix is : ")

print (numpy.sum(y,axis=1))


print ("The transpose of given matrix is : ")

print (x.T)



import numpy as np

# Vectors as 1D numpy arrays

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

b = np.array([4, 5, 6])


print("a= ", a)

print("b= ", b)

print("\ninner:", np.inner(a, b))

print("dot:", np.dot(a, b))


############## Dot and inner product of matrix



import numpy as np

# Matrices as ndarray objects

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

b = np.array([[5, 6, 7], [8, 9, 10]])

print("a", type(a))

print(a)

print("\nb", type(b))

print(b)


# Matrices as matrix objects

c = np.matrix([[1, 2], [3, 4]])

d = np.matrix([[5, 6, 7], [8, 9, 10]])


print("\ndot product of two ndarray objects")

print(np.dot(a, b))

print("\ndot product of two matrix objects")

print(np.dot(c, d))




import numpy as np


# Matrices as ndarray objects

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

b = np.array([[5, 6], [8, 9]])

print("a", type(a))

print(a)

print("\nb", type(b))

print(b)


# Matrices as matrix objects

c = np.matrix([[1, 2], [3, 4]])

d = np.matrix([[5, 6], [8, 9]])

print("\nc", type(c))

print(c)

print("\nd", type(d))

print(d)

print("\n* operation on two ndarray objects (Elementwise)")

print(a * b)

print("\n* operation on two matrix objects (same as np.dot())")

print(c * d)

 


Tutorial 27:

There is No Code of this video, for better understanding watch the video.


Tutorial 28:

There is No Code of this video, for better understanding watch the video.


Tutorial 29:

There is No Code of this video, for better understanding watch the video.


Tutorial 30:

There is No Code of this video, for better understanding watch the video.


Tutorial 31:

Here is the code of above all theory based videos of LU Decomposition, which we have discussed so far, kindly watch the above all videos first for better understanding about;

# LU Decomposition

import numpy as np

import scipy.linalg as la

np.set_printoptions(suppress=True)


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


L = np.array([[1,0,0],[2,1,0],[4,11/5,1]])

U = np.array([[1,3,4],[0,-5,-5],[0,0,-3]])

print(L.dot(U))

print(L)

print(U)


# SVD

from numpy import array

from scipy.linalg import svd

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

print(A)

U, s, VT = svd(A)

print(U)

print(s)

print(VT)


# SVD 

import numpy as np

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

print(A)

print(np.linalg.pinv(A))


import matplotlib.pyplot as plt

x1 = np.linspace(-5, 5, 1000)

x2_1 = -2*x1 + 2

x2_2 = 4*x1 + 8

x2_3 = -1*x1 + 2


plt.plot(x1, x2_1)

plt.plot(x1, x2_2)

plt.plot(x1, x2_3)

plt.xlim(-2., 1)

plt.ylim(1, 5)

plt.show()




# pinv used to Compute the (Moore-Penrose) pseudo-inverse of a matrix.

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

A_plus = np.linalg.pinv(A)

A_plus


Tutorial 32:

There is No Code of this video, for better understanding watch the video.


Tutorial 33:

There is No Code of this video, for better understanding watch the video.


Tutorial 34:

Follow the Tutorial 33, for better understanding about;

Click to download the code on COLAB.


Tutorial 35:

There is No Code of this video, for better understanding watch the video.


Tutorial 36:

Follow the Tutorial 33, for better understanding about;


Tutorial 37:

There is No Code of this video, for better understanding watch the video.


Tutorial 38:

There is No Code of this video, for better understanding watch the video.


Tutorial 39:

There is No Code of this video, for better understanding watch the video.


Tutorial 40:

Here is the code of above all theory based videos of DISTANCES, which we have discussed so far, kindly watch the above all videos first for better understanding about;

# Python Program to Calculate Distance 


# Euclidean distance

x1 = float(input('Enter x1: '))

y1 = float(input('Enter y1: '))

x2 = float(input('Enter x2: '))

y2 = float(input('Enter y2: '))


# Calculating distance

d = ( (x2-x1)**2 + (y2-y1)**2 ) ** 0.5


# Displaying result

print('Distance = %f' %(d))

# cosine_similarity using python


import numpy as np

from sklearn.metrics.pairwise import cosine_similarity,cosine_distances

A=np.array([10,3])

B=np.array([8,7])

result=cosine_similarity(A.reshape(1,-1),B.reshape(1,-1))

print(result)


# Hamming distance using python


from scipy.spatial.distance import hamming

x = [0, 1, 1, 1, 0, 1]

y = [0, 0, 1, 1, 0, 0]


hamming(x, y) * len(x)


# Manhattan distance using python


from math import sqrt

def manhattan(a, b):

    return sum(abs(val1-val2) for val1, val2 in zip(a,b))


A = [2, 4, 4, 6]

B = [5, 5, 7, 8]


manhattan(A, B)


# minkowski_distance using python


from math import *

from decimal import Decimal


def p_root(value, root):


    root_value = 1 / float(root)

    return round (Decimal(value) **

            Decimal(root_value), 3)


def minkowski_distance(x, y, p_value):

    return (p_root(sum(pow(abs(a-b), p_value)

            for a, b in zip(x, y)), p_value))


# Driver Code

vector1 = [0, 2, 3, 4]

vector2 = [2, 4, 3, 7]

p = 3

print(minkowski_distance(vector1, vector2, p))


# minkowski_distance using python

from scipy.spatial import minkowski_distance

# define data

row1 = [10, 20, 15, 10, 5]

row2 = [12, 24, 18, 8, 7]

# calculate distance (p=1)

dist = minkowski_distance(row1, row2, 1)

print(dist)

# calculate distance (p=2)

dist = minkowski_distance(row1, row2, 2)

print(dist)




# Jaccard Distance using python

A = {1, 2, 3, 5, 7}

B = {1, 2, 4, 8, 9}

def jaccard_similarity(A, B):

    #Find intersection of two sets

    nominator = A.intersection(B)


    #Find union of two sets

    denominator = A.union(B)


    #Take the ratio of sizes

    similarity = len(nominator)/len(denominator)

    

    return similarity


similarity = jaccard_similarity(A, B)

print(similarity)


# Haversine Distance using python


# However you can use !pip install haversine

# to make model in better way but I used basic work to make you understand this


from math import radians, cos, sin, asin, sqrt


def haversine(lon1, lat1, lon2, lat2):

    """

    Calculate the great circle distance in kilometers between two points 

    on the earth (specified in decimal degrees)

    """

    # convert decimal degrees to radians 

    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])


    # haversine formula 

    dlon = lon2 - lon1 

    dlat = lat2 - lat1 

    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2

    c = 2 * asin(sqrt(a)) 

    r = 6371 # Radius of earth in kilometers. Use 3956 for miles. Determines return value units.

    return c * r


haversine(-99.436554, 41.507483, -98.315949, 38.504048)



# Sørensen-Dice Index Distance using python


import numpy as np

np.random.seed(0)

true = np.random.rand(10, 5, 5, 4)>0.5

pred = np.random.rand(10, 5, 5, 4)>0.5


def single_dice_coef(y_true, y_pred_bin):

    # shape of y_true and y_pred_bin: (height, width)

    intersection = np.sum(y_true * y_pred_bin)

    if (np.sum(y_true)==0) and (np.sum(y_pred_bin)==0):

        return 1

    return (2*intersection) / (np.sum(y_true) + np.sum(y_pred_bin))


def mean_dice_coef(y_true, y_pred_bin):

    # shape of y_true and y_pred_bin: (n_samples, height, width, n_channels)

    batch_size = y_true.shape[0]

    channel_num = y_true.shape[-1]

    mean_dice_channel = 0.

    for i in range(batch_size):

        for j in range(channel_num):

            channel_dice = single_dice_coef(y_true[i, :, :, j], y_pred_bin[i, :, :, j])

            mean_dice_channel += channel_dice/(channel_num*batch_size)

    return mean_dice_channel


def dice_coef2(y_true, y_pred):

    y_true_f = y_true.flatten()

    y_pred_f = y_pred.flatten()

    union = np.sum(y_true_f) + np.sum(y_pred_f)

    if union==0: return 1

    intersection = np.sum(y_true_f * y_pred_f)

    return 2. * intersection / union


print(mean_dice_coef(true, pred))

print(dice_coef2(true, pred))

Tutorial 41:

There is No Code of this video, for better understanding watch the video.

Get ready to understand about Statistics FOR DATA SCIENCE AND MACHINE LEARING coming soon, stay in touch and keep visiting blog.



4 comments:

  1. sir, ppt slide for linear algebra ?

    ReplyDelete
  2. Thankyou sir give me a wonderful content? this content are very
    helpful to my data-Science! I really need of this type content

    ReplyDelete

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