当前位置: 首页>>代码示例>>Python>>正文


Python KNN.knn方法代码示例

本文整理汇总了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]
开发者ID:philipz1,项目名称:ML,代码行数:34,代码来源:LLE.py

示例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]
开发者ID:philipz1,项目名称:ML,代码行数:20,代码来源:Isomap.py

示例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))
开发者ID:philipz1,项目名称:ML,代码行数:20,代码来源:LaplacianEigenmap.py

示例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]
开发者ID:philipz1,项目名称:ML,代码行数:24,代码来源:LaplacianEigenmap.py


注:本文中的KNN.knn方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。