def summer(idx_vals, idx_cols): idxs = [i[0] for i in idx_vals] if len(idxs) == 1: return np.Vector(*idx_cols[idxs[0]]) else: mutate_idxs = list(idx_vals) tail = mutate_idxs[-1][0] head = tuple(mutate_idxs[:-1]) return np.Vector(*idx_cols[tail]) + summer(head, idx_cols)
def fit(self, X, y): cols = len(X[0]) rows = len(X) beta = np.Vector(*cols * [0]) iteration = 0 XT = t(X) while iteration < self.max_iter: old_beta = np.Vector(*beta) for j in range(len(beta)): beta = self.step(X, XT, beta, y, j) iteration += 1 if converged(beta, old_beta): break self.coef_ = beta return self
def b1(size): def vec(size): return [urandom.getrandbits(8) / 100 for i in range(size)] mat = (vec(size) for i in range(size)) v = np.Vector(*vec(size)) res = v.gen_matrix_mult(mat) return
def step(self, X, XT, beta, y, j): tmp_beta = np.change_idx(beta, j, 0.0) r_j = y - self.cached_mult(tmp_beta, XT, recursive=True) arg1 = r_j.inner(XT[j]) arg2 = self.alpha * len(X) new_beta_j = self._soft_thresholding_operator(arg1, arg2) / sum( np.Vector(*XT[j]).__pow__(2)) return np.change_idx(beta, j, new_beta_j)
def cached_mult(self, tmp_beta, XT, recursive=False): active_coefs = get_active(tmp_beta) if active_coefs: if recursive: all_cols = dict( ((zipped_idx_val[0], row_multer(zipped_idx_val, XT)) for zipped_idx_val in active_coefs)) idxs = tuple(active_coefs) summed_cols = group_summer(idxs, all_cols) else: all_cols = (row_multer(zipped_idx_val, XT) for zipped_idx_val in active_coefs) summed_cols = np.Vector(*(sum(i) for i in zip(*all_cols))) # return summed_cols else: dummy_lst = len(XT[0]) * [0] summed_cols = np.Vector(*dummy_lst) return summed_cols
def isft(spectrum): inverse_W = ft.inv_dft_mat(len(spectrum)) v = np.Vector(*spectrum) return spectrum.matrix_mult(inverse_W)
def mem_friendly_sft(signal): N = len(signal) W = ft.bp_fourier_generator(N) v = np.Vector(*signal) return v.gen_matrix_mult(W)
def sft(signal): N = len(signal) W = ft.bp_fourier(N) v = np.Vector(*signal) return v.matrix_mult(W)
def inverse_ft(spectrum): N = len(spectrum) + 1 W = ft.bp_fourier(N) vec = np.Vector(*spectrum) return vec.matrix_mult(W)
def sparseft(signal, alpha=1e-4): N = len(signal) W = ft.bp_fourier(N) v = np.Vector(*signal).zero_mean_normalize() spectrum = sk_bp(W, v, alpha) return spectrum
def atom_norm(atom): return list(np.Vector(*atom).zero_mean_normalize())
def intra_summer(idx_vals, idx_cols): all_cols = (idx_cols[v[0]] for v in idx_vals) summed_cols = np.Vector(*(sum(i) for i in zip(*all_cols))) # return summed_cols