當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。