def home(): if request.method == 'POST': yh = Yhat("*****@*****.**", "b36b987283a83e5e4d2814af6ef0eda9", "http://cloud.yhathq.com/") recommender_name = "Final_Recommender" data = {"user" : request.json['user'], "products" : request.json['products'], "n": request.json['n']} pred = yh.predict(recommender_name, data) # returns the dictionary return Response(json.dumps(pred), mimetype='application/json') else: # if it is GET method, you just need to render the homepage part # defines the jQuery pages in order to render the page in home.html template css_url = url_for('static', filename='css/main.css') jquery_url = url_for('static', filename='js/jquery-1.11.1.js') # prodcuts_url = aData products_url = url_for('static', filename='js/products.js') highlight_url = url_for('static', filename='js/highlight.js') main_url = url_for('static', filename='js/main.js') return render_template('home.html', css_url=css_url,jquery_url=jquery_url, products_url=products_url, main_url=main_url, highlight_url=highlight_url)
def index(): if request.method == 'POST': yh = Yhat("USERNAME", "APIKEY", "http://cloud.yhathq.com/") pred = yh.predict("BeerRec", {"beers": request.json['beers'], "n": request.json['n']}) return Response(json.dumps(pred), mimetype='application/json') else: # static files css_url = url_for('static', filename='css/main.css') jquery_url = url_for('static', filename='js/jquery-1.10.2.min.js') beers_url = url_for('static', filename='js/beers.js') highlight_url = url_for('static', filename='js/code.js') js_url = url_for('static', filename='js/main.js') return render_template('index.html', css_url=css_url, jquery_url=jquery_url, beers_url=beers_url, js_url=js_url, highlight_url=highlight_url)
def index(): if request.method == 'POST': yh = Yhat("USERNAME", "APIKEY", "http://cloud.yhathq.com/") pred = yh.predict("BeerRec", { "beers": request.json['beers'], "n": request.json['n'] }) return Response(json.dumps(pred), mimetype='application/json') else: # static files css_url = url_for('static', filename='css/main.css') jquery_url = url_for('static', filename='js/jquery-1.10.2.min.js') beers_url = url_for('static', filename='js/beers.js') highlight_url = url_for('static', filename='js/code.js') js_url = url_for('static', filename='js/main.js') return render_template('index.html', css_url=css_url, jquery_url=jquery_url, beers_url=beers_url, js_url=js_url, highlight_url=highlight_url)
beer = raw_data['beer'] weights = raw_data.get("weights", [1, 1, 1, 1]) # normalize the weights so they sum to 1.0 weights = [float(w) / sum(weights) for w in weights] print "making recs for: " + beer return (beer, weights) def predict(self, data): beer, weights = data results = [] for beer_cmp in self.beers: if beer!=beer_cmp: dist = calc_distance(self.simple_distances, beer, beer_cmp, weights) results.append((beer, beer_cmp, dist)) dists = sorted(results, key=lambda x: x[2]) # return dists return normalize_dists(dists) yh = Yhat({USERNAME}, {APIKEY}) myBeerModel = BeerRec(simple_distances=simple_distances, beers=beers, udfs=[calc_distance, normalize_dists]) if raw_input("Deploy? (y/N)")=="y": print yh.deploy("BeerRec", myBeerModel) print yh.predict("BeerRec", None, {"beer": "Coors Light"})
get_sims(["Sierra Nevada Pale Ale", "120 Minute IPA", "Coors Light"]) # Index([u'Samuel Adams Boston Lager', u'Sierra Nevada Celebration Ale', u'90 Minute IPA', u'Arrogant Bastard Ale', u'Stone IPA (India Pale Ale)', u'60 Minute IPA', u'HopDevil Ale', u'Stone Ruination IPA', u'Sierra Nevada Bigfoot Barleywine Style Ale', u'Storm King Stout', u'Samuel Adams Winter Lager', u'Samuel Adams Summer Ale', u'Prima Pils', u'Anchor Steam Beer', u'Old Rasputin Russian Imperial Stout', u'Samuel Adams Octoberfest', ...], dtype='object') from yhat import Yhat, YhatModel, preprocess class BeerRecommender(YhatModel): @preprocess(in_type=dict, out_type=dict) def execute(self, data): beers = data.get("beers") suggested_beers = get_sims(beers) result = [] for beer in suggested_beers: result.append({"beer": beer}) return result BeerRecommender().execute({ "beers": ["Sierra Nevada Pale Ale", "120 Minute IPA", "Stone Ruination IPA"] }) yh = Yhat("USERNAME", "APIKEY", "http://cloud.yhathq.com") yh.deploy("BeerRecommender", BeerRecommender, globals()) yh.predict("BeerRecommender", { "beers": ["Sierra Nevada Pale Ale", "120 Minute IPA", "Stone Ruination IPA"] })
product - a product id (integer) """ p = dists[products].apply(lambda row: np.sum(row), axis=1) p = p.order(ascending=False) return p.index[p.index.isin(products) == False] get_sims(["Sierra Nevada Pale Ale", "120 Minute IPA", "Stone Ruination IPA"]) from yhat import Yhat, YhatModel, preprocess class BeerRecommender(YhatModel): @preprocess(in_type=dict, out_type=dict) def execute(self, data): beers = data.get("beers") suggested_beers = get_sims(beers) result = [] for beer in suggested_beers: result.append({"beer": beer}) return result yh = Yhat("YOUR_USERNAME", "YOUR_APIKEY", "http://cloud.yhathq.com/") if raw_input("Deploy? (y/N)") == "y": print yh.deploy("BeerRecommender", BeerRecommender, globals()) print yh.predict("BeerRecommender", {"beers": ["Sierra Nevada Pale Ale", "120 Minute IPA", "Stone Ruination IPA"]})
def is_poor_coverage(row): pct_null = float(row.isnull().sum()) / row.count() return pct_null < 0.8 df_head[df_head.apply(is_poor_coverage, axis=1)] df = df[df.apply(is_poor_coverage, axis=1)] df['year_issued'] = df.issue_d.apply(lambda x: int(x.split("-")[0])) df_term = df[df.year_issued < 2012] features = ['last_fico_range_low', 'last_fico_range_high', 'home_ownership'] yh = Yhat("demo-master", "3b0160e10f6d7a94a2528b11b1c9bca1", "https://sandbox.c.yhat.com/") for i, row in df_term[features][:500].iterrows(): # some models require vectorized data, others don't # non-vectorized # row = row.to_dict() # {'is_rent': True, 'last_fico_range_low': 785, 'last_fico_range_high': 789} # vectorized row = { k: [v] for k, v in row.to_dict().items() } # {'is_rent': [True], 'last_fico_range_low': [785], 'last_fico_range_high': [789]} print yh.predict("LendingClub", row) time.sleep(.05)
#!/usr/bin/env python from yhat import Yhat #yh = Yhat("*****@*****.**", "RoVGt5VDZfHkdBLx2rre76sg998cD4IuJiYzzNmNp48") yh = Yhat("*****@*****.**", "HaDobDyJtFoQQPZ9xRkCJrI44OB6EW8hC6IfUMsGzo8") checkoo_models = yh.show_models() for model in checkoo_models['models']: print model newcase = { 'loc':'BeiJing', 'major':'Computer Science/Engineering', 'vtype':'F1', 'ventry':'New', 'byear':'2013', 'bmonth':'7', 'bday':'20' } checkoo_version = 14 print yh.predict('CKModel',checkoo_version,newcase)
return X def predict(self, data): pred = self.clf.predict(data).tolist() return pred clf = pickle.load(open('./yhat/test/decisiontree.model', 'rb')) xtrain = pickle.load(open('./yhat/test/xtrain.model', 'rb')) myTree = DecisionTreePML(clf=clf, xtrain=xtrain) pp.pprint(yh.upload("chTestTree", myTree)) print yh.predict("chTestTree", 2, [2, 3, 2, 4, 2, 3, 2])
import time from yhat import Yhat # cd ~/repos/yhat/demos/heroku-demos/demo-lending-club/model df = pd.read_csv("./model/LoanStats3a.csv", skiprows=1) df_head = df.head() def is_poor_coverage(row): pct_null = float(row.isnull().sum()) / row.count() return pct_null < 0.8 df_head[df_head.apply(is_poor_coverage, axis=1)] df = df[df.apply(is_poor_coverage, axis=1)] df['year_issued'] = df.issue_d.apply(lambda x: int(x.split("-")[0])) df_term = df[df.year_issued < 2012] features = ['last_fico_range_low', 'last_fico_range_high', 'home_ownership'] yh = Yhat("demo-master", "3b0160e10f6d7a94a2528b11b1c9bca1", "https://sandbox.c.yhat.com/") for i, row in df_term[features][:500].iterrows(): # some models require vectorized data, others don't # non-vectorized # row = row.to_dict() # {'is_rent': True, 'last_fico_range_low': 785, 'last_fico_range_high': 789} # vectorized row = { k: [v] for k,v in row.to_dict().items() } # {'is_rent': [True], 'last_fico_range_low': [785], 'last_fico_range_high': [789]} print yh.predict("LendingClub", row) time.sleep(.05)
from yhat import Yhat import string import pandas as pd yh = Yhat("colin", "d325fc5bcb83fc197ee01edb58b4b396", "https://sandbox.c.yhat.com/") filename = "http://yhat-data.s3.amazonaws.com/beer_reviews.csv" df = pd.read_csv(filename) printable = set(string.printable) df.beer_name = df.beer_name.map(lambda y: filter(lambda x: x in printable, y)) for row in df.beer_name.unique(): data = {"beers": [row]} try: result = yh.predict("BeerRecommender", data)['result'] print(result[:5]) except: pass
img = np.array(img) s = img.shape[0] * img.shape[1] img_wide = img.reshape(1, s) return self.pca.transform(img_wide[0]) def predict(self, data): preds = self.knn.predict(data) preds = np.where(preds==1, "check", "drivers_license") pred = preds[0] return {"image_label": pred} img_clf = ImageClassifier(pca=pca, knn=knn, STANDARD_SIZE=STANDARD_SIZE) # authenticate yh = Yhat("YOUR USERNAME", "YOUR API KEY") # upload model to yhat yh.upload("imageClassifier", img_clf) #call the api w/ some example data # this one is a drivers license new_image = open("dl16.jpeg", 'rb').read() import base64 #we need to make the image JSON serializeable new_image = base64.encodestring(new_image) print yh.predict("imageClassifier", version=4, data=new_image)
return (beer, weights) def predict(self, data): beer, weights = data results = [] for beer_cmp in self.beers: if beer != beer_cmp: dist = calc_distance(self.simple_distances, beer, beer_cmp, weights) results.append((beer, beer_cmp, dist)) return sorted(results, key=lambda x: x[2]) #Deploy to Yhat yh = Yhat("{USERNAME}", "{APIKEY}") br = BeerRec(simple_distances=simple_distances, beers=beers, udfs=[calc_distance]) yh.deploy("PydataBeerRec", br) #Test it Out yh.predict("PydataBeerRec", 1, { "beer": "Coors Light", "weights": [1, 1, 1, 1] }) yh.predict("PydataBeerRec", 1, { "beer": "Coors Light", "weights": [2, 1, 0, 0] })
from yhat import Yhat import string import pandas as pd yh = Yhat("colin", "d325fc5bcb83fc197ee01edb58b4b396", "https://sandbox.c.yhat.com/") filename = "http://yhat-data.s3.amazonaws.com/beer_reviews.csv" df = pd.read_csv(filename) printable = set(string.printable) df.beer_name = df.beer_name.map(lambda y: filter(lambda x: x in printable, y)) for row in df.beer_name.unique(): data = { "beers": [row] } try: result = yh.predict("BeerRecommender", data)['result'] print(result[:5]) except: pass
weights = raw_data.get("weights", [1, 1, 1, 1]) # normalize the weights so they sum to 1.0 weights = [float(w) / sum(weights) for w in weights] return (beer, weights) def predict(self, data): beer, weights = data results = [] for beer_cmp in self.beers: if beer!=beer_cmp: dist = calc_distance(self.simple_distances, beer, beer_cmp, weights) results.append((beer, beer_cmp, dist)) return sorted(results, key=lambda x: x[2]) #Deploy to Yhat yh = Yhat("{USERNAME}", "{APIKEY}") br = BeerRec(simple_distances=simple_distances, beers=beers, udfs=[calc_distance]) yh.deploy("PydataBeerRec", br) #Test it Out yh.predict("PydataBeerRec", 1, {"beer": "Coors Light", "weights": [1, 1, 1, 1]}) yh.predict("PydataBeerRec", 1, {"beer": "Coors Light", "weights": [2, 1, 0, 0]})