def test_norm(self, count_matrix_dataset, vectorizer): """Normalization test.""" _, x, y = count_matrix_dataset v = vectorizer(norm="l2") v.fit(x, y) matrix = v.transform(x) norm = sp_norm(matrix, axis=1) np.testing.assert_allclose(np.mean(norm), 1.)
def norm(self): if self.isnone: return None if self.mode == MODE_NP or (self.mode == MODE_SP and self.type == 'vector'): return np_norm(self.x) if self.mode == MODE_TT: return self.x.norm() if self.mode == MODE_SP and self.type == 'matrix': return sp_norm(self.x)
def compute_cosine_similarity( sentence_vector: Union[np.ndarray, sp_sp.spmatrix], sentence_vector2: Union[np.ndarray, sp_sp.spmatrix]) -> float: """ Params: - sentence_vector: sentence vector - sentence_vector2: sentence vector2 returns: similarity score of the two sentences """ score = sentence_vector @ (sentence_vector2.transpose()) if isinstance(sentence_vector, sp_sp.spmatrix) and \ isinstance(sentence_vector2, sp_sp.spmatrix): score = score.todense()[0, 0] nrm, nrm2 = sp_norm(sentence_vector), sp_norm(sentence_vector2) score = score / (nrm * nrm2) if nrm * nrm2 > 0 else 0 else: nrm, nrm2 = norm(sentence_vector), norm(sentence_vector2) score = score / (nrm * nrm2) if nrm * nrm2 > 0 else 0 return score
def _get_error(self, rec, test_matrix, norm_ord=2): total_error = 0 test_users = test_matrix.shape[0] for i in range(test_users): vec = test_matrix.getrow(i) dropped_vec = self._drop_entries(vec, 0.25) estimated_vec = rec.get_estimated_vector(dropped_vec) total_error += sp_norm(vec - estimated_vec, ord=norm_ord, axis=0)[0] return total_error
from oppgave4c import oppgave4c y4_c = oppgave4c() E = 1.3 * 10**10 I = (0.3 * 0.03**3) / 12 f = -480 * 0.3 * 0.03 * 9.81 # Regner ut vektoren med lengde 10 y4_e = (f / (E * I)) * ones((10, 1)) forward_error = norm(y4_e - y4_c, ord=inf) rel_forward_error = forward_error / norm(y4_e, ord=inf) # Antar at den relative bakoverfeilen er 2^-52 rel_backwards_error = 2**(-52) # Feilforstørringen er relativ foroverfeil delt på relativ bakoverfeil error_magnification = rel_forward_error / rel_backwards_error # Lager matrisen fra oppgave 2 A = lagA(10) # Konverterer matrisen til en 'sparse matrix' for å inverse den A = csc_matrix(A) A_Invers = inv(A) # Kondisjonstallet cond_A = sp_norm(A, ord=inf) * sp_norm(A_Invers, ord=inf) # Printer ut svarene print("Foroverfeil: ", forward_error) print("Relativ foroverfeil:", rel_forward_error) print("Feilforstørring:", error_magnification) print("Kondisjonstall for A:", cond_A)