Skip to content

Instantly share code, notes, and snippets.

@cdaringe
Created November 15, 2017 04:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdaringe/ab678fe4b3dfca87f7906b2faf08f976 to your computer and use it in GitHub Desktop.
Save cdaringe/ab678fe4b3dfca87f7906b2faf08f976 to your computer and use it in GitHub Desktop.
make a model and predict hypothetical max on-the-fly
import sys
import json
from sklearn import linear_model
import numpy as np
from scipy.optimize import basinhopping
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
class Guess:
@staticmethod
def ridge_regression_with_sim_ann(X, Y):
# reg = linear_model.Ridge().fit(X, Y)
model = make_pipeline(PolynomialFeatures(4), linear_model.Ridge())
reg = model.fit(X, Y)
def explore(x):
all_zero_plus = np.all(x >= 0)
all_one_or_under = np.all(x <= 1)
if all_one_or_under and all_zero_plus:
return -1 * reg.predict([x])
return 0
return basinhopping(explore, X[0], niter=1000)
@staticmethod
def from_stdin():
sys.stderr.write('Reading input\n')
request = json.load(sys.stdin)
response = {}
for learner in request['learners']:
X = request['X']
sys.stderr.write(f'Execing learner ({len(X)} records): {learner}\n')
reg = getattr(Guess, learner)(X, request['Y'])
response[learner] = list(reg.x)
sys.stderr.write('writing response\n')
sys.stdout.write(json.dumps(response))
if __name__ == "__main__":
sys.stderr.write('Launching Guess\n')
Guess.from_stdin()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment