# -*- coding: utf-8 -*-
import numpy as np
def mds(data,d):
'''
input:data(ndarray):待降维数据
d(int):降维后数据维数
output:Z(ndarray):降维后的数据
'''
#********* Begin *********#
#计算dist2,dist2i,dist2j,dist2ij
m,n = data.shape
dist =np.zeros((m,m))
disti = np.zeros(m)
distj = np.zeros(m)
B = np.zeros((m,m))
for i in range(m):
dist[i] = np.sum(np.square(data[i]-data),axis=1).reshape(1,m)
for i in range(m):
disti[i] = np.mean(dist[i,:])
distj[i] = np.mean(dist[:,i])
distij = np.mean(dist)
#计算B
for i in range(m):
for j in range(m):
B[i,j] = -0.5*(dist[i,j] - disti[i] - distj[j] + distij)
#矩阵分解得到特征值与特征向量
lamda,V=np.linalg.eigh(B)
#计算Z
index=np.argsort(-lamda)[:d]
diag_lamda=np.sqrt(np.diag(-np.sort(-lamda)[:d]))
V_selected=V[:,index]
Z=V_selected.dot(diag_lamda)
#********* End *********#
return Z
第一关
第二关
# -*- coding: utf-8 -*-
from sklearn.manifold import MDS
def mds(data,d):
'''
input:data(ndarray):待降维数据
d(int):降维后数据维度
output:Z(ndarray):降维后数据
'''
#********* Begin *********#
mds = MDS(d)
Z = mds.fit_transform(data)
#********* End *********#
return Z