Spaces:
Running
Running
File size: 1,370 Bytes
b865169 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import numpy as np
class Truth:
def __init__(self, transformation, model):
self.transformation = transformation
self.weights = list(model.coef_) + [model.intercept_]
def predict(self, X, y):
transformed = self.transformation.transform(X)
res = np.zeros(shape=y.shape)
for w in range(len(self.weights)):
if w < X.shape[1]:
res = res + (X[:, w] * self.weights[w])
elif w == X.shape[1]:
res = res + (y * self.weights[w])
else:
assert w == X.shape[1] + 1
res = res + self.weights[w]
return res
def transform(self, X):
return self.transformation.transform(X)
def __str__(self):
return f"Auxiliary Truth: {self.transformation} with linear coefficients for X, y, 1 {self.weights}"
def __repr__(self):
return str(self)
def julia_string(self):
"""
Return an expression that sorta creates a julia instances of Truth with these parameters
Specifically Truth(type, params, weights)
Julia indexing starts at 1 not 0 so we need to add 1 to all parameter indices
"""
index = self.transformation.index
params = self.transformation.get_params()
return f"Truth({index}, {[param + 1 for param in params]}, {self.weights})" |