def main():
    sd = SynthData()
    ca = coordinateAscent()
    cal = CoordinateAscentLasso()
    st = Standardize()
    beta0Seperate = True
    lam = 0.1

    X, y, b = sd.generateData(noise=False,
                              w=np.array([1, 1, 1, 1, 1, 0, 0, 0, 0,
                                          0])[np.newaxis].T)
    #if beta0Seperate:
    #    beta = np.array([1, 1, 1, 1, 0, 0, 0, 0])[np.newaxis].T
    #else:
    #    beta = np.array([1, 1, 1, 1, 1, 0, 0, 0, 0])[np.newaxis].T

    #if beta0Seperate:
    #    y = 1 + np.dot(X, beta)
    #else:
    #    X = np.append(np.ones((X.shape[0], 1)), X, 1)
    #    y = np.dot(X, beta)

    print('Fitting the model with Lasso:')
    print('Lambda = ' + str(lam))
    print('beta0, array of betas:')
    t0 = time()
    print(cal.coordinateAscentLasso(y, X, lam, [], False, beta0Seperate))
    dt = time() - t0
    print('done in %.4fs.' % dt)

    print()
    print('Fitting the model with plain \'ol Coordinate Ascent')
    print('beta0, array of betas:')
    t0 = time()
    print(ca.coordinateAscent(y, X, [], False))
    dt = time() - t0
    print('done in %.4fs.' % dt)
    print()

    #print('Dictionary Learning')
    #dl = decomposition.DictionaryLearning(fit_algorithm='cd')
    #print(dl.fit(X))
    #print(np.shape(dl.components_))
    #print(dl.components_)

    print('Fitting the model with LARS (from the scikit library)')
    clf = linear_model.LassoLars(alpha=0.01)
    t0 = time()
    print(clf.fit(X, y))
    dt = time() - t0
    print('done in %.4fs.' % dt)
    print('array of betas:')
    print(clf.coef_)
    return 1
def main():
    sd = SynthData()
    ca = coordinateAscent()
    cal = CoordinateAscentLasso()
    st = Standardize()
    beta0Seperate = True
    lam = 0.1

    X, y, b = sd.generateData(noise=False,  w=np.array([1, 1, 1, 1, 1, 0, 0, 0, 0, 0])[np.newaxis].T)
    #if beta0Seperate:
    #    beta = np.array([1, 1, 1, 1, 0, 0, 0, 0])[np.newaxis].T
    #else:
    #    beta = np.array([1, 1, 1, 1, 1, 0, 0, 0, 0])[np.newaxis].T

    #if beta0Seperate:
    #    y = 1 + np.dot(X, beta)
    #else:
    #    X = np.append(np.ones((X.shape[0], 1)), X, 1)
    #    y = np.dot(X, beta)

    print('Fitting the model with Lasso:')
    print('Lambda = ' + str(lam))
    print('beta0, array of betas:')
    t0 = time()
    print(cal.coordinateAscentLasso(y, X, lam, [], False, beta0Seperate))
    dt = time() - t0
    print('done in %.4fs.' % dt)

    print()
    print('Fitting the model with plain \'ol Coordinate Ascent')
    print('beta0, array of betas:')
    t0 = time()
    print(ca.coordinateAscent(y, X, [], False))
    dt = time() - t0
    print('done in %.4fs.' % dt)
    print()

    #print('Dictionary Learning')
    #dl = decomposition.DictionaryLearning(fit_algorithm='cd')
    #print(dl.fit(X))
    #print(np.shape(dl.components_))
    #print(dl.components_)

    print('Fitting the model with LARS (from the scikit library)')
    clf = linear_model.LassoLars(alpha=0.01)
    t0 = time()
    print(clf.fit(X, y))
    dt = time() - t0
    print('done in %.4fs.' % dt)
    print('array of betas:')
    print(clf.coef_)
    return 1
    def dictLearning(self):
        # p > n?
        n = 5
        p = 10
        r = 5

        # Dictionary - Line generates a random normal distributed Matrix with dimensions of R^n*p
        # will be x in lasso
        X = np.random.random((n, p))
        self.X = X
        # same as D = np.random.randn(s, r)

        # sparse code R^p*1 with # of zeros < p (or n?), rest are ones
        w = np.zeros((p, r))
        for i in range(r):
            w[: np.random.randint(1, n), i] = 1
            np.random.shuffle(w[:, i])
        self.w = w
        print(w)
        # print(np.dot(w, w.T))

        # Data matrix - R^n*1
        # will be y in Lasso
        self.y = np.dot(X, w) + 0.1 * np.random.randn(n, r)
        y = self.y
        print(y)

        print("X = ")
        print(np.shape(X))
        print("m*k")
        print("w = ")
        print(np.shape(w))
        print("k*n")
        print("y = ")
        print(np.shape(y))
        print("m*n")

        # assume default tolerance and number of iterations
        TOL = 1e-6
        MAXIT = 100

        A = 0
        B = 0

        # Coordinate Ascent Lasso
        cal = CoordinateAscentLasso()

        for i in range(MAXIT):
            # each column of weights 'corresponds' to to columns of y.
            for k in range(r):
                # updating the weights column-wise
                w[:, k] = cal.coordinateAscentLasso(y[:, k][np.newaxis].T, X, 0.1)[1].flatten()
            # print('w updated using Lasso: ')
            # print(w)

            # A = A + np.dot(w, w.T)
            # B = B + np.dot(y, w.T)
            # print('A')
            # print(A)
            # exit()

            # X = self.updateDict(X, A, B)
            X = self.updateDict5(X, y, w)
            # X = self.updateDict3(X, y, w)
            # print()
            # print(X)
            # print(np.shape(X))
            # return 1
            # select complete k-th column, select complete k-th row
            # X[:, k], w[k, :]
            # print(y - np.dot(X, w))
            # print(np.shape(y - np.dot(X, w)))
            # print(np.shape(w))
            # print(np.dot(y - np.dot(X, w), w.T))
            # X = np.divide(np.dot(y - np.dot(X, w), w.T).T, w).T
            # print(np.shape(X))

        return w, X
