def main(): base_dir = '/home/caio/workspace/laps/' proj_tsne = np.load(base_dir + 'laps_X_proj_tsne.npy') min0 = proj_tsne[:, 0].min() min1 = proj_tsne[:, 1].min() max0 = proj_tsne[:, 0].max() max1 = proj_tsne[:, 1].max() proj_tsne[:, 0] = (proj_tsne[:, 0] - min0) / (max0 - min0) proj_tsne[:, 1] = (proj_tsne[:, 1] - min1) / (max1 - min1) X_train = np.load(base_dir + 'laps_X_train.npy') # y_train = np.load('data/fm/y_proj.npy') clf1 = CLF() clf1.LoadSKLearn(base_dir + 'lr_clf.joblib', "LR") # Plots the projected points coulored according to the label assigned by # the classifier. # As it is the first projection plotted, the legend is also save into a # separate file y_pred = clf1.Predict(X_train) labels = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] path = base_dir + "projection_clf1_tsne.pdf" path_leg = base_dir + "projection_leg.pdf" title = "t-SNE Projection" PlotProjection(proj_tsne, y_pred, path, title, path_leg, labels) # Run boundary map construction function R = 200 N = [5] grid1 = Grid(proj_tsne, R) _, dmap = grid1.BoundaryMap(X_train, N[0], clf1) np.save(base_dir + 'dmap_tsne_lr_R_50_N_10.npy', dmap)
def main(): # Will try to plot errors caused by classfier # using segmentation dataset with open("data/segmentation/seg.json") as f: data_json = json.load(f) proj = np.load(data_json["proj"]) X_train = np.load(data_json['X_train']) y_train = np.load(data_json['y_train']) X_test = np.load(data_json['X_test']) y_test = np.load(data_json['y_test']) # Logistic regression had the lowest accuracy, so it will be used in this # test clf_logreg = CLF() clf_logreg.LoadSKLearn(data_json['clfs'][0], "Logistic Regression") y_pred = clf_logreg.Predict(X_train) path = "data/segmentation/projection_logreg_err.pdf" title = "LAMP Projection Error (Logistic Regression)" R = 50 N = 1 grid_logreg = Grid(proj, R) PlotProjectionErr(grid_logreg, proj, y_pred, y_train, path, title) print("Create grid logreg") _, dmap = grid_logreg.BoundaryMap(X_train, N, clf_logreg) fig_title = "{}x{} DenseMap Err ({} samples, {})".format( grid_logreg.grid_size, grid_logreg.grid_size, N, clf_logreg.name) fig_name = "data/segmentation/DenseMap_Err_{}x{}_N_{}_dense_map_{}".format( grid_logreg.grid_size, grid_logreg.grid_size, N, clf_logreg.name) miss_classified = proj[y_train != y_pred] PlotDenseMapErr(grid_logreg, dmap, proj, y_train, y_pred, fig_title, fig_name) errmap = CalcErrMap(grid_logreg, dmap, X_train, y_train, clf_logreg) tmp_err = np.flip(errmap, axis=0) plt.xticks([]) plt.yticks([]) #tmp_dense = np.flip(dmap, axis=0) #tmp_dense = TransferFunc(tmp_dense, 0.7) #rgb_img = hsv_to_rgb(tmp_dense) #plt.imshow(rgb_img, interpolation='none') plt.imshow(tmp_err, interpolation='none') plt.show()
def main(): # Ideal path: # 1 - Load dataset, projection and a trained classifier with open("data/toy/toy.json") as f: data_json = json.load(f) proj = np.load(data_json["proj"]) X_train = np.load(data_json['X_train']) y_train = np.load(data_json['y_train']) X_test = np.load(data_json['X_test']) y_test = np.load(data_json['y_test']) clf = CLF() clf.LoadSKLearn(data_json['clfs'][0], "Logistic Regression") # Plots the projected points coulored according to the label assigned by # the classifier. # As it is the first projection plotted, the legend is also save into a # separate file y_pred = clf.Predict(X_train) labels = ["0", "1"] path = "data/toy/projection_clf1_tsne.pdf" path_leg = "data/toy/projection_leg.pdf" title = "LAMP Projection" PlotProjection(proj, y_pred, path, title, path_leg, labels) #PlotMatrix(proj, X_train, clf) # 2 - Run boundary map construction function grid = Grid(proj, 100) num_samples = 5 _, dmap = grid.BoundaryMap(X_train, num_samples, clf) fig_title = "{}x{} DenseMap ({} samples, {})".format( grid.grid_size, grid.grid_size, num_samples, clf.name) fig_name = "data/toy/DenseMap_{}x{}_N_{}_dense_map_{}".format( grid.grid_size, grid.grid_size, num_samples, clf.name) PlotDenseMap(dmap, fig_title, fig_name)
def main(): # Ideal path: # 1 - Load dataset, projection and a trained classifier with open("data/segmentation/seg.json") as f: data_json = json.load(f) proj = np.load(data_json["proj"]) X_train = np.load(data_json['X_train']) y_train = np.load(data_json['y_train']) X_test = np.load(data_json['X_test']) y_test = np.load(data_json['y_test']) clf_logreg = CLF() clf_logreg.LoadSKLearn(data_json['clfs'][0], "Logistic Regression") # Plots the projected points coulored according to the label assigned by # the classifier. # As it is the first projection plotted, the legend is also save into a # separate file y_pred = clf_logreg.Predict(X_train) labels = ["0", "1", "2", "3", "4", "5", "6"] path = "data/segmentation/projection_logreg.pdf" path_leg = "data/segmentation/projection_leg.pdf" title = "LAMP Projection (Logistic Regression)" PlotProjection(proj, y_pred, path, title, path_leg, labels) clf_svm = CLF() clf_svm.LoadSKLearn(data_json['clfs'][1], "SVM") y_pred = clf_svm.Predict(X_train) path = "data/segmentation/projection_svm.pdf" title = "LAMP Projection (SVM)" PlotProjection(proj, y_pred, path, title) clf_knn5 = CLF() clf_knn5.LoadSKLearn(data_json['clfs'][2], "KNN (5)") y_pred = clf_knn5.Predict(X_train) path = "data/segmentation/projection_knn5.pdf" title = "LAMP Projection (KNN)" PlotProjection(proj, y_pred, path, title) # 2 - Run boundary map construction function on clf_logreg R = 500 N = 5 grid_logreg = Grid(proj, R) print("Create grid logreg") _, dmap = grid_logreg.BoundaryMap(X_train, N, clf_logreg) fig_title = "{}x{} DenseMap ({} samples, {})".format( grid_logreg.grid_size, grid_logreg.grid_size, N, clf_logreg.name) fig_name = "data/segmentation/DenseMap_{}x{}_N_{}_dense_map_{}".format( grid_logreg.grid_size, grid_logreg.grid_size, N, clf_logreg.name) PlotDenseMap(dmap, fig_title, fig_name) # Run boundary map construction function on clf_svm grid_svm = Grid(proj, R) print("Create grid svm") _, dmap = grid_svm.BoundaryMap(X_train, N, clf_svm) fig_title = "{}x{} DenseMap ({} samples, {})".format( grid_svm.grid_size, grid_svm.grid_size, N, clf_svm.name) fig_name = "data/segmentation/DenseMap_{}x{}_N_{}_dense_map_{}".format( grid_svm.grid_size, grid_svm.grid_size, N, clf_svm.name) PlotDenseMap(dmap, fig_title, fig_name) # Run boundary map construction function on clf_knn5 grid_knn5 = Grid(proj, R) print("Create grid knn") _, dmap = grid_knn5.BoundaryMap(X_train, N, clf_knn5) fig_title = "{}x{} DenseMap ({} samples, {})".format( grid_knn5.grid_size, grid_knn5.grid_size, N, clf_knn5.name) fig_name = "data/segmentation/DenseMap_{}x{}_N_{}_dense_map_{}".format( grid_knn5.grid_size, grid_knn5.grid_size, N, clf_knn5.name) PlotDenseMap(dmap, fig_title, fig_name)