通过梯度下降实现LDA是正确的方法吗?

时间:2020-10-17 18:34:46

标签: python numpy machine-learning classification linear-discriminant

任何对“线性判别分析”算法有深入了解的人,请对此提供一些想法。

我已经了解了LDA的基础知识,并对数学的工作原理有了一些直观的了解,但是当我通过实施时,我发现这个问题的关键在于找到特征值和特征向量,但是我想为什么不使用梯度下降来实现相同的效果(我从没发现梯度下降到任何地方的LDA),我就继续对2D数据集使用梯度下降实现了LDA, ”“根据我的理解,最终目标是将数据点指向新的子空间,其类别之间的平均距离最大,并且类别中的散布更少”,因此我将其用作损失函数并优化了算法,以找到最合适的子空间。 >

下面是我的代码,请仔细检查逻辑,让我说这是实现LDA的正确方法,如果不是,为什么?

def computecost(P1,P2,X):
    projected_point = []
    for i in range(0,len(X)):
        #d = np.linalg.norm(np.cross(P2-P1 , P1-X[i]))/np.linalg.norm(P2-P1)
        d=np.cross(P2-P1,X[i]-P1)/np.linalg.norm(P2-P1)
        #print('Distance between datapoint',X[i], 'and the line is',d)
        projected_point.append([X[i][0],X[i][1]-d])
    #print("\nCoordinates of projected points:")
    return  projected_point

computecost(P1,P2,X)
def gradient(x,X,theta):
    
    alpha = 0.0001
    iteration = 2000
    theta = np.random.rand(x.shape[1],1)

    #gradient descend algorithm
    J_history = np.zeros([iteration, 1]);
    
    

    for iter in range(0,2000):
        P1 = np.hstack((x[:,1].reshape(len(x@theta),1),x@theta)).min(0) #start point of new subspace
        P2 = np.hstack((x[:,1].reshape(len(x@theta),1),x@theta)).max(0) #End point of new subspace
        new_space = np.array(computecost(P1,P2,X)).reshape(X.shape)
        mask = y == 0
        mask_ = y == 1

        #MEAN
        mu1 = np.mean(new_space[mask, :])
        mu2 = np.mean(new_space[mask_, :])

        #SCATTER
        var1 = np.var(new_space[mask, :],ddof=1)
        var2 = np.var(new_space[mask_, :],ddof=1)




        error = mu1-mu2/(var1**2 + var2**2)
        temp0 = theta[0] + ((alpha/m) * np.sum(error*x[:,0]))
        temp1 = theta[1] + ((alpha/m) * np.sum(error*x[:,1]))
        theta = np.array([temp0,temp1]).reshape(2,1)
        J_history[iter] = mu1-mu2/(var1**2 + var2**2)
    return theta , J_history
#Step5 : Prediction
x_ = [0.001,0.9]

#project new datapoints to subspace to know which cateogry it belgons
P1 = np.hstack((x[:,1].reshape(len(x@theta),1),x@theta)).min(0) #start point of new subspace
P2 = np.hstack((x[:,1].reshape(len(x@theta),1),x@theta)).max(0) #End point of new subspace

d=np.cross(P2-P1,x_ -P1)/np.linalg.norm(P2-P1)

projected = [x_[0],x_[1]-d]
projected
if (mu1-projected[1])**2 < (mu2-projected[1])**2:
    print("Non Poisoneous")
else:
    print("Poisenous")

等待回复。

干杯!! 阿伦·拉姆吉(Arun Ramji S)

0 个答案:

没有答案