Beispiel #1
0
 def fit(self, Y, iterations=100, verbose=False):
     """
     Y: numpy data [n_features, n_samples]
     k: interger: number of atoms in the dictionary
         if k is None, select k = round(0.2*n_samples)
     """
     self.Y = Y
     del Y
     Y_range = np.array([0, self.Y.shape[1]])
     D_range = np.array([0, self.k])
     self.D = utils.pickDfromY(self.Y, Y_range, D_range)
     self.X = np.zeros((self.D.shape[1], self.Y.shape[1]))
     for it in range(iterations):
         # update X
         lasso = optimize.Lasso(self.D, self.lambd)
         lasso.fit(self.Y, Xinit=self.X)
         self.X = lasso.coef_
         # update D
         F = np.dot(self.X, self.X.T)
         E = np.dot(self.Y, self.X.T)
         self.D = optimize.ODL_updateD(self.D,
                                       E,
                                       F,
                                       iterations=self.updateD_iters)
         if verbose:
             print('iter \t%d/%d \t\t loss \t%.4f' %
                   (it, iterations, self.loss()))
Beispiel #2
0
 def predict(self, Y):
     E = np.zeros((self.nclass, Y.shape[1]))
     for c in range(self.nclass):
         Dc = self._getDc(c)
         lasso = optimize.Lasso(Dc, self.lambd)
         lasso.fit(Y)
         Xc = lasso.solve()
         R1 = Y - np.dot(Dc, Xc)
         E[c, :] = 0.5 * (R1 *
                          R1).sum(axis=0) + self.lambd * abs(Xc).sum(axis=0)
     return np.argmin(E, axis=0) + 1
Beispiel #3
0
 def predict(self, Y):
     N = Y.shape[1]
     lambda_list = [self.lambd]
     for lambd in lambda_list:
         E = np.zeros((self.nclass, N))
         for c in range(self.nclass):
             # Dc in D only
             Dc_ = get_block_col(self.D, c, self.D_range)
             # Dc in D and D0
             Dc = np.hstack((Dc_, self.D0)) if self.k0 > 0 else Dc_
             lasso = optimize.Lasso(Dc, lambd=lambd)
             lasso.fit(Y)
             Xc = lasso.solve()
             R = Y - np.dot(Dc, Xc)
             E[c, :] = 0.5*np.sum(R*R, axis = 0) + \
                     lambd*np.sum(np.abs(Xc), axis = 0)
         pred = np.argmin(E, axis=0) + 1
     return pred
     pass
Beispiel #4
0
 def _updateXc(self, c):
     lasso = optimize.Lasso(self._getDc(c), self.lambd)
     self.X[c] = lasso.fit(self._getYc(c), Xinit=self.X[c])
     self.X[c] = lasso.coef_