数据降维——MDS

模型原型
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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值