LDF
QDF
python实现
import numpy as np
# import matplotlib.pyplot as plt
import math
import tensorflow as tf
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.model_selection import train_test_split, cross_val_score, GridSearchCV # 划分数据 交叉验证 网格搜索
class LDFandQDF():
# 计算判别函数系数
def train(self, x_train, y_train, classA, classB):
"""X为训练数据集,y为训练label"""
X1 = np.array([x_train[i] for i in range(len(x_train)) if y_train[i] == classA]) # 找出标签为classA的数据
X2 = np.array([x_train[i] for i in range(len(x_train)) if y_train[i] == classB]) # 找出标签为classB的数据
# 计算先验概率
Pw1 = len(X1) / (len(X1) + len(X2))
Pw2 = len(X2) / (len(X1) + len(X2))
# 求均值
mju1 = np.mean(X1, axis=0) # mju是ndarray类型
mju2 = np.mean(X2, axis=0)
# 求协方差