模型原型
class sklearn.manifold.MDS(n_components=2,metric=True,n_init=4,max_iter=300,verbose=0,eps=0.001,n_jobs=1,random_state=None,dissimilarity=’euclidean’)
参数
- n_components:低维空间
- metric:
- True:距离度量
- False:非距离度量SMACOF
- n_init:初始化的次数
- max_iter
- eps:收敛阙值
- n_jobs
- random_state
- dissimilarity:用于定义图和计算不相似性
- ’euclidean’:欧式距离
- ‘precomputed’:由使用者提供距离矩阵
属性
- embedding_:给出了原始数据集在低维空间中的嵌入矩阵
- stress_:给出了不一致的距离的总和
- ncomponents:指示主成分有多少个元素
方法
- fit(X[,y,init])
- fit_transform(X,[,y,init]):训练模型并返回转换后的低维坐标
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets,decomposition,manifold
加载数据
def load_data():
iris=datasets.load_iris()
return iris.data,iris.target
使用MDS类
def test_MDS(*data):
X,y=data
for n in [4,3,2,1]:
mds=manifold.MDS(n_components=n)
mds.fit(X,y)
print('stress(n_components=%d):%s'%(n,str(mds.stress_)))
X,y=load_data()
test_MDS(X,y)
降维后样本的分布图
def plot_MDS(*data):
X,y=data
mds=manifold.MDS(n_components=2)
X_r=mds.fit_transform(X)
fig=plt.figure()
ax=fig.add_subplot(1,1,1)
colors=((1,0,0),(0,1,0),(0,0,1),(0.5,0.5,0),(0,0.5,0.5),(0.5,0,0.5),
(0.4,0.6,0),(0.6,0.4,0),(0,0.6,0.4),(0.5,0.3,0.2),)
for label,color in zip(np.unique(y),colors):
position=y==label
ax.scatter(X_r[position,0],X_r[position,1],label='target=%d'%label,color=color)
ax.set_xlabel=('X[0]')
ax.set_ylabel=('X[1]')
ax.legend(loc='best')
ax.set_title('MDS')
plt.show()
plot_MDS(X,y)