微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我们是否有任何方法可以在 sklearn 中使用重心进行局部线性嵌入

如何解决我们是否有任何方法可以在 sklearn 中使用重心进行局部线性嵌入

参考本网站的代码 http://fa.bianp.net/blog/2011/locally-linear-embedding-and-sparse-eigensolvers/

from scipy.sparse import linalg,eye
from pyamg import smoothed_aggregation_solver
from sklearn import neighbors
import numpy as np

def locally_linear_embedding(X,n_neighbors,out_dim,tol=1e-6,max_iter=200):
    W = neighbors.kneighbors_graph(
        X,n_neighbors=n_neighbors,mode='barycenter',include_self=False)
    #"connectivity" or "distance"

    # M = (I-W)' (I-W)
    A = eye(*W.shape,format=W.format) - W
    A = (A.T).dot(A).tocsr()

    # initial approximation to the eigenvectors
    X = np.random.rand(W.shape[0],out_dim)
    ml = smoothed_aggregation_solver(A,symmetry='symmetric')
    prec = ml.aspreconditioner()

    # compute eigenvalues and eigenvectors with LOBPCG
    eigen_values,eigen_vectors = linalg.lobpcg(
        A,X,M=prec,largest=False,tol=tol,maxiter=max_iter)

    index = np.argsort(eigen_values)
    return eigen_vectors[:,index],np.sum(eigen_values)

我正在尝试根据本研究 https://github.com/liuyanfang023/KBS-RNE 中的代码为我的作业实施鲁棒邻域嵌入,但由于 LLE 与原始结果不相似。

这是我应用的代码

def robust_neighbor_embedding(X,max_iter=200):

#recevied from LLE
W = neighbors.kneighbors_graph(
    X,mode='distance')
#"connectivity" or "distance"
#n,d = X.shape
X = np.random.rand(W.shape[0],out_dim)
n,d = X.shape
H = np.random.rand(d,n)
## W

# M = (I-W)' (I-W)
#A = eye(*W.shape,format=W.format) - W
#A = (A.T).dot(A).tocsr()
A = ((eye(n) - np.transpose(W)) * X)#.tocsr()
AA = np.dot(A.T,A) #.tocsr()
AAplus = 0.5 * (np.abs(AA) + AA)
AAsubtract = 0.5 * (np.abs(AA) - AA)

#initialize H,Y,alpha,gamma,gamma_max,mu
O = np.dot(A,H)
Y = np.zeros((n,2000))
mu = 1.1
gamma = 10
max_gamma = 10000000000.0
alpha = 1000.0

AM = np.dot(np.transpose(A),O)
AY =np.dot(np.transpose(A),Y)
AMplus = 0.5 * (np.abs(AM) + AM)
AMsubtract = 0.5 * (np.abs(AM) - AM)
AYplus = 0.5 * (np.abs(AY) + AY)
AYsubtract = 0.5 * (np.abs(AY) - AY)
iter_num = 50
iter_numH = 30

G1 = np.diag(np.sqrt(1.0 / np.diag(np.dot(np.transpose(H),H)) + 1))
H = np.dot(H,G1)
H = np.multiply(H,np.sqrt((alpha * H) + (gamma * AMplus)+np.dot(gamma*AAsubtract,H) + AYplus/ np.dot((alpha * H ),np.dot(np.transpose(H),H))+gamma * AMsubtract + gamma * np.dot(AAplus,H)+ AYsubtract + 1))
H

# compute eigenvalues and eigenvectors 
eigen_values,eigen_vectors = np.linalg.eigh(np.dot(np.transpose(H),H))

index = np.argsort(eigen_values)

return eigen_vectors[:,np.sum(eigen_values)

我使用 LLE 的结果将重心更改为连接模式

enter image description here

我与 RNE 的结果(我不确定结果会是什么)

enter image description here

但在我看来,如果 LLE 函数执行真正的结果,我认为我的 RNE 结果会更正确

提前谢谢你T__T

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。