from sklearn import decomposition
from sklearn.preprocessing import StandardScaler
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.manifold import LocallyLinearEmbedding
import matplotlib.pyplot as plt
import numpy as np
# import seaborn #可视化
from mpl_toolkits.mplot3d import Axes3D
# %matplotlib notebook
data = np.loadtxt('feature_result.txt')
data_x = data[:,0:15]
data_y = data[:,15]
# data_y = [np.argmax(one_hot) for one_hot in data_y] #转成一类
std = StandardScaler()
data_x_std = std.fit_transform(data_x) #均值化
# lda = LinearDiscriminantAnalysis(n_components=3)
lle = LocallyLinearEmbedding(n_neighbors = 10, n_components = 3)
# pca = decomposition.PCA(n_components=3)
PCA_X = lle.fit_transform(data_x, data_y)
# np.savetxt('pca_x.txt', PCA_X)
# print(PCA_X)
x_1, y_1, z_1 = [],[],[]
x_2, y_2, z_2 = [],[],[]
x_3, y_3, z_3 = [],[],[]
x_4, y_4, z_4 = [],[],[]
# x_5, y_5, z_5 = [],[],[]
# x_6, y_6, z_6 = [],[],[]
for i in range(len(PCA_X)):
if data_y[i] == 0:
x_1.append(PCA_X[i][0])
y_1.append(PCA_X[i][1])
z_1.append(PCA_X[i][2])
elif data_y[i] ==1:
x_2.append(PCA_X[i][0])
y_2.append(PCA_X[i][1])
z_2.append(PCA_X[i][2])
elif data_y[i] ==2:
x_3.append(PCA_X[i][0])
y_3.append(PCA_X[i][1])
z_3.append(PCA_X[i][2])
elif data_y[i] ==3:
x_4.append(PCA_X[i][0])
y_4.append(PCA_X[i][1])
z_4.append(PCA_X[i][2])
# elif data_y[i] ==5:
# x_5.append(PCA_X[i][0])
# y_5.append(PCA_X[i][1])
# z_5.append(PCA_X[i][2])
# else:
# x_6.append(PCA_X[i][0])
# y_6.append(PCA_X[i][1])
# z_6.append(PCA_X[i][2])
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.scatter(x_1, y_1, z_1, c='orange', label = '0', marker = '*')
ax.scatter(x_2, y_2, z_2, c='firebrick', label = '1', marker = 'p')
ax.scatter(x_3, y_3, z_3, c='slateblue', label = '2', marker = 's')
ax.scatter(x_4, y_4, z_4, c='lightseagreen', label = '3', marker = 'o')
# ax.scatter(x_5, y_5, z_5, c='slateblue', label = '$C_3H_6O$', marker = '^')
# ax.scatter(x_6, y_6, z_6, c='firebrick', label = '$NH_3b$', marker = 'd')
# #alt+f3全选所有变量
ax.set_xlabel('pc1', fontsize = 10)
ax.set_ylabel('pc2', fontsize = 10)
ax.set_zlabel('pc3', fontsize = 10)
plt.savefig('lda.jpg')
ax.legend(loc='upper left')
plt.show()
# print(lle.explained_variance_ratio_)
lda
最新推荐文章于 2024-07-18 19:41:34 发布