# components = dl.components().reshape((n_components, n_features)) components = dl.dictionary # Visualizing the components as images plot_gallery("Visualizing top components", components.T, w, h, n_row=n_components / 10, n_col=10) plt.show() ############################################################################### # Sparse Encoding print("\nSparse Encoding") train_X_sc = dl.code.T np.set_printoptions(precision=1, suppress=False, linewidth=800) test_X_sc = np.zeros((len(test_X), n_components)) test_X_sc = dl.sparse_encode(test_X, components).T print "train_X_sc.shape", train_X_sc.shape ############################################################################### # Visualize reconstructed images reconstructed_X = np.zeros((20, n_features)) reconstructed_X_idx = range(10) reconstructed_X[reconstructed_X_idx] = test_X[reconstructed_X_idx] reconstructed_X[reconstructed_X_idx] = np.dot(components, test_X_sc[:, reconstructed_X_idx]) print "reconstructed_X.shape", reconstructed_X.shape print(reconstructed_X) plot_gallery("Reconstructed images", reconstructed_X, w, h, n_row=2, n_col=10) plt.show()
print("Extracting the top %d eigenfaces from %d faces" % (n_components, X_train.shape[0])) t0 = time() # pca = RandomizedPCA(n_components=n_components, whiten=True).fit(X_train) dl = KSVDSparseCoding(n_components, preserve_dc=True, approx=False, max_iter=15, verbose=1) dl.fit(X_train) print("done in %0.3fs" % (time() - t0)) eigenfaces = dl.dictionary print("Projecting the input data on the eigenfaces orthonormal basis") t0 = time() X_train_pca = dl.code.T X_test_pca = dl.sparse_encode(X_test, eigenfaces).T print("done in %0.3fs" % (time() - t0)) ############################################################################### # Train a SVM classification model print("Fitting the classifier to the training set") t0 = time() param_grid = {'C': [1e3, 5e3, 1e4, 5e4, 1e5], 'gamma': [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.1],} clf = GridSearchCV(SVC(kernel='rbf', class_weight='balanced'), param_grid) clf = clf.fit(X_train_pca, y_train) print("done in %0.3fs" % (time() - t0)) print("Best estimator found by grid search:") print(clf.best_estimator_)