机器学习-线性模型-LDA(线性判别分析)PYTHON 代码实现

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis

def weight(x,y):

    u = []
    classify = np.unique(y)
    for i in range(len(classify)):
        u.append(np.mean(x[y==classify[i]],axis=0))

    Sw = 0
    for j in range(x.shape[0]):
        if y[j] == 0:
            u0 = u[0]
            a = np.mat((x[j] - u0))
            Sw += np.dot(a.T,a)
        elif y[j] == 1:
            u1 = u[1]
            b = np.mat((x[j] - u1))
            Sw += np.dot(b.T,b)

    Sb = np.dot((u[0] - u[1]).T,((u[0] - u[1])))
    mu, sigma, v = np.linalg.svd(np.array(np.mat(Sw),dtype=float))
    Sw_inv = v.T * np.linalg.inv(np.diag(sigma)) * mu.T
    w = np.dot(Sw_inv,(u[0] - u[1]).T)

    return w,Sb,Sw

def costfunc(w,Sb,Sw):

    J = np.dot(np.dot(w.T,Sb),w) / np.dot(np.dot(w.T,Sw),w)

    return J

def LDA(x,y):

    w,Sb,Sw = weight(x,y)
    print("cost is {}".format(costfunc(w,Sb,Sw)))

    return w

你可能感兴趣的:(python,机器学习)