type, X = dataset_class(**DATASETS.get(dataset, {})) for algo_name in algorithms: assert(algo_name in ALGORITHMS, "Unknown algorithm: %s" % algo_name) title = algo_name.title() + "-" + dataset.title() algo_class = getattr(algos, algo_name) algo = algo_class(n_components=2, **ALGORITHMS.get(algo_name, {})) try: logging.info("Running " + " ".join(title.split("-"))) if type == ARTIFICIAL: X_data = X[0] X_data.dtype='float64' X_reduced = algo.run(X_data) plot_artificial_dataset(X_data, X_reduced, color=X[1], title=title, save=args.save, saveDir=output_dir) else: X_data = X["data"] X_reduced = algo.run(X_data) # For 2 dimensional plot_embedding(X_reduced, X["target"], X["images"], X['target'], title, save=args.save, saveDir=output_dir) if args.threeD: # For 3 dimensional algo = algo_class(n_components=3, n_neighbors=30) X_reduced = algo.run(X_data) title = title + "-" + "3D" plot_embedding_3D(X_reduced, X["target"], title, save=args.save, saveDir=output_dir) except Exception, e:
for i in range(numNodes): for j in range(numNodes): if k_neighbors_array[j, i] <= k_neighbors_array[i, j]: k_neighbors_array[i, j] = k_neighbors_array[j, i] else: k_neighbors_array[j, i] = k_neighbors_array[i, j] # Compute the all pair shortest path distance. dist_matrix = floyd_warshall(k_neighbors_array, directed=False) dist_matrix[np.isinf(dist_matrix)] = 0 # Do MDS or learn embedding # MDS can also be seen as a case of Kernel PCA # using data dependent kernel # So using K = 1/2 D^2, # we generate projections along principal components kernel = dist_matrix ** 2 kernel *= -0.5 kernelPCA = KernelPCA(n_components=self.n_components, kernel='precomputed') return kernelPCA.fit_transform(kernel) if __name__ == "__main__": isomap = Isomap(10, 3) X, color = datasets.make_swiss_roll(n_samples=3000) X_r = isomap.run(X) plot_artificial_dataset(X, X_r, color, "Swiss Roll")