它与 MNIST 数据一起使用。你可以在这个网站上找到 mnist 数据:
代码中用到了四个文件,即
train-images-idx3-ubyte.gz:训练集图像(9912422 字节)
train-labels-idx1-ubyte.gz:训练集标签(28881字节)
t10k-images-idx3-ubyte.gz:测试集图像(1648877 字节)
t10k-labels-idx1-ubyte.gz:测试集标签(4542字节)
我首先使用PCA Whitening处理数据,然后使用LDF来判断样本是c1还是c2,其中c1和c2是类别。
很奇怪的是
我设置c1为0,c2为1,正确的是0.8122931442080378,但是,当我简单地将c2改为0,c1改为1时,正确的改为0.5938534278959811 !!!!!!
我尝试其他情况,例如交换 1 和 9,或交换 1 和 7。在上述情况下,正确的没有变化,分别为 0.9925373134328358 和 0.9889042995839112
请告诉我为什么。非常感谢!
我的代码如下:
请记住在第 47/48 行更改 os 路径
import os
import struct
import numpy as np
import math
def ldf(c1,c2):
def load_mnist(path, kind):
"""Load MNIST data from `path`"""
labels_path = os.path.join(path,
'%s-labels-idx1-ubyte'
% kind)
images_path = os.path.join(path,
'%s-images-idx3-ubyte'
% kind)
with open(labels_path, 'rb') as lbpath:
magic, n = struct.unpack('>II',
lbpath.read(8))
labels = np.fromfile(lbpath,
dtype=np.uint8)
with open(images_path, 'rb') as imgpath:
magic, num, rows, cols = struct.unpack('>IIII',
imgpath.read(16))
images = np.fromfile(imgpath,
dtype=np.uint8).reshape(len(labels), 784)
return images, labels
X_train,y_train = load_mnist('./num/MNIST/raw','train')
X_test,y_test = load_mnist('./num/MNIST/raw','t10k')
X_train0=X_train[y_train==c1]
X_train1=X_train[y_train==c2]
y_train0=y_train[y_train==c1]
y_train1=y_train[y_train==c2]
X_test0=X_test[y_test==c1]
X_test1=X_test[y_test==c2]
y_test0=y_test[y_test==c1]
y_test1=y_test[y_test==c2]
X_test01=np.append(X_test0,X_test1,axis=0)
y_test01=np.append(y_test0,y_test1,axis=0)
X_train0=[[1 if i >0 else 0 for i in j] for j in X_train0]
X_train1=[[1 if i >0 else 0 for i in j] for j in X_train1]
X_test01=[[1 if i >0 else 0 for i in j] for j in X_test01]
#把大于0的全赋值为1
X_train0=np.array(X_train0)
X_train1=np.array(X_train1)
X_test01=np.array(X_test01)
X_train01=np.append(X_train0,X_train1,axis=0)
y_train01=np.append(y_train0,y_train1,axis=0)
m0=np.size(X_train0,0)
m1=np.size(X_train1,0)
m=np.size(X_train01,0)
m_t=np.size(X_test01,0)
n=np.size(X_train01,1)
sumu=np.zeros([n,1])
for i in X_train01:
i=i.reshape(n,1)
sumu+=i
u=sumu/m
u=u.reshape(1,n)
#去中心化
X_train01_c=X_train01-u
sigma=np.dot(X_train01_c.T,X_train01_c)/m
D,U= np.linalg.eig(sigma)
#对特征矩阵进行变换
D=np.diag(D)
D=np.real(D)
U=np.real(U)
for i in range(n):
if D[i][i]==0:
D[i][i]==10e-10
elif D[i][i]<0:
D[i][i]=-D[i][i]
D[i][i]=1/(math.sqrt(D[i][i]))
else:
D[i][i]=1/(math.sqrt(D[i][i]))
#D^{-1/2}
W=np.dot(D,U.T)
Y_train=np.dot(X_train01_c,W.T)#PCA白化
Y_train0_c=Y_train[y_train01==c1]
Y_train1_c=Y_train[y_train01==c2]
sumu0=np.zeros([n,1])
sumu1=np.zeros([n,1])
for i in Y_train0_c:
i=i.reshape(n,1)
sumu0+=i
u0=sumu0/m0
for i in Y_train1_c:
i=i.reshape(n,1)
sumu1+=i
u1=sumu1/m1
p0=m0/m
p1=m1/m
def decision(xk):
x=xk.reshape(n,1)
g0=np.dot(u0.T,x)-(1/2)*np.dot(u0.T,u0)+math.log(p0)
g1=np.dot(u1.T,x)-(1/2)*np.dot(u1.T,u1)+math.log(p1)
if g0>g1:
return c1
else:
return c2
ldf=np.zeros(m_t)
#对测试集进行数据处理
#去中心化
X_test01_c=X_test01-u
Y_test=np.dot(X_test01_c,W.T)#PCA白化
for i in range(m_t):
ldf[i]=decision(Y_test[i])
count=0
for i in range(m_t):
if ldf[i]==y_test01[i]:
count+=1
correct=count/m_t
return correct
ldf(0,1)