本文整理汇总了Python中sklearn.manifold.LocallyLinearEmbedding方法的典型用法代码示例。如果您正苦于以下问题:Python manifold.LocallyLinearEmbedding方法的具体用法?Python manifold.LocallyLinearEmbedding怎么用?Python manifold.LocallyLinearEmbedding使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.manifold
的用法示例。
在下文中一共展示了manifold.LocallyLinearEmbedding方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: learn_manifold
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import LocallyLinearEmbedding [as 别名]
def learn_manifold(manifold_type, feats, n_components=2):
if manifold_type == 'tsne':
feats_fitted = manifold.TSNE(n_components=n_components, random_state=0).fit_transform(feats)
elif manifold_type == 'isomap':
feats_fitted = manifold.Isomap(n_components=n_components).fit_transform(feats)
elif manifold_type == 'mds':
feats_fitted = manifold.MDS(n_components=n_components).fit_transform(feats)
elif manifold_type == 'spectral':
feats_fitted = manifold.SpectralEmbedding(n_components=n_components).fit_transform(feats)
else:
raise Exception('wrong maniford type!')
# methods = ['standard', 'ltsa', 'hessian', 'modified']
# feats_fitted = manifold.LocallyLinearEmbedding(n_components=n_components, method=methods[0]).fit_transform(pred)
return feats_fitted
示例2: test_lle_simple_grid
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import LocallyLinearEmbedding [as 别名]
def test_lle_simple_grid():
# note: ARPACK is numerically unstable, so this test will fail for
# some random seeds. We choose 20 because the tests pass.
rng = np.random.RandomState(20)
tol = 0.1
# grid of equidistant points in 2D, n_components = n_dim
X = np.array(list(product(range(5), repeat=2)))
X = X + 1e-10 * rng.uniform(size=X.shape)
n_components = 2
G = geom.Geometry(adjacency_kwds = {'radius':3})
G.set_data_matrix(X)
tol = 0.1
distance_matrix = G.compute_adjacency_matrix()
N = lle.barycenter_graph(distance_matrix, X).todense()
reconstruction_error = np.linalg.norm(np.dot(N, X) - X, 'fro')
assert(reconstruction_error < tol)
for eigen_solver in EIGEN_SOLVERS:
clf = lle.LocallyLinearEmbedding(n_components = n_components, geom = G,
eigen_solver = eigen_solver, random_state = rng)
clf.fit(X)
assert(clf.embedding_.shape[1] == n_components)
reconstruction_error = np.linalg.norm(
np.dot(N, clf.embedding_) - clf.embedding_, 'fro') ** 2
assert(reconstruction_error < tol)
示例3: test_lle_manifold
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import LocallyLinearEmbedding [as 别名]
def test_lle_manifold():
rng = np.random.RandomState(0)
# similar test on a slightly more complex manifold
X = np.array(list(product(np.arange(18), repeat=2)))
X = np.c_[X, X[:, 0] ** 2 / 18]
X = X + 1e-10 * rng.uniform(size=X.shape)
n_components = 2
G = geom.Geometry(adjacency_kwds = {'radius':3})
G.set_data_matrix(X)
distance_matrix = G.compute_adjacency_matrix()
tol = 1.5
N = lle.barycenter_graph(distance_matrix, X).todense()
reconstruction_error = np.linalg.norm(np.dot(N, X) - X)
assert(reconstruction_error < tol)
for eigen_solver in EIGEN_SOLVERS:
clf = lle.LocallyLinearEmbedding(n_components = n_components, geom = G,
eigen_solver = eigen_solver, random_state = rng)
clf.fit(X)
assert(clf.embedding_.shape[1] == n_components)
reconstruction_error = np.linalg.norm(
np.dot(N, clf.embedding_) - clf.embedding_, 'fro') ** 2
assert(reconstruction_error < tol)
示例4: test_lle_simple_grid
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import LocallyLinearEmbedding [as 别名]
def test_lle_simple_grid():
# note: ARPACK is numerically unstable, so this test will fail for
# some random seeds. We choose 2 because the tests pass.
rng = np.random.RandomState(2)
# grid of equidistant points in 2D, n_components = n_dim
X = np.array(list(product(range(5), repeat=2)))
X = X + 1e-10 * rng.uniform(size=X.shape)
n_components = 2
clf = manifold.LocallyLinearEmbedding(n_neighbors=5,
n_components=n_components,
random_state=rng)
tol = 0.1
N = barycenter_kneighbors_graph(X, clf.n_neighbors).toarray()
reconstruction_error = linalg.norm(np.dot(N, X) - X, 'fro')
assert_less(reconstruction_error, tol)
for solver in eigen_solvers:
clf.set_params(eigen_solver=solver)
clf.fit(X)
assert clf.embedding_.shape[1] == n_components
reconstruction_error = linalg.norm(
np.dot(N, clf.embedding_) - clf.embedding_, 'fro') ** 2
assert_less(reconstruction_error, tol)
assert_almost_equal(clf.reconstruction_error_,
reconstruction_error, decimal=1)
# re-embed a noisy version of X using the transform method
noise = rng.randn(*X.shape) / 100
X_reembedded = clf.transform(X + noise)
assert_less(linalg.norm(X_reembedded - clf.embedding_), tol)
示例5: test_lle_manifold
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import LocallyLinearEmbedding [as 别名]
def test_lle_manifold():
rng = np.random.RandomState(0)
# similar test on a slightly more complex manifold
X = np.array(list(product(np.arange(18), repeat=2)))
X = np.c_[X, X[:, 0] ** 2 / 18]
X = X + 1e-10 * rng.uniform(size=X.shape)
n_components = 2
for method in ["standard", "hessian", "modified", "ltsa"]:
clf = manifold.LocallyLinearEmbedding(n_neighbors=6,
n_components=n_components,
method=method, random_state=0)
tol = 1.5 if method == "standard" else 3
N = barycenter_kneighbors_graph(X, clf.n_neighbors).toarray()
reconstruction_error = linalg.norm(np.dot(N, X) - X)
assert_less(reconstruction_error, tol)
for solver in eigen_solvers:
clf.set_params(eigen_solver=solver)
clf.fit(X)
assert clf.embedding_.shape[1] == n_components
reconstruction_error = linalg.norm(
np.dot(N, clf.embedding_) - clf.embedding_, 'fro') ** 2
details = ("solver: %s, method: %s" % (solver, method))
assert_less(reconstruction_error, tol, msg=details)
assert_less(np.abs(clf.reconstruction_error_ -
reconstruction_error),
tol * reconstruction_error, msg=details)
# Test the error raised when parameter passed to lle is invalid
示例6: test_lle_init_parameters
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import LocallyLinearEmbedding [as 别名]
def test_lle_init_parameters():
X = np.random.rand(5, 3)
clf = manifold.LocallyLinearEmbedding(eigen_solver="error")
msg = "unrecognized eigen_solver 'error'"
assert_raise_message(ValueError, msg, clf.fit, X)
clf = manifold.LocallyLinearEmbedding(method="error")
msg = "unrecognized method 'error'"
assert_raise_message(ValueError, msg, clf.fit, X)
示例7: test_pipeline
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import LocallyLinearEmbedding [as 别名]
def test_pipeline():
# check that LocallyLinearEmbedding works fine as a Pipeline
# only checks that no error is raised.
# TODO check that it actually does something useful
from sklearn import pipeline, datasets
X, y = datasets.make_blobs(random_state=0)
clf = pipeline.Pipeline(
[('filter', manifold.LocallyLinearEmbedding(random_state=0)),
('clf', neighbors.KNeighborsClassifier())])
clf.fit(X, y)
assert_less(.9, clf.score(X, y))
# Test the error raised when the weight matrix is singular
示例8: test_integer_input
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import LocallyLinearEmbedding [as 别名]
def test_integer_input():
rand = np.random.RandomState(0)
X = rand.randint(0, 100, size=(20, 3))
for method in ["standard", "hessian", "modified", "ltsa"]:
clf = manifold.LocallyLinearEmbedding(method=method, n_neighbors=10)
clf.fit(X) # this previously raised a TypeError
示例9: test_ltsa_with_sklearn
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import LocallyLinearEmbedding [as 别名]
def test_ltsa_with_sklearn():
N = 10
X, color = datasets.samples_generator.make_s_curve(N, random_state=0)
n_components = 2
n_neighbors = 3
knn = NearestNeighbors(n_neighbors + 1).fit(X)
G = geom.Geometry()
G.set_data_matrix(X)
G.set_adjacency_matrix(knn.kneighbors_graph(X, mode = 'distance'))
sk_Y_ltsa = manifold.LocallyLinearEmbedding(n_neighbors, n_components,
method = 'ltsa',
eigen_solver = 'arpack').fit_transform(X)
(mm_Y_ltsa, err) = ltsa.ltsa(G, n_components, eigen_solver = 'arpack')
assert(_check_with_col_sign_flipping(sk_Y_ltsa, mm_Y_ltsa, 0.05))
示例10: test_lle_with_sklearn
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import LocallyLinearEmbedding [as 别名]
def test_lle_with_sklearn():
N = 10
X, color = datasets.samples_generator.make_s_curve(N, random_state=0)
n_components = 2
n_neighbors = 3
knn = NearestNeighbors(n_neighbors + 1).fit(X)
G = geom.Geometry()
G.set_data_matrix(X)
G.set_adjacency_matrix(knn.kneighbors_graph(X, mode = 'distance'))
sk_Y_lle = manifold.LocallyLinearEmbedding(n_neighbors, n_components, method = 'standard').fit_transform(X)
(mm_Y_lle, err) = lle.locally_linear_embedding(G, n_components)
assert(_check_with_col_sign_flipping(sk_Y_lle, mm_Y_lle, 0.05))
示例11: test_objectmapper
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import LocallyLinearEmbedding [as 别名]
def test_objectmapper(self):
df = pdml.ModelFrame([])
self.assertIs(df.manifold.LocallyLinearEmbedding,
manifold.LocallyLinearEmbedding)
self.assertIs(df.manifold.Isomap, manifold.Isomap)
self.assertIs(df.manifold.MDS, manifold.MDS)
self.assertIs(df.manifold.SpectralEmbedding, manifold.SpectralEmbedding)
self.assertIs(df.manifold.TSNE, manifold.TSNE)
示例12: test_lle_simple_grid
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import LocallyLinearEmbedding [as 别名]
def test_lle_simple_grid():
# note: ARPACK is numerically unstable, so this test will fail for
# some random seeds. We choose 2 because the tests pass.
rng = np.random.RandomState(2)
# grid of equidistant points in 2D, n_components = n_dim
X = np.array(list(product(range(5), repeat=2)))
X = X + 1e-10 * rng.uniform(size=X.shape)
n_components = 2
clf = manifold.LocallyLinearEmbedding(n_neighbors=5,
n_components=n_components,
random_state=rng)
tol = 0.1
N = barycenter_kneighbors_graph(X, clf.n_neighbors).toarray()
reconstruction_error = linalg.norm(np.dot(N, X) - X, 'fro')
assert_less(reconstruction_error, tol)
for solver in eigen_solvers:
clf.set_params(eigen_solver=solver)
clf.fit(X)
assert_true(clf.embedding_.shape[1] == n_components)
reconstruction_error = linalg.norm(
np.dot(N, clf.embedding_) - clf.embedding_, 'fro') ** 2
assert_less(reconstruction_error, tol)
assert_almost_equal(clf.reconstruction_error_,
reconstruction_error, decimal=1)
# re-embed a noisy version of X using the transform method
noise = rng.randn(*X.shape) / 100
X_reembedded = clf.transform(X + noise)
assert_less(linalg.norm(X_reembedded - clf.embedding_), tol)
示例13: test_lle_manifold
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import LocallyLinearEmbedding [as 别名]
def test_lle_manifold():
rng = np.random.RandomState(0)
# similar test on a slightly more complex manifold
X = np.array(list(product(np.arange(18), repeat=2)))
X = np.c_[X, X[:, 0] ** 2 / 18]
X = X + 1e-10 * rng.uniform(size=X.shape)
n_components = 2
for method in ["standard", "hessian", "modified", "ltsa"]:
clf = manifold.LocallyLinearEmbedding(n_neighbors=6,
n_components=n_components,
method=method, random_state=0)
tol = 1.5 if method == "standard" else 3
N = barycenter_kneighbors_graph(X, clf.n_neighbors).toarray()
reconstruction_error = linalg.norm(np.dot(N, X) - X)
assert_less(reconstruction_error, tol)
for solver in eigen_solvers:
clf.set_params(eigen_solver=solver)
clf.fit(X)
assert_true(clf.embedding_.shape[1] == n_components)
reconstruction_error = linalg.norm(
np.dot(N, clf.embedding_) - clf.embedding_, 'fro') ** 2
details = ("solver: %s, method: %s" % (solver, method))
assert_less(reconstruction_error, tol, msg=details)
assert_less(np.abs(clf.reconstruction_error_ -
reconstruction_error),
tol * reconstruction_error, msg=details)
# Test the error raised when parameter passed to lle is invalid
示例14: see_iso_map
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import LocallyLinearEmbedding [as 别名]
def see_iso_map(bottlenecks, labels, suptitle=None):
"""
:param bottlenecks:
:param labels:
:param suptitle: String to add as plot suptitles
:return: Nothing, will just plot a scatter plot to show the distribution of our data after dimensionality reduction.
"""
n_samples, n_features = bottlenecks.shape
n_neighbors = 25
n_components = 2
start_index_outlier = np.where(labels == 1)[0][0]
alpha_inlier = 0.25
B_iso = manifold.Isomap(n_neighbors, n_components).fit_transform(bottlenecks)
B_pca = decomposition.TruncatedSVD(n_components=2).fit_transform(bottlenecks)
B_lle = manifold.LocallyLinearEmbedding(n_neighbors, n_components, method='standard').fit_transform(bottlenecks)
B_spec = manifold.SpectralEmbedding(n_components=n_components, random_state=42,
eigen_solver='arpack').fit_transform(bottlenecks)
plt.figure()
plt.subplot(221)
plt.scatter(B_iso[:start_index_outlier, 0], B_iso[:start_index_outlier, 1], marker='o', c='b', alpha=alpha_inlier)
plt.scatter(B_iso[start_index_outlier:, 0], B_iso[start_index_outlier:, 1], marker='^', c='k')
plt.title("Isomap projection")
plt.subplot(222)
inlier_scatter = plt.scatter(B_lle[:start_index_outlier, 0], B_lle[:start_index_outlier, 1], marker='o', c='b',
alpha=alpha_inlier)
outlier_scatter = plt.scatter(B_lle[start_index_outlier:, 0], B_lle[start_index_outlier:, 1], marker='^', c='k')
plt.legend([inlier_scatter, outlier_scatter], ['Inliers', 'Outliers'], loc='lower left')
plt.title("Locally Linear Embedding")
plt.subplot(223)
plt.scatter(B_pca[:start_index_outlier, 0], B_pca[:start_index_outlier, 1], marker='o', c='b', alpha=alpha_inlier)
plt.scatter(B_pca[start_index_outlier:, 0], B_pca[start_index_outlier:, 1], marker='^', c='k')
plt.title("Principal Components projection")
plt.subplot(224)
plt.scatter(B_spec[:start_index_outlier, 0], B_spec[:start_index_outlier, 1], marker='o', c='b', alpha=alpha_inlier)
plt.scatter(B_spec[start_index_outlier:, 0], B_spec[start_index_outlier:, 1], marker='^', c='k')
plt.title("Spectral embedding")
if suptitle:
plt.suptitle(suptitle)
示例15: visualize_encodings
# 需要导入模块: from sklearn import manifold [as 别名]
# 或者: from sklearn.manifold import LocallyLinearEmbedding [as 别名]
def visualize_encodings(encodings, file_name=None,
grid=None, skip_every=999, fast=False, fig=None, interactive=False):
encodings = manual_pca(encodings)
if encodings.shape[1] <= 3:
return print_data_only(encodings, file_name, fig=fig, interactive=interactive)
encodings = encodings[0:720]
hessian_euc = dist.squareform(dist.pdist(encodings[0:720], 'euclidean'))
hessian_cos = dist.squareform(dist.pdist(encodings[0:720], 'cosine'))
grid = (3, 4) if grid is None else grid
project_ops = []
n = 2
project_ops.append(("LLE ltsa N:%d" % n, mn.LocallyLinearEmbedding(10, n, method='ltsa')))
project_ops.append(("LLE modified N:%d" % n, mn.LocallyLinearEmbedding(10, n, method='modified')))
project_ops.append(('MDS euclidean N:%d' % n, mn.MDS(n, max_iter=300, n_init=1, dissimilarity='precomputed')))
project_ops.append(("TSNE 30/2000 N:%d" % n, TSNE(perplexity=30, n_components=n, init='pca', n_iter=2000)))
n = 3
project_ops.append(("LLE ltsa N:%d" % n, mn.LocallyLinearEmbedding(10, n, method='ltsa')))
project_ops.append(("LLE modified N:%d" % n, mn.LocallyLinearEmbedding(10, n, method='modified')))
project_ops.append(('MDS euclidean N:%d' % n, mn.MDS(n, max_iter=300, n_init=1, dissimilarity='precomputed')))
project_ops.append(('MDS cosine N:%d' % n, mn.MDS(n, max_iter=300, n_init=1, dissimilarity='precomputed')))
plot_places = []
for i in range(12):
u, v = int(i / (skip_every - 1)), i % (skip_every - 1)
j = v + u * skip_every + 1
plot_places.append(j)
fig = get_figure(fig)
fig.set_size_inches(fig.get_size_inches()[0] * grid[0] / 1.,
fig.get_size_inches()[1] * grid[1] / 2.0)
for i, (name, manifold) in enumerate(project_ops):
is3d = 'N:3' in name
try:
if is3d:
subplot = plt.subplot(grid[0], grid[1], plot_places[i], projection='3d')
else:
subplot = plt.subplot(grid[0], grid[1], plot_places[i])
data_source = encodings if not _needs_hessian(manifold) else \
(hessian_cos if 'cosine' in name else hessian_euc)
projections = manifold.fit_transform(data_source)
scatter(subplot, projections, is3d, _build_radial_colors(len(data_source)))
subplot.set_title(name)
except:
print(name, "Unexpected error: ", sys.exc_info()[0], sys.exc_info()[1] if len(sys.exc_info()) > 1 else '')
visualize_data_same(encodings, grid=grid, places=plot_places[-4:])
if not interactive:
save_fig(file_name, fig)
ut.print_time('visualization finished')