lda次元ダウンおよび後続ラベル図示処理

2016 ワード

from sklearn import decomposition
from sklearn.preprocessing import StandardScaler
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis 
import matplotlib.pyplot as plt
import numpy as np
# import seaborn  #   
from mpl_toolkits.mplot3d import Axes3D
# %matplotlib notebook

data = np.loadtxt('batch10_random_train.txt')
data_x = data[:,6:]
data_y = data[:,:6]
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)
new_X = lda.fit_transform(data_x, data_y)
print(new_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(new_X)):
	if data_y[i] == 0:
		x_1.append(new_X[i][0])
		y_1.append(new_X[i][1])
		z_1.append(new_X[i][2])
	elif data_y[i] ==1:
		x_2.append(new_X[i][0])
		y_2.append(new_X[i][1])
		z_2.append(new_X[i][2])
	elif data_y[i] ==2:
		x_3.append(new_X[i][0])
		y_3.append(new_X[i][1])
		z_3.append(new_X[i][2])
	elif data_y[i] ==3:
		x_4.append(new_X[i][0])
		y_4.append(new_X[i][1])
		z_4.append(new_X[i][2])
	elif data_y[i] ==4:
		x_5.append(new_X[i][0])
		y_5.append(new_X[i][1])
		z_5.append(new_X[i][2])
	else:
		x_6.append(new_X[i][0])
		y_6.append(new_X[i][1])
		z_6.append(new_X[i][2])

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.scatter(x_1, y_1, z_1, c='r', label = 1)
ax.scatter(x_2, y_2, z_2, c='y', label = 2)
ax.scatter(x_3, y_3, z_3, c='g', label = 3)
ax.scatter(x_4, y_4, z_4, c='b', label = 4)
ax.scatter(x_5, y_5, z_5, c='k', label = 5)
ax.scatter(x_6, y_6, z_6, c='m', label = 6)
#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='best')
plt.show()
print(lda.explained_variance_ratio_)