Skip to content
Snippets Groups Projects
Commit 8b774296 authored by Askarpour, Zahra's avatar Askarpour, Zahra
Browse files

test_fitting works

parent 0c054fa4
No related branches found
No related tags found
No related merge requests found
...@@ -61,37 +61,35 @@ class DiffFitting(AbstractFitting): ...@@ -61,37 +61,35 @@ class DiffFitting(AbstractFitting):
def fit(self, vectors: List[np.ndarray], target: np.ndarray): def fit(self, vectors: List[np.ndarray], target: np.ndarray):
"""Given a set of vectors and a target return the fitting """Given a set of vectors and a target return the fitting
coefficients.""" coefficients."""
if len(vectors) == 1:
raise ValueError("DiffFit does not work for one vector")
target=target-vectors[-1] target=target-vectors[-1]
VECTORS=[] VECTORS=[]
print("lenvector", len(vectors)) for i in range(0, len(vectors)+1):
if len(vectors)>1: VECTORS.append(vectors[i]-vectors[-1])
for i in range(2, len(vectors)+1): matrix = np.array(VECTORS).T
print("lenvector", len(vectors)) a = matrix.T @ matrix
VECTORS.append(vectors[i-2]-vectors[-1]) b = matrix.T @ target
print(len(VECTORS)) if self.options["regularization"] > 0.0:
matrix = np.array(VECTORS).T a += np.identity(len(b))*self.options["regularization"]
a = matrix.T @ matrix coefficients = np.linalg.solve(a, b)
b = matrix.T @ target print("coefficients", coefficients)
if self.options["regularization"] > 0.0: return np.array(coefficients, dtype=np.float64)
a += np.identity(len(b))*self.options["regularization"]
coefficients = np.linalg.solve(a, b)
print("coefficients", coefficients)
return np.array(coefficients, dtype=np.float64)
def linear_combination(self, vectors: List[np.ndarray], def linear_combination(self, vectors: List[np.ndarray],
coefficients: np. ndarray) -> np.ndarray: coefficients: np. ndarray) -> np.ndarray:
"""Given a set of vectors (or matrices) and the corresponding """Given a set of vectors (or matrices) and the corresponding
coefficients, build their linear combination.""" coefficients, build their linear combination."""
if len(vectors) == 1:
raise ValueError("DiffFit does not work for one vector")
result = np.zeros(vectors[0].shape, dtype=np.float64) result = np.zeros(vectors[0].shape, dtype=np.float64)
VECTORS_DiffFitting=[] VECTORS_DiffFitting=[]
if len(vectors)>1: for i in range(0,len(vectors)+1):
for i in range(2,len(vectors)+1): VECTORS_DiffFitting.append(vectors[i]-vectors[-1])
VECTORS_DiffFitting.append(vectors[i-2]-vectors[-1]) for coeff, vector in zip(coefficients, VECTORS_DiffFitting):
for coeff, vector in zip(coefficients, vectors): result += vector*coeff
result += vector*coeff result=result+vectors[-1]
result=result+vectors[-1] return result
print(result.shape)
return result
class LeastSquare(AbstractFitting): class LeastSquare(AbstractFitting):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment