本文整理汇总了Python中KNN.knn方法的典型用法代码示例。如果您正苦于以下问题:Python KNN.knn方法的具体用法?Python KNN.knn怎么用?Python KNN.knn使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类KNN
的用法示例。
在下文中一共展示了KNN.knn方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: lle
# 需要导入模块: import KNN [as 别名]
# 或者: from KNN import knn [as 别名]
def lle(data, k = 10, target_dim = 2):
p = data.shape[1]
graph = KNN.knn(data, k)
n = len(graph.keys())
weights_vec = np.zeros((n,k))
weights_dict = {}
locals_ = np.zeros((n,k))
for i, key in enumerate(list(graph.keys())):
local = construct_knn_vector(key, graph, k)
local_centered = local - np.repeat(np.array(key).reshape([1, p]), k, axis = 0)
gram = do_gram(local_centered, k)
w_num = np.dot(np.linalg.inv(gram), np.ones(gram.shape[0]).T)
w = w_num / w_num.sum()
weights_vec[i] = w
temp_dict = {}
for q in range(len(local)):
temp_dict[tuple(local[q])] = w[q]
weights_dict[tuple(key)] = temp_dict
weights = reconstruct(data, weights_dict)
M = np.dot((np.identity(n) - weights).T, (np.identity(n) - weights))
eigvals, eigvecs = np.linalg.eigh(M)
index = np.argsort(eigvals)[::1]
eigvals = eigvals[index]
eigvecs = eigvecs[:,index]
return eigvecs[:,1:target_dim + 1]
示例2: isomap
# 需要导入模块: import KNN [as 别名]
# 或者: from KNN import knn [as 别名]
def isomap(data, k = 10, target_dim = 2, load = False, save = False):
if load == False:
graph = KNN.knn(data, k)
A = construct_A(data, graph)
dists = shortest_dist_weight(A)
if save != False:
np.save(save, dists)
else:
dists = np.load(load)
gram_tilda = do_gram_tilda(dists)
eigvals, eigvecs = np.linalg.eigh(gram_tilda)
index = np.argsort(eigvals)[::-1]
eigvals = eigvals[index]
eigvecs = eigvecs[:,index]
return eigvecs[:,0:target_dim]
示例3: le
# 需要导入模块: import KNN [as 别名]
# 或者: from KNN import knn [as 别名]
def le(data, k = 10, target_dim = 2):
graph = KNN.knn(data, k)
A = construct_mesh(data, graph)
from sklearn import manifold
return(manifold.spectral_embedding(A, 2))
D = construct_degree(A)
L = D - A
eigvals, eigvecs = scipy.linalg.eigh(A, L)
index = np.argsort(eigvals)[::-1]
eigvals = eigvals[index]
eigvecs = eigvecs[:,index]
return eigvecs[:,1:target_dim + 1]
# print(le(npdata))
示例4: le
# 需要导入模块: import KNN [as 别名]
# 或者: from KNN import knn [as 别名]
def le(data, k = 10, target_dim = 2):
graph = KNN.knn(data, k)
A = construct_mesh(data, graph)
# from sklearn import manifold
# return(manifold.spectral_embedding(A, 2))
D = np.diag(A.sum(1))
# L = D - A
# print(D**(-1/2))
x = D ** (-1/2)
x[np.isinf(x)] = 0
L = np.dot(x, D-A)
L = np.dot(L, x)
# L[np.isinf(L)] = 0
eigvals, eigvecs = eig(L, D)
index = np.argsort(eigvals)[::1]
eigvals = eigvals[index]
eigvecs = eigvecs[:,index]
return eigvecs[:, 1: 1 + target_dim]