Commit ab430625 by 20200318093

update kernel func

parent aee8dc20
......@@ -66,13 +66,15 @@ class KernelDiscriminantAnalysis(BaseEstimator, ClassifierMixin,
self.N = 0 # 用于存放 Kernel LDA 最优化公式中的 N
self.EigenVectors = None # 用于存放 Kernel LDA 最优化公式中的 M 对应的广义特征向量, 每一列为一个特征向量, 按照对应特征值大小排序
def Kernal(self,x,y,gamma):
### Defining a kernel, can replace it with linear or others
def Kernel(self,x,y,gamma):
return math.exp(-(x-y).T.dot(x-y)*gamma)
### Computting kernel matrix,
### input: X1(n_samples_1,n_feature) * X2(n_samples_2,n_feature)
### Output: (n_samples_1,n_samples_2)
def Kernal_matrix(self,x1,x2,gamma):
#print(x1.shape,x2.shape,gamma)
#print(self.Kernal([1,1],[2,2],gamma=gamma))
return np.fromfunction(np.vectorize(lambda i,j: self.Kernal(x1[int(i),:],x2[int(j),:],gamma=gamma)), (x1.shape[0],x2.shape[0]), dtype=np.float32)
return np.fromfunction(np.vectorize(lambda i,j: self.Kernel(x1[int(i),:],x2[int(j),:],gamma=gamma)), (x1.shape[0],x2.shape[0]), dtype=np.float32)
def fit(self, X, y):
"""Fit KDA model.
......@@ -105,14 +107,17 @@ class KernelDiscriminantAnalysis(BaseEstimator, ClassifierMixin,
l.append(X1.shape[0])
### Computing M, based on M = (M0-M1) @ (M1-M1).T, M is symmetric
### Alternatively, we can use sklearn.metrics.pairwise.rbf_kernel
### Reason of using this function is that we can replace the RBF kernel by other kernels easily
M0 = np.mean(self.Kernal_matrix(X,X0,gamma=self.gamma),axis=-1).reshape(-1,1)
M1 = np.mean(self.Kernal_matrix(X,X1,gamma=self.gamma),axis=-1).reshape(-1,1)
self.M = (M0-M1) @ (M0-M1).T
### Computing kernal matrix K0 and K1
### Alternatively, we can use sklearn.metrics.pairwise.rbf_kernel
self.K = []
self.K.append(rbf_kernel(X,X0,gamma=self.gamma))
self.K.append(rbf_kernel(X,X1,gamma=self.gamma))
self.K.append(self.Kernal_matrix(X,X0,gamma=self.gamma))
self.K.append(self.Kernal_matrix(X,X0,gamma=self.gamma))
### Computing N, N is symmetric
for i in range(1):
self.N += self.K[i] @ (np.eye(l[i]) - np.full((l[i],l[i]),1./l[i])) @ self.K[i].T
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment