MDS 降维

MDS 降维:点击打开链接


import numpy as np

import tensorflow as tf
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

data = []

cty_data = []
def data_p(fliename):
    fr = open(fliename)

    tid = 0
    cid = 0
    for line in fr.readlines():     
        tline = line.strip().split(';')

        data.append(tline)
        
        cn = tline[0]
        if tid==0:
            cty_data.append([tid])
            #print(cty_data)
            #print(cty_data[cid][0])
        elif cn == data[cty_data[cid][0]][0]:
            cty_data[cid].append(tid)
        else:
            cid += 1
            cty_data.append([tid])

        tid += 1
            

data_p('C:\\Users\\imac\\Desktop\\2018\\bigdata\\py\\wealth1951.txt')
cty_data = np.asarray(cty_data)
#print(cty_data)
data = np.asarray(data)
#print(data[cty_data[:,:],0])

cdata = []

def getdata():
    for i in range(len(cty_data)):
        itm =  data[cty_data[i,-1],3:6]

        cdata.append(itm)

   
getdata()
cdata = np.asarray(cdata,dtype=np.float32)

def dis(v1,v2):
    return np.sqrt(sum(np.power(v1-v2,2)))

lenn = len(cdata)
DD = np.zeros([lenn,lenn])

for i in range(lenn):
    for j in range(i,lenn):
        dist = dis(cdata[i],cdata[j])
        DD[i,j] = dist
        DD[j,i] = dist

#print(DD)

D=np.array([[0,411,213,219,296,397],
            [411,0,204,203,120,152],
            [213,204,0,73,136,245],
            [219,203,73,0,90,191],
            [296,120,136,90,0,109],
            [ 397,152,245,191,109,0]])

def MDS(D,k):
    N = D.shape[0]
    T = np.zeros([N,N])

    D2 = D**2
    H = np.eye(N) - 1/N
    T = -0.5 * np.dot(np.dot(H, D2), H)

    eigVal, eigVec = np.linalg.eig(T)
    indices = np.argsort(eigVal)# 返回从小到大的索引值

    indices = indices[::-1] # 反转
    eigVal = eigVal[indices]
    eigVec = eigVec[:,indices]

    m = eigVec[:,:k]
    n = np.diag(np.sqrt(eigVal[:k]))
    X = np.dot(m,n)

    print(eigVal.shape)
    print(m.shape)

    print(n.shape)
    print('original distance','\tnew distance')
    for i in range(N):
        for j in range(i+1,N):
            print(np.str(D[i,j]),'\t\t',np.str("%.4f"%np.linalg.norm(X[i]-X[j])))
    
    return X

X = MDS(DD,2)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')  
ax.scatter(X[:, 0], X[:, 1]) 

plt.show()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值