<jupyter_start><jupyter_text>Data Science Project   Project Topic: Multiview Uncorrelated Linear Discriminant Analysis(MULDA)  Prepared by 1. <NAME>(0801CS171001) 2. <NAME> (0801CS171027)3. <NAME>(0801CS171031)  Importing the required libraries<jupyter_code>import numpy as np
import pandas as pd
import math
import os<jupyter_output><empty_output><jupyter_text>Importing Datasets We are considering 2 datasets for multiview X,Y also these datasets uploaded in github for further info.<jupyter_code>X=pd.read_csv('mfeat-mor',delim_whitespace=True,header=None)
X
Y=df=pd.read_csv('mfeat-pix',delim_whitespace=True,header=None)
Y<jupyter_output><empty_output><jupyter_text>Calculating shape of each dataset by using shape function<jupyter_code>Y.shape<jupyter_output><empty_output><jupyter_text>Reducing dataset column and rows for better results<jupyter_code>Y=Y[:10]
Y
X=X[:10]
X
Y.drop(Y.iloc[:, 7:], inplace = True, axis = 1) 
Y
Y.drop(Y.iloc[:, 6:], inplace = True, axis = 1) 
Y
X
row,n=X.shape
n<jupyter_output><empty_output><jupyter_text>Mathematical Formulation ImplementationNotations Used for calculation of Covariance matrices 1. Cxy=Covariance matrices between datasets x and y2. Cyy=Covariance matrices between datasets y and y3. Cxx=Covariance matrices between datasets x and xDot function returns the dot product of two arrays. For 2-D vectors, it is the equivalent to matrix multiplication.<jupyter_code>Cxy=np.dot(X,Y.T)
Cxy=Cxy/n
Cxy
Cyy=np.dot(Y,Y.T)
Cyy=Cyy/n
Cyy
Cxx=np.dot(X,X.T)
Cxx=Cxx/n
Cxx<jupyter_output><empty_output><jupyter_text>W below is an diagonal matrices of size(n*n)<jupyter_code>row,col=(n,n)
W=[]
for i in range(col):
    c=[]
    for j in range(row):
        if i==j:
            c.append(1/n)
        else:
            c.append(0)
    W.append(c)
W
row,col=(n,n)
I=[]
for i in range(col):
    c1=[]
    for j in range(row):
        if i==j:
            c1.append(1)
        else:
            c1.append(0)
    I.append(c1)
I<jupyter_output><empty_output><jupyter_text>I is identity matrix of size(n*n) made to perform subtraction of 2 matrices (I-W)<jupyter_code>Diff=np.subtract(I,W)
Diff<jupyter_output><empty_output><jupyter_text>Creation of Scatter Matrix Part for the datasetsNotations Used for calculation of Scatter matrices 1. Swx=Between class scatter matrix for dataset x2. Sbx=Within class scatter matrix for dataset x3. Stx=Total scatter matrix for dataset x4. Swy=Between class scatter matrix for dataset y2. Sbx=Within class scatter matrix for dataset y3. Stx=Total scatter matrix for dataset y<jupyter_code>S=np.dot(X,Diff)
S
Sw=np.dot(S,X.T)
Sw=Sw/n
Sw
t=np.dot(X,W)
Sb=np.dot(t,X.T)
Sb=Sb/n
Sb
Swx=Sw
Sbx=Sb
Stx=np.dot(X,X.T)
Stx=Stx/n
Stx
S1=np.dot(Y,Diff)
S1
Swy=np.dot(S1,Y.T)
Swy
Swy=Swy/n
S3=np.dot(Y,W)
Sby=np.dot(S3,Y.T)
Sby
Sby=Sby/n
Sty=np.dot(Y,Y.T)
Sty=Sty/n
Sty<jupyter_output><empty_output><jupyter_text>Calculating sigma : σ ← tr(Stx )/tr(Sty )<jupyter_code>sigma1= Stx.trace()
sigma2= Sty.trace()
sigma= sigma1/sigma2
sigma<jupyter_output><empty_output><jupyter_text>Trace function above determines the trace of the matrix Stx, Sty<jupyter_code>r,c=Stx.shape<jupyter_output><empty_output><jupyter_text>Iu below is an diagonal matrix of size (r.c) i.e. (10*10) in this case.<jupyter_code>Iu=[]

for i in range(c):
    c1=[]
    for j in range(r):
        if i==j:
            c1.append(1)
        else:
            c1.append(0)
    Iu.append(c1)
Iu<jupyter_output><empty_output><jupyter_text>Notations used in below part of the code will be1.) Dx,Dy= Empty Matrices of shape (r,c)<jupyter_code>d = Stx.shape[0]

