# coding:utf-8
import numpy as np
from sklearn.datasets import make_s_curve
import matplotlib.pyplot as plt
from sklearn.manifold import LocallyLinearEmbedding
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import operator
import matplotlib.pyplot as plt
from sklearn import datasets, decomposition, manifold
from itertools import cycle
from mpl_toolkits.mplot3d import Axes3D
from sklearn.datasets import make_swiss_roll
# def make_swiss_roll(n_samples=100, noise=0.0, random_state=None):
# # Generate a swiss roll dataset.
# t = 1.5 * np.pi * (1 + 2 * np.random.rand(1, n_samples))
# x = t * np.cos(t)
# y = 83 * np.random.rand(1, n_samples)
# z = t * np.sin(t)
# X = np.concatenate((x, y, z))
# X += noise * np.random.randn(3, n_samples)
# X = X.T
# t = np.squeeze(t)
# return X, t
def load_data():
swiss_roll = datasets.make_swiss_roll(n_samples=1000)
return swiss_roll[0], np.floor(swiss_roll[1])
def cal_pairwise_dist(x):
'''计算pairwise 距离, x是matrix
(a-b)^2 = a^2 + b^2 - 2*a*b
'''
sum_x = np.sum(np.square(x), 1)
dist = np.add(np.add(-2 * np.dot(x, x.T), sum_x).T, sum_x)
# 返回任意两个点之间距离的平方
return dist
def get_n_neighbors(data, n_neighbors=10):
'''
:param data: (n_samples, n_features)
:param n_neighbors: n nearest neighbors
:return: neighbors indexs
'''
dist = cal_pairwise_dist(data)
dist[dist < 0] = 0
dist = dist ** 0.5
n = dist.shape[0]#读取dist的第一维度长度
N = np.zeros((n, n_neighbors))
for i in range(n):
index_ = np.argsort(dist[i])[1:n_neighbors + 1]
N[i] = N[i] + index_
return N.astype(np.int32)
def lle(data, n_dims=2, n_neighbors=10):
N = get_n_neighbors(data, n_neighbors)
n, D = data.shape
# prevent Si to small
if n_neighbors > D:
tol = 1e-3
else:
tol = 0
# calculate W
W = np.zeros((n_neighbors, n))#返回来一个给定形状和类型的用0填充的数组;
I = np.ones((n_neighbors, 1))#返回来一个给定形状和类型的用1填充的数组;
for i in range(n):
Xi = np.tile(data[i], (n_neighbors, 1)).T#在列方向重复data1次,在行方向重复n_neighbors次
Ni = data[N[i]].T
Si = np.dot((Xi - Ni).T, (Xi - Ni))
# magic and why????
Si = Si + np.eye(n_neighbors) * tol * np.trace(Si)
Si_inv = np.linalg.pinv(Si)
wi = (np.dot(Si_inv, I)) / (np.dot(np.dot(I.T, Si_inv), I)[0, 0])
W[:, i] = wi[:, 0]
# print("Xi.shape", Xi.shape)
# print("Ni.shape", Ni.shape)
# print("Si.shape", Si.shape)
W_y = np.zeros((n, n))
for i in range(n):
index = N[i]
for j in range(n_neighbors):
W_y[index[j], i] = W[j, i]
I_y = np.eye(n)
M = np.dot((I_y - W_y), (I_y - W_y).T)
eig_val, eig_vector = np.linalg.eig(M)
index_ = np.argsort(np.abs(eig_val))[1:n_dims + 1]
print("index_", index_)
Y = eig_vector[:, index_]
return Y
if __name__ == '__main__':
# X, Y = make_s_curve(n_samples = 500,
# noise = 0.1,
# random_state = 42)
#X, Y = make_swiss_roll(n_samples=500, noise=0.1, random_state=42)
# X, Y =load_data()
# fig = plt.figure('data')
# plt.subplot(131,projection='3d')
# plt.title("3D swiss roll")
# ax = Axes3D(fig) # 绘制3D图
# plt.scatter(X[:, 0], X[:, 1], X[:, 2], marker='o', c=Y)
# plt.show()
X, Y = make_swiss_roll(n_samples=2000, noise=0.2, random_state=42) # X为坐标 t为颜色
print("X", X)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=Y)
ax.view_init(10, 60)
ax.set_xlabel("$x$", fontsize=18)
ax.set_ylabel("$y$", fontsize=18)
ax.set_zlabel("$z$", fontsize=18)
plt.title('3D swiss roll')
data_1 = lle(X, n_neighbors=40)
print("data_1.shape", data_1.shape)
data_2 = LocallyLinearEmbedding(n_components=2, n_neighbors=40).fit_transform(X)
print("data_2.shape", data_2.shape)
print("data_2", data_2)
fig =plt.figure(figsize=(8, 4))
plt.subplot(121)
plt.title("my_LLE")
plt.scatter(data_1[:, 0], data_1[:, 1], c=Y)
plt.subplot(122)
plt.title("sklearn_LLE")
plt.scatter(data_2[:, 0], data_2[:, 1], c=Y)
plt.show()
没有合适的资源?快使用搜索试试~ 我知道了~
python_几种LLE_llepython_LLE算法Python_降维算法
共6个文件
py:6个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 2 下载量 146 浏览量
2021-09-11
06:27:06
上传
评论 3
收藏 8KB ZIP 举报
温馨提示
几种LLE(局部线性嵌入)算法的python实例,有数据集。
资源推荐
资源详情
资源评论
收起资源包目录
python_几种LLE_llepython_LLE算法Python_降维算法_源码.zip (6个子文件)
test3.py 2KB
test1.py 3KB
test6.py 4KB
test5.py 4KB
test4.py 2KB
test2.py 1KB
共 6 条
- 1
资源评论
- weixin_423695082022-04-12用户下载后在一定时间内未进行评价,系统默认好评。
- 2301_773425432023-06-12资源内容详尽,对我有使用价值,谢谢资源主的分享。
心梓
- 粉丝: 791
- 资源: 8060
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功