Reputation: 67
Is it possible to add function like predict from sklean library? And how to do it?
def monomial(a,b):
return lambda x : a * math.pow(x,b)
Returns a list of monomials forming a polynomial of the desired order
def polyList(order):
return [monomial(1,i) for i in range(0,order+1)]
Returns the sum of functions for a given input
def evaluate(functionList, x):
return sum([f(x) for f in functionList])
Returns the weighted sum, ie w0f0 + w1f1 +...
def weightedSum(w,F):
if(len(w) != len(F)):
raise Exception("Function/weight size mismatch")
else:
return lambda x:sum([w[i]*F[i](x) for i in range(0,len(w))])
############
Here, we fit the polynomial of the given order with the max likelihood estimates for the weights.
def polyTrain(x,y,order):
#Initialize the weight vector and design matrix
w = [1 for i in range(0,order)]
F = polyList(order)
design = [[f(i) for f in F] for i in x]
#Convert them to numpy arrays
w = numpy.asarray(w)
design = numpy.asarray(design)
#We solve Ax=b, [x values x 3][coefficients]T = [yvalues]
pinv = numpy.linalg.pinv(design)
t = numpy.asarray(y).T
#We know that the ML estimates for w are w* = pinv(design)y.T
w = numpy.dot(pinv,t)
return weightedSum(w,F)
Upvotes: 1
Views: 184
Reputation: 1672
It would be better if you define a class that would handle all logic you want. Nevertheless, If you want to write code that is fully compliant with fit-transform-predict protocol, that is used in scikit-learn, you need to subclass from some base classes of scikit-learn, e.g. BaseEstimator, TransformerMixin, BaseRegressor.
Numpy supplied with very convenient function vander that could significantly help you when you are working with polynomials.
Lets define a class.
class PolyRegressor: # I omit subclassing for now.
def __init__(self, weights=None):
self.weights = np.array(weights) if weights is not None else None
@property
def order(self):
return len(self.weights) if self.weights is not None else 0
def evaluate(self, x):
return np.dot(np.vander(x, self.order), self.weights[:, np.newaxis]).ravel()
def fit(self, X, y=None):
self.weights = (np.linalg.pinv(np.vander(X, self.order)) @ y[:, np.newaxis]).ravel()
def predict(self, X):
if self.weights is not None:
return self.evaluate(X)
else:
raise Exception("Model wasn't fitted. Fit model first. ")
def fit_predict(self, X, y=None):
self.fit(X, y)
return self.predict(X)
reg = PolyRegressor()
reg.weights = np.array([1,2,3]) # we implicitly define order = 2 here, e.g. 3 + 2x + 1x^2
reg.evaluate(np.array([5])) # testing
array([38]) # output
reg.fit_predict(np.random.rand(10), np.random.rand(10) * 5)
array([2.55922997, 1.81433623, 2.29153779, 1.78458414, 1.75961514, 2.59770317, 2.65122647, 1.81313616, 2.61993941, 2.63325695])
Adopt the code for your needs. Hope that helps...
Upvotes: 2