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):
def fit(self, vectors: List[np.ndarray], target: np.ndarray):
"""Given a set of vectors and a target return the fitting
coefficients."""
if len(vectors) == 1:
raise ValueError("DiffFit does not work for one vector")
target=target-vectors[-1]
VECTORS=[]
print("lenvector", len(vectors))
if len(vectors)>1:
for i in range(2, len(vectors)+1):
print("lenvector", len(vectors))
VECTORS.append(vectors[i-2]-vectors[-1])
print(len(VECTORS))
matrix = np.array(VECTORS).T
a = matrix.T @ matrix
b = matrix.T @ target
if self.options["regularization"] > 0.0:
a += np.identity(len(b))*self.options["regularization"]
coefficients = np.linalg.solve(a, b)
print("coefficients", coefficients)
return np.array(coefficients, dtype=np.float64)
for i in range(0, len(vectors)+1):
VECTORS.append(vectors[i]-vectors[-1])
matrix = np.array(VECTORS).T
a = matrix.T @ matrix
b = matrix.T @ target
if self.options["regularization"] > 0.0:
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],
coefficients: np. ndarray) -> np.ndarray:
"""Given a set of vectors (or matrices) and the corresponding
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)
VECTORS_DiffFitting=[]
if len(vectors)>1:
for i in range(2,len(vectors)+1):
VECTORS_DiffFitting.append(vectors[i-2]-vectors[-1])
for coeff, vector in zip(coefficients, vectors):
result += vector*coeff
result=result+vectors[-1]
print(result.shape)
return result
for i in range(0,len(vectors)+1):
VECTORS_DiffFitting.append(vectors[i]-vectors[-1])
for coeff, vector in zip(coefficients, VECTORS_DiffFitting):
result += vector*coeff
result=result+vectors[-1]
return result
class LeastSquare(AbstractFitting):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment