def test(): test_data, test_labels = load_data(test_data_path) train_data, train_labels = load_data(train_data_path) if args.alg == 'pca': saved_data = np.load( os.path.join(args.output, args.alg + str(args.dim) + '.npz')) mean_vecs = saved_data['mean_vecs'] eig_vecs = saved_data['eig_vecs'] reduction_test_data = pca_test(np.array(test_data), mean_vecs, eig_vecs) reduction_train_data = pca_test(np.array(train_data), mean_vecs, eig_vecs) elif args.alg == 'svd': saved_data = np.load( os.path.join(args.output, args.alg + str(args.dim) + '.npz')) v = saved_data['v'] mean_vecs = saved_data['mean_vecs'] reduction_test_data = svd_test(np.array(test_data), mean_vecs, v) reduction_train_data = svd_test(np.array(train_data), mean_vecs, v) else: reduction_test_data = isomap(np.array(test_data), n=args.dim) reduction_train_data = isomap(np.array(train_data), n=args.dim) acc = eval(reduction_test_data, test_labels, reduction_train_data, train_labels) print('ACC = ' + str(acc))
def callback(iters, current, env): print 'Callback: ', iters # get all traces env.updategui = False traces = env.evaluate_policy(current) env.updategui = True pickle.dump(traces, open('traces{0}.pck'.format(iters), 'w'), pickle.HIGHEST_PROTOCOL) # measure task performance avg_reward = 0 for t in traces: avg_reward += sum([i[2] for i in t]) avg_reward = avg_reward / float(len(traces)) print 'Avg reward: ', avg_reward # find current embedding ematrix = np.zeros((512, 512)) for (i, t) in enumerate(traces): for (j, s) in enumerate(traces): ematrix[i, j] = edit_distance_vc([e[1] for e in t], [l[1] for l in s], (1.0, 1.0, 1.2)) pickle.dump(ematrix, open('ematrix{0}.pck'.format(iters), 'w'), pickle.HIGHEST_PROTOCOL) y, s, adj = isomap(ematrix) if len(y) < 512: # fallback to mds if more than 1 connected component print "More than 1 CC - Falling back to MDS" y, s = mds(ematrix) adj = None # plot stuff later because of pylab / pygame incompat on mac # save embedding image - multiple formats? #scatter(y[:,0],y[:,1], filename='scatter{0}.pdf'.format(iters)) # save scree plot #plot(s[:10], filename='scree{0}.pdf'.format(iters)) # procrustes error gt = env.coords_array() err = procrustes(gt, y) print "Procrustes ", err pickle.dump((iters, err, gt, avg_reward, current, y, s, adj), open('misc{0}.pck'.format(iters), 'w'), pickle.HIGHEST_PROTOCOL) env.save('iter{0}.png'.format(iters))
pickle.dump((iters, err, gt, avg_reward, current, y, s, adj), open('misc{0}.pck'.format(iters), 'w'), pickle.HIGHEST_PROTOCOL) env.save('iter{0}.png'.format(iters)) if __name__ == '__main__': workspace = "{0}/wrk/lspi/bin".format(os.environ['HOME']) if False: # fix isomap issue on bad matches ematrix = pickle.load(open('ematrix0.pck')) y, s, adj = isomap(ematrix) if True: endstates = [272] # [16] ogw = RBFObserverGridworldGui( "/Users/stober/wrk/lspi/bin/16/20comp.npy", "/Users/stober/wrk/lspi/bin/16/states.npy", endstates=endstates, walls=None, nrbf=80) # ogw = RBFObserverGridworld("/Users/stober/wrk/lspi/bin/16/20comp.npy", "/Users/stober/wrk/lspi/bin/16/states.npy", endstates = endstates, walls=None, nrbf=80) t = pickle.load(open(workspace + "/traces/complete_trace.pck"))
# -*- coding: utf-8 -*- from sklearn.manifold import Isomap import isomap as isa import sklearn.datasets as db def isomap(data, d, k): ''' input:data(ndarray):待降维数据 d(int):降维后数据维度 k(int):最近的k个样本 output:Z(ndarray):降维后数据 ''' # ********* Begin *********# iso = Isomap(n_neighbors=k, n_components=d) return iso.fit_transform(data) if __name__ == '__main__': ir = db.load_boston() X1 = isa.isomap(ir.data[:10], d=2, k=4) X2 = isomap(ir.data[:10], d=2, k=4) print(X1) print(X2)
#import lle #print shape(iris) #a,b,newData = lle.lle(iris,2,12) #print shape(newData) #print newData[w0,:] #print "---" #print newData[w1,:] #print "---" #print newData[w2,:] # #plot(newData[w0,0],newData[w0,1],'ok') #plot(newData[w1,0],newData[w1,1],'^k') #plot(newData[w2,0],newData[w2,1],'vk') #axis('off') import isomap print labels newData,newLabels = isomap.isomap(iris,2,100) print shape(newData) print newLabels w0 = where(newLabels==0) w1 = where(newLabels==1) w2 = where(newLabels==2) plot(newData[w0,0],newData[w0,1],'ok') plot(newData[w1,0],newData[w1,1],'^k') plot(newData[w2,0],newData[w2,1],'vk') axis('off') show()
print np.shape(iris) a,b,newData = lle.lle(iris,2,12) print np.shape(newData) print newData[w0,:] print "---" print newData[w1,:] print "---" print newData[w2,:] pl.plot(newData[w0,0],newData[w0,1],'ok') pl.plot(newData[w1,0],newData[w1,1],'^k') pl.plot(newData[w2,0],newData[w2,1],'vk') pl.axis('off') import isomap print labels newData,newLabels = isomap.isomap(iris,2,100) print np.shape(newData) print newLabels w0 = np.where(newLabels==0) w1 = np.where(newLabels==1) w2 = np.where(newLabels==2) pl.plot(newData[w0,0],newData[w0,1],'ok') pl.plot(newData[w1,0],newData[w1,1],'^k') pl.plot(newData[w2,0],newData[w2,1],'vk') pl.axis('off') print "Done" pl.show()
iy = 10 * j + 1 img[ix:ix + 8, iy:iy + 8] = X[i * n_img_per_row + j].reshape((8, 8)) plt.imshow(img, cmap=plt.cm.binary) plt.xticks([]) plt.yticks([]) plt.title('A selection from the 64-dimensional digits dataset') # Random 2D projection using a random unitary matrix print("Computing random projection") rp = random_projection.SparseRandomProjection(n_components=2, random_state=42) X_projected = rp.fit_transform(X) plot_embedding(X_projected, "Random Projection of the digits") # Isomap projection of the digits dataset print("Computing Isomap embedding") t0 = time() embedding = isomap(input=X, n_neighbors=n_neighbors, n_components=2, n_jobs=4) print("Done.") plot_embedding(embedding,"Isomap projection of the digits (time %.2fs)" %(time() - t0)) embedding_three_dim = isomap(input=X, n_neighbors=n_neighbors, n_components=3, n_jobs=4) # draw 32 plots fig = plt.figure() ax = Axes3D(fig) ax.scatter(embedding_three_dim[:, 0], embedding_three_dim[:, 1], embedding_three_dim[:, 2], c=y)