Dx = [ [ 0 for i in range(r) ] for j in range(c) ]
Dy = [ [ 0 for i in range(r) ] for j in range(c) ]<jupyter_output><empty_output><jupyter_text>Calculation of matrix Px,Py can be done using following formula  Px = I−StxDx(Transpose)*(DxStxDx(Transpose)^(-1))*DxPy = I−StyDy(Transpose)*(DyStyDy(Transpose)^(-1))*Dy  Calculation of rth vector pair wxr,wyr  The value of wxr,wyr can be find by each time in loop running through variable r by finding eigen value of matrix wx  and taking maximum of all values coming everytime through loop  Finding value of matrix Dx,Dy The value of Dx and Dy can be find by appending wxr to Dx and wyr to Dy as the last column<jupyter_code>for u in range(0,d):
    #calculating Px
    p=np.dot(Stx,np.transpose(Dx))
    p1=np.dot((np.dot(Dx,Stx)),np.transpose(Dx))
    p1_inv=np.linalg.pinv(p1)

    Px=np.dot((np.dot(p,p1_inv)),Dx)
    Px = Iu - Px
    #Calculating Py
    P=np.dot(Sty,np.transpose(Dy))
    P1=np.dot((np.dot(Dy,Sty)),np.transpose(Dy))
    P1_inv=np.linalg.pinv(P1)

    Py=np.dot((np.dot(P,P1_inv)),Dy)
    Py= Iu-Py
    
    #Wx and Wy
    A = Sty*Px*Sbx
    A = sigma*A
    B = Sty*Px*Cxy
    B = sigma*B
    C = Stx*Py*Cxy
    D =Stx*Py*Sby
    
    Inv_a=np.linalg.pinv(A)
    Inv_d=np.linalg.pinv(D)

    F1=np.dot(Inv_a,B)
    F2=np.dot(F1,Inv_d)
    F=np.dot(F2,C)
    F[F<0]=0
    F_Final=np.sqrt(F)

    U,D,V=np.linalg.svd(F_Final)
    V=V.T

    Cxxi=np.linalg.inv(Cxx)
    Cxxi[Cxxi<0]=0
    Cxx_sqrt=np.sqrt(Cxxi)
    wx=np.dot(Cxx_sqrt,U)

    Cyyi=np.linalg.pinv(Cyy)
    Cyyi[Cyyi<0]=0
    Cyy_sqrt=np.sqrt(Cyyi)
    wy=np.dot(Cyy_sqrt,V)
    
    #rth vector pair
    wValues, vVectors = np.linalg.eig(wx)
    index = np.argmax(wValues, axis=0)
    
    l = [0]*r
    for i in range (0,r):
        l[i] = wx[i][index]

     # Finding matrix Dx,Dy
    for i in range(len(l)):
        Dx[i][u]=l[i]
        
    wValuesY, vVectorsY = np.linalg.eig(wy)
    indexY = np.argmax(wValuesY, axis=0)
    l1 = [0]*r
    for i in range (0,r):
        l1[i] = wy[i][indexY]

    for i in range(len(l1)):
        Dy[i][u]=l1[i] 
Dx
temp = np.array(Dx)
temp.reshape(r,c)<jupyter_output><empty_output><jupyter_text>Finding value of Wx and Wy The feature extraction Wx and Wy can be finded just assigning Dx to be wxr and Dy to be wyr<jupyter_code>Wx = temp
temp1 = np.array(Dy)
temp1.reshape(r,c)
Wy = temp1
Wy.shape<jupyter_output><empty_output><jupyter_text>Finding Combined Feature Extraction Z Res1 is finded out by multilplying Wy by X and similiarly Res2 by multiplying Wx with Y Z is the combined vector pair of Res1 and Res2<jupyter_code>Res1 = Wy.dot(X)
Res2 = Wx.dot(Y)<jupyter_output><empty_output><jupyter_text>Z=[Res1,   Res2  ]<jupyter_code>Z= [[0]*1]*2
Z[0] = Res1
Z[1] = Res2
Z<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12962089035708668, 'fraction_numerical': 0.020921818467830738, 'mean_word_length': 4.160714285714286, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 0, '>': 68, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '482207', 'license': 'MIT', 'n_tokens_mistral': 2797, 'n_tokens_neox': 2683, 'n_words': 622, 'path': 'Groups/Group_ID_35/MULDA/DataScience_MULDA.ipynb', 'repo_name': 'aryapushpa/DataScience', 'size': '6378'}