# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets,manifold
def load_data():
'''
加载用于降维的数据
'''
# 使用 scikit-learn 自带的 iris 数据集
iris=datasets.load_iris()
return iris.data,iris.target
#多维缩放降维MDS模型
def test_MDS(*data):
X,y=data
# 依次考察降维目标为 4维、3维、2维、1维
for n in [4,3,2,1]:
mds=manifold.MDS(n_components=n)
mds.fit(X)
print('stress(n_components=%d) : %s'% (n, str(mds.stress_)))
# 产生用于降维的数据集
X,y=load_data()
# 调用 test_MDS
test_MDS(X,y)
def plot_MDS(*data):
'''
绘制经过 使用 MDS 降维到二维之后的样本点
'''
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
plot_MDS(X,y)