## PCA pca = PCA(n_components=nband) pca.fit(Y) Ypca = pca.transform(Y) Cpca = pca.transform(center) Ypca[:, 0] = -Ypca[:, 0] Cpca[:, 0] = -Cpca[:, 0] # Ypca[:,1]=-Ypca[:,1] # Cpca[:,1]=-Cpca[:,1] ## rename clusters ythe = np.array([0]) label, Cpca, center = SSRS.Cluster_rename(label, ythe, Cpca, center) ## plot PCA and cluster after resign name SSRS.Cluster_plot(Y, label, center) #plt.savefig(figdir+'Cluster') plt.figure() p1 = plt.scatter(Ypca[:, 0], Ypca[:, 1], c=label) p2 = plt.plot(Cpca[:, 0], Cpca[:, 1], 'rx', markersize=20, markeredgewidth=5) plt.colorbar() #plt.savefig(figdir+'PCA') ## compute distance dist = np.zeros([nind, nc]) distx = np.zeros([nind, nc]) disty = np.zeros([nind, nc]) for j in range(0, nind): for i in range(0, nc): dist[j, i] = SSRS.DistCal(Y[j, :], center[i, :])
string, nodeind, leaf, label = SSRS.traverseTree(regTree, feature_names, Xin) for i in range(0, regTree.node_count): plt.figure() plt.boxplot(Yin[nodeind[i], :]) plt.title(string[i], fontsize=8) #plt.tight_layout() plt.savefig(savedir + "Test_node%i" % i) plt.close() ## PCA of tree leaves ## PCA Xin = Xn2 Yin = Y string, nodeind, leaf, label = SSRS.traverseTree(regTree, feature_names, Xin) nclass = np.unique(leaf).shape[0] nband = 30 pca = PCA(n_components=30) pca.fit(Yin) Ypca = pca.transform(Yin) center = np.zeros([nclass, nband]) for i in range(0, nclass): center[i, :] = Yin[nodeind[i], :].mean(axis=0) Cpca = pca.transform(center) p1 = plt.scatter(Ypca[:, 0], Ypca[:, 1], c=label) p2 = plt.plot(Cpca[:, 0], Cpca[:, 1], 'rx', markersize=20, markeredgewidth=5) plt.colorbar() SSRS.Cluster_plot(Y, label.astype(int))