LLE降维——代码实现

本文介绍了使用Python实现局部线性嵌入(LLE)降维的方法,包括算法原理、代码实现、测试过程及初步的测试结果。由于代码尚需完善,后续会进行更新补充。
摘要由CSDN通过智能技术生成

参考文章:

  1. 算法实现
#!/usr/bin/env python
# coding: utf-8

# ## LLE算法实现

# In[2]:


'''
LLE 2019.11.20
Author: luo
Reference:
    Zhihua Zhou. Machine learning[M]. Tsinghua University Press, 2016

实现了LLE
总结:对比研究了LLE的在不同近邻参数下的降维效果,并与sklearn中的LLE进行了比较。
同时将降维后的数据应用于softmax训练
'''

import numpy as np
from scipy.spatial.distance import pdist, squareform
import matplotlib.pyplot as plt


'''
define get_k_maxtria function.
Input:
    D:numpy.ndarry,  size: [num_sample, num_feature]
    k: float,  the nearest neighbor parameter k
Return:
    k_idx: numpy.ndarry,  size: [num_sample, k], the index of the k nearest neighbor
'''
def get_k_maxtria(D, k):
    dist = pdist(D, 'euclidean')     # 获得距离矩阵
    dist = squareform(dist)          # 转化为方阵
    m = dist.shape[0]
    k_idx = np.zeros([m, k])
    for i in range(m):
        topk = np.argsort(dist[i])[1:k + 1]   # 从1开始,是因为最小那个距离是它本身, 返回最小的k个的索引
        k_idx[i] = k_idx[i] + topk
    return k_idx.astype(np.int32)

'''
define get_w function.
Input:
    D:numpy.ndarry,  size: [num_sample, num_feature]
    kear_idx: numpy.ndarry,  size: [num_sample, k]
    k: float,  the nearest neighbor parameter k
Return:
    w: numpy.ndarry,  size: [num_sample, k], Linear reconstruction coefficient matrix
'''
def get_w(D, knear_idx, k):
    m = D.shape[0]
    w = np.zeros([m, k])
    I = np.ones((k, 1))
    for i in range(m):
        Q_x = D[knear_idx[i]]       
        xi = D[i]              
        xi 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值