Esempio n. 1
0
    def compress(self, X, n_components, n_neighbours):
        n = X.shape[0]
        k = self.k
        numNeighbours = self.numNeighbours

        # find the distances to every other point
        euclD = utils.euclidean_dist_squared(X, X)
        euclD = np.sqrt(euclD)

        knnD = np.zeros((n, n))

        # get the KNN of point i
        for i in range(n):
            # finds numNeighbours smallest distances from obj_i
            # +1 because it will always select itself as (distance of 0), and distances are non-negative
            minIndexes = np.argsort(euclD[i])[:numNeighbours + 1]

            for index in minIndexes:
                # add distances of KNN_i to the distance matrix
                knnD[i, index] = euclD[i, index]

        D = np.zeros((n, n))
        # get distance of every other path using only KNN
        for i in range(n):
            for j in range(n):
                if i != j:
                    D[i, j] = utils.dijkstra(knnD, i, j)

        Z = AlternativePCA(k).fit(X).compress(X)
        z = find_min(self._fun_obj_z, Z.flatten(), 500, False, D)
        Z = z.reshape(n, k)
        return Z
Esempio n. 2
0
        ax.scatter(Z[:, 0], Z[:, 1])
        for i in range(n):
            ax.annotate(animals[i], (Z[i, 0], Z[i, 1]))

        utils.savefig('q2_2_PCA_animals.png')

    elif question == '3.1':
        X = load_dataset('highway.pkl')['X'].astype(float) / 255
        n, d = X.shape
        print(n, d)
        h, w = 64, 64  # height and width of each image

        k = 5  # number of PCs
        threshold = 0.1  # threshold for being considered "foreground"

        model = AlternativePCA(k=k)
        model.fit(X)
        Z = model.compress(X)
        Xhat_pca = model.expand(Z)

        model = RobustPCA(k=k)
        model.fit(X)
        Z = model.compress(X)
        Xhat_robust = model.expand(Z)

        fig, ax = plt.subplots(2, 3)
        for i in range(10):
            ax[0, 0].set_title('$X$')
            ax[0, 0].imshow(X[i].reshape(h, w).T, cmap='gray')

            ax[0, 1].set_title('$\hat{X}$ (L2)')
Esempio n. 3
0
    io_args = parser.parse_args()
    task = io_args.task

    if task == '1':
        X = load_dataset('highway.pkl')['X'].astype(float) / 255
        #print(X[0].reshape(64,64).T)
        n, d = X.shape
        print(n, d)
        h, w = 64, 64  # height and width of each image

        k = 5  # number of PCs
        threshold = 0.1  # threshold for being considered "foreground"
        #threshold = 0.07

        model = AlternativePCA(k=k)
        model.fit(X)
        Z = model.compress(X)
        Xhat_pca = model.expand(Z)

        model = RobustPCA(k=k)
        model.fit(X)
        Z = model.compress(X)
        Xhat_robust = model.expand(Z)

        fig, ax = plt.subplots(2, 3)
        for i in range(10):
            ax[0, 0].set_title('$X$')
            ax[0, 0].imshow(X[i].reshape(h, w).T, cmap='gray')

            ax[0, 1].set_title('$\hat{X}$ (L2)')