Пример #4
0
    def dictLearning(self):
        #p > n?
        n = 5
        p = 10
        r = 5

        #Dictionary - Line generates a random normal distributed Matrix with dimensions of R^n*p
        #will be x in lasso
        X = np.random.random((n, p))
        self.X = X
        #same as D = np.random.randn(s, r)

        #sparse code R^p*1 with # of zeros < p (or n?), rest are ones
        w = np.zeros((p, r))
        for i in range(r):
            w[:np.random.randint(1, n), i] = 1
            np.random.shuffle(w[:, i])
        self.w = w
        print(w)
        #print(np.dot(w, w.T))

        #Data matrix - R^n*1
        #will be y in Lasso
        self.y = np.dot(X, w) + 0.1 * np.random.randn(n, r)
        y = self.y
        print(y)

        print('X = ')
        print(np.shape(X))
        print('m*k')
        print('w = ')
        print(np.shape(w))
        print('k*n')
        print('y = ')
        print(np.shape(y))
        print('m*n')

        #assume default tolerance and number of iterations
        TOL = 1e-6
        MAXIT = 100

        A = 0
        B = 0

        #Coordinate Ascent Lasso
        cal = CoordinateAscentLasso()

        for i in range(MAXIT):
            #each column of weights 'corresponds' to to columns of y.
            for k in range(r):
                #updating the weights column-wise
                w[:, k] = cal.coordinateAscentLasso(y[:, k][np.newaxis].T, X,
                                                    0.1)[1].flatten()
            #print('w updated using Lasso: ')
            #print(w)

            #A = A + np.dot(w, w.T)
            #B = B + np.dot(y, w.T)
            #print('A')
            #print(A)
            #exit()

            #X = self.updateDict(X, A, B)
            X = self.updateDict5(X, y, w)
            #X = self.updateDict3(X, y, w)
            #print()
            #print(X)
            #print(np.shape(X))
            #return 1
            #select complete k-th column, select complete k-th row
            #X[:, k], w[k, :]
            #print(y - np.dot(X, w))
            #print(np.shape(y - np.dot(X, w)))
            #print(np.shape(w))
            #print(np.dot(y - np.dot(X, w), w.T))
            #X = np.divide(np.dot(y - np.dot(X, w), w.T).T, w).T
            #print(np.shape(X))

        return w, X