Ejemplo n.º 1
0
 def test_detect_submodule_in_deployment(self):
     yh = Yhat("greg", "test", "http://api.yhathq.com/")
     _, bundle = yh.deploy("TestModel",
                           TestModel,
                           globals(),
                           sure=True,
                           dry_run=True)
     self.assertEqual(len(bundle['modules']), 8)
Ejemplo n.º 2
0
import os
from yhat import Yhat, YhatModel
from pricing import Pricing


class MarketingSearchAPI(YhatModel):
    REQUIREMENTS = ["pandas==0.15.2", "numpy"]

    def execute(self, data):
        result = p.predict(data)
        return result


p = Pricing()

username = os.environ["USERNAME"]
apikey = os.environ["APIKEY"]
endpoint = os.environ["OPS_ENDPOINT"]

print "%s:%s:%s" % (
    username,
    apikey,
    endpoint,
)

yh = Yhat(username, apikey, endpoint)
yh.deploy("RelayRidesPricing", MarketingSearchAPI, globals(), sure=True)
Ejemplo n.º 3
0
from yhat import YhatModel, Yhat
import nltk

bot = nltk.chat.eliza.eliza_chatbot
# bot = nltk.chat.iesha.iesha_chatbot


class ChatBot(YhatModel):
    def execute(self, data):
        text = data['text']
        reply = bot.respond(text)
        return {"reply": reply}


print ChatBot().execute({"text": "I'm feeling sad."})

yh = Yhat("greg", "foo", "http://cloud.yhathq.com/")
print yh.deploy("ChatBot", ChatBot, globals())
Ejemplo n.º 4
0
        import base64

    def transform(self, data):
        image_string = data["image_string"]
        STANDARD_SIZE = (50, 50)
        f = StringIO(base64.decodestring(image_string))
        img = Image.open(f)
        img = img.getdata()
        img = img.resize(STANDARD_SIZE)
        img = map(list, img)
        img = np.array(img)
        s = img.shape[0] * img.shape[1]
        img_wide = img.reshape(1, s)
        return img_wide[0]

    def predict(self, img):
        x = self.pca.transform([img])
        x = self.std_scaler.transform(x)
        results = {"label": self.clf.predict(x)[0]}
        probs = {"prob_" + str(i) : prob for i, prob in enumerate(self.clf.predict_proba(x)[0])}
        results['probs'] = probs
        return results

digit_model = DigitModel(clf=clf, std_scaler=std_scaler, pca=pca)

yh = Yhat("YOUR USERNAME", "YOUR APIKEY", "http://cloud.yhathq.com/")
yh.deploy("digitRecognizer", digit_model) 



Ejemplo n.º 5
0

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"]
})
Ejemplo n.º 6
0
# create and train a classifier
nbayes = MultinomialNB(fit_prior=False)
nbayes.fit(train_twitter_tfidf, train.liked_content.tolist())

# prep the test data, then create a confusion matrix to examine the results
test_twitter_tfidf = vec.transform(test.text)
preds = nbayes.predict(test_twitter_tfidf)
print pd.crosstab(test.liked_content, preds)

from yhat import Yhat, YhatModel, preprocess


class TwitterRanker(YhatModel):

    @preprocess(in_type=dict, out_type=dict)
    def execute(self, data):
        tweet = data['tweet_content']
        data = vec.transform([tweet])
        pred = nbayes.predict(data)
        prob = nbayes.predict_proba(data)
        prob = {
            "ham": round(prob[0][0], 4),
            "spam": 1 - round(prob[0][0], 4)
        }
        return {"pred": pred[0], "prob": prob}


yh = Yhat("YOUR_USERNAME", "YOUR_APIKEY", "http://cloud.yhathq.com/")

yh.deploy("twitterRanker", TwitterRanker, globals())
Ejemplo n.º 7
0
    def execute(self, data):
        P = matrix(data['risk_aversion'] * returns_cov.as_matrix())
        q = matrix(-exp_returns['mean'].as_matrix())
        G = matrix(0.0, (len(q), len(q)))
        G[::len(q) + 1] = -1.0
        h = matrix(0.0, (len(q), 1))
        A = matrix(1.0, (1, len(q)))
        b = matrix(1.0)

        solution = solvers.qp(P, q, G, h, A, b)
        expected_return = exp_returns['mean'].dot(solution['x'])[0]
        variance = sum(solution['x'] *
                       returns_cov.as_matrix().dot(solution['x']))[0]

        investments = {}
        for i, amount in enumerate(solution['x']):
            # Ignore values that appear to have converged to 0.
            if amount > 10e-5:
                investments[countries[i]] = amount * 100

        return {
            'risk_aversion': data['risk_aversion'],
            'investments': investments,
            'expected_return': expected_return,
            'variance': variance
        }


yh = Yhat('USERNAME', 'APIKEY', 'http://cloud.yhathq.com/')
yh.deploy('CurrencyPortfolio', CurrencyPortfolio, globals())
Ejemplo n.º 8
0
import os

from yhat import Yhat, YhatModel, preprocess

class HelloWorld(YhatModel):
    version = os.environ["MODEL_VERSION"]
    @preprocess(in_type=dict, out_type=dict)
    def execute(self, data):
        me = data['name']
        greeting = "Hello %s!" % me
        print os.environ["MODEL_VERSION"]
        return { "greeting": greeting }


username = os.environ["USERNAME"]
apikey = os.environ["APIKEY"]
endpoint = os.environ["OPS_ENDPOINT"]

print "%s:%s:%s" % (username, apikey, endpoint,)

yh = Yhat(
    username,
    apikey,
    endpoint
)
yh.deploy("HelloWorldVer", HelloWorld, globals(), sure=True)
Ejemplo n.º 9
0
# 1                4.9               3.0                1.4
# 2                4.7               3.2                1.3
y = pd.DataFrame(iris.data[:, 3:4], columns=iris.feature_names[3:4])
#    petal width (cm)
# 0               0.2
# 1               0.2
# 2               0.2
regr = linear_model.LinearRegression()
regr.fit(X, y)


class LinReg(YhatModel):
    @preprocess(in_type=pd.DataFrame, out_type=pd.DataFrame)
    def execute(self, data):
        prediction = regr.predict(pd.DataFrame(data))
        return prediction


username = os.environ["USERNAME"]
apikey = os.environ["APIKEY"]
endpoint = os.environ["OPS_ENDPOINT"]

print "%s:%s:%s" % (
    username,
    apikey,
    endpoint,
)

yh = Yhat(username, apikey, endpoint)
yh.deploy("LinearRegression", LinReg, globals(), sure=True)
Ejemplo n.º 10
0
from yhat import Yhat, YhatModel , preprocess

x = range(10)
class HelloWorld(YhatModel):
    @preprocess(in_type=dict, out_type=dict)
    def execute(self, data):
        print x[:10]
        me = data['name']
        greeting = "Hello " + str(me) + "!"
        return { "greeting": greeting, "x": x}

# yh = Yhat("greg", "fCVZiLJhS95cnxOrsp5e2VSkk0GfypZqeRCntTD1nHA", "http://cloud.yhathq.com/")
yh = Yhat("greg", "9207b9a2dd9d48848b139b729d4354bc", "http://localhost:8080/")
yh.deploy("NewZippedModel", HelloWorld, globals())
Ejemplo n.º 11
0
import os

from yhat import Yhat, YhatModel, preprocess


class HelloWorld(YhatModel):
    @preprocess(in_type=dict, out_type=dict)
    def execute(self, data):
        me = data['name']
        greeting = "Hello %s!" % me
        return {"greeting": greeting}


username = os.environ["USERNAME"]
apikey = os.environ["APIKEY"]
endpoint = os.environ["OPS_ENDPOINT"]

print "%s:%s:%s" % (
    username,
    apikey,
    endpoint,
)

yh = Yhat(username, apikey, endpoint)
yh.deploy("IndentedModel", HelloWorld, globals(), sure=True)
        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]
})
Ejemplo n.º 13
0
from example_app.models.yhat_model import TestModel, Foo
from yhat import Yhat
import json

yh = Yhat("greg", "foo", "http://api.yhat.com/")
TestModel().execute(1)

# _, bundle = yh.deploy("Foo", TestModel, globals(), dry_run=True)
yh.deploy("Foo", TestModel, globals(), verbose=2)
# print json.dumps(bundle, indent=2)
Ejemplo n.º 14
0
from yhat import Yhat, YhatModel , preprocess
import os
USERNAME = os.environ["USERNAME"]
APIKEY = os.environ["APIKEY"]
URL = os.environ["URL"]

class HelloWorld(YhatModel):
    @preprocess(in_type=dict, out_type=dict)
    def execute(self, data):
        me = data['name']
        greeting = "Hello " + str(me) + "!"
        return { "greeting": greeting }

yh = Yhat(USERNAME, APIKEY, URL)
yh.deploy("Gitmodel", HelloWorld, globals(),True)
Ejemplo n.º 15
0
    def transform(self, data):
        image_string = data["image_string"]
        STANDARD_SIZE = (50, 50)
        f = StringIO(base64.decodestring(image_string))
        img = Image.open(f)
        img = img.getdata()
        img = img.resize(STANDARD_SIZE)
        img = map(list, img)
        img = np.array(img)
        s = img.shape[0] * img.shape[1]
        img_wide = img.reshape(1, s)
        return img_wide[0]

    def predict(self, img):
        x = self.pca.transform([img])
        x = self.std_scaler.transform(x)
        results = {"label": self.clf.predict(x)[0]}
        probs = {
            "prob_" + str(i): prob
            for i, prob in enumerate(self.clf.predict_proba(x)[0])
        }
        results['probs'] = probs
        return results


digit_model = DigitModel(clf=clf, std_scaler=std_scaler, pca=pca)

yh = Yhat("YOUR USERNAME", "YOUR APIKEY", "http://cloud.yhathq.com/")
yh.deploy("digitRecognizer", digit_model)
Ejemplo n.º 16
0
    return p[0:n_recs]

get_sims(["Sierra Nevada Pale Ale", "60 Minute IPA"])

from yhat import Yhat, YhatModel, preprocess

class BeerRecommender(YhatModel):
    REQUIREMENTS=['numpy==1.11.3',
                  'pandas==0.19.2',
                  'scikit-learn==0.18.1']
    def execute(self, data):
        beers = data.get("beers")
        n_recs = data.get("n_recs")
        prob = data.get("prob")
        unique = data.get("unique")

        suggested_beers = get_sims(beers, n_recs, prob, unique)
        result = suggested_beers.to_dict(orient='records')
        return result

model = BeerRecommender()
model.execute({'beers':["Sierra Nevada Pale Ale"],'n_recs':10})

yh = Yhat("colin", "ce796d278f4840e30e763413d8b4baa4", "http://do-sb-dev-master.x.yhat.com/")
print yh.deploy("BeerRecommender", BeerRecommender, globals(), autodetect=False, sure=True)


# print yh.predict("BeerRecommender", {"beers": ["Sierra Nevada Pale Ale",
#                  "120 Minute IPA", "Stone Ruination IPA"]})
Ejemplo n.º 17
0
### Actually deploying our model to Yhat
from yhat import Yhat, YhatModel


class ProductClassifier(YhatModel):
    def execute(self, data):
        if "texts" not in data:
            return {}
        texts = data["texts"]
        return make_prediction(texts)


# example handling a single record
example = {"texts": {"text": "Alpo dog food"}}
pp.pprint(ProductClassifier().execute(example))

# example handling multiple records
example = {"texts": [{"text": "Alpo dog food"}, {"text": "Diet Coke"}]}
pp.pprint(ProductClassifier().execute(example))

YHAT_USERNAME = ""
YHAT_APIKEY = ""

try:
    yh = Yhat(YHAT_USERNAME, YHAT_APIKEY, "http://cloud.yhathq.com/")
except:
    print "Please add in your YHAT_USERNAME and YHAT_APIKEY"
    sys.exit(1)

print yh.deploy("ProductClassifier", ProductClassifier, globals())
Ejemplo n.º 18
0
        self.hprint2('<div id="lineCanvas" style="overflow: auto; position:relative;height:300px;width:400px;"></div>')
        self.hprint2('<script type="text/javascript">')
        self.hprint2('var g = new line_graph();')
        for i in range(len(parcoh)):
            self.hprint2("g.add('%d', %f);"%(i+1, parcoh[i]*100))
        self.hprint2('g.render("lineCanvas", "Paragraphs");')
        self.hprint2('</script>')

    # display text annotation/highlight
    met(d, is_local=is_local, num_label=max(int(good), int(bad)), label_sent=True)
    self.fout.write("<hr>")
    self.fout.write("<label><h2>Cohesion Highlighter</h2></label>")
    if int(good)>0:
        self.fout.write('<span class="bold red">Red: Cohesive </span>')
    if int(bad)>0:
        self.fout.write('<span class="yellow-background">Yellow: Not Cohesive</span>')

    self.hprint2('<div style="width:600px;"><p align="left">')
    d.print_html(self.fout, int(good), int(bad))
    self.hprint2('</p></div>')
    # End Computing and OUtput 

    output = self.fout.getvalue()
    self.fout.close()
    return { "html_output": output }

#StickyTextYhat().run()

yh = Yhat("*****@*****.**", "ff7bb725be9e4a32af286f464b316a23", "http://umsi.yhathq.com/")
yh.deploy ("StickyText", StickyTextYhat, globals())
Ejemplo n.º 19
0
        image = np.array(pilImage)
        resized_image = cv2.resize(image, (224, 224)).astype(np.float32)
        resized_image[:,:,0] -= 103.939
        resized_image[:,:,1] -= 116.779
        resized_image[:,:,2] -= 123.68
        resized_image = resized_image.transpose((2,0,1))
        resized_image = np.expand_dims(resized_image, axis=0)

        out = model.predict(resized_image)
        # pred = dict(zip(labels, model.predict_proba(im)[0]))

        output = []
        guesses = np.array(labels)[np.argsort(out[0])].tolist()
        guesses.reverse()
        for item in guesses[:10]:
            output.append(item)

        output = ", ".join(output)
        guesses = ",".join(output.split(", ")[:5])
        print "It's a %s" % guesses
        return { "guess": guesses }


testdata = {
    "image64": open('./test-image.base64', 'rb').read()
}
print ImageRecognizer().execute(testdata)

yh = Yhat(USERNAME, APIKEY, URL)
yh.deploy("ImageRecognizer", ImageRecognizer, globals(), True)
Ejemplo n.º 20
0
from yhat import Yhat, YhatModel, preprocess
import os

USERNAME = os.environ["USERNAME"]
APIKEY = os.environ["APIKEY"]
URL = os.environ["URL"]


class HelloWorld(YhatModel):
    @preprocess(in_type=dict, out_type=dict)
    def execute(self, data):
        me = data['name']
        greeting = "Hello " + str(me) + "!"
        return {"greeting": greeting}


yh = Yhat(USERNAME, APIKEY, URL)
yh.deploy("Gitmodel", HelloWorld, globals(), True)
Ejemplo n.º 21
0
import os
from yhat import Yhat, YhatModel
from pricing import Pricing

class MarketingSearchAPI(YhatModel):
    REQUIREMENTS = [
        "pandas==0.15.2",
        "numpy"
        ]
    def execute(self, data):
        result = p.predict(data)
        return result

p = Pricing()

username = os.environ["USERNAME"]
apikey = os.environ["APIKEY"]
endpoint = os.environ["OPS_ENDPOINT"]

print "%s:%s:%s" % (username, apikey, endpoint,)

yh = Yhat(
    username,
    apikey,
    endpoint
)
yh.deploy("RelayRidesPricing", MarketingSearchAPI, globals(), sure=True)
Ejemplo n.º 22
0
    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"]})
Ejemplo n.º 23
0
        data = data[features]
        prob = glm.predict_proba(data)[0][1]
        if prob > 0.3:
            decline_code = "Credit score too low"
        else:
            decline_code = ""
        odds = glm.predict_log_proba(data)[0][1]
        score = calculate_score(odds)

        output = {
            "prob_default": [prob],
            "decline_code": [decline_code],
            "score": [score]
        }

        return output

df_term[features].head()

test = {
    "last_fico_range_low": 705,
    "last_fico_range_high": 732,
    "home_ownership": "MORTGAGE"
}

LoanModel().execute(test)

yh = Yhat("colin", "d325fc5bcb83fc197ee01edb58b4b396",
          "https://sandbox.c.yhat.com/")
yh.deploy("LendingClub", LoanModel, globals(), True)
Ejemplo n.º 24
0
        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"})




Ejemplo n.º 25
0
    return p[0:n_recs]

get_sims(["Sierra Nevada Pale Ale", "60 Minute IPA"])

from yhat import Yhat, YhatModel, preprocess

class BeerRecommender(YhatModel):
    REQUIREMENTS=['numpy==1.11.3',
                  'pandas==0.19.2',
                  'scikit-learn==0.18.1']
    def execute(self, data):
        beers = data.get("beers")
        n_recs = data.get("n_recs")
        prob = data.get("prob")
        unique = data.get("unique")

        suggested_beers = get_sims(beers, n_recs, prob, unique)
        result = suggested_beers.to_dict(orient='records')
        return result

model = BeerRecommender()
model.execute({'beers':["Sierra Nevada Pale Ale"],'n_recs':10})

yh = Yhat("demo-master", "3b0160e10f6d7a94a2528b11b1c9bca1", "https://sandbox.c.yhat.com/")
print yh.deploy("BeerRecommender", BeerRecommender, globals(), autodetect=False, sure=True)


# print yh.predict("BeerRecommender", {"beers": ["Sierra Nevada Pale Ale",
#                  "120 Minute IPA", "Stone Ruination IPA"]})
Ejemplo n.º 26
0
features = df.columns[df.columns != "MEDVALUE"]

target = "MEDVALUE"
y = df[target]
X = df.drop(target, 1)
X_train, X_test, y_train, y_test = train_test_split(X, y)

clf = linear_model.LinearRegression()
clf.fit(X_train,y_train)

y_pred = clf.predict(X_test)
print r2_score(y_test, y_pred)

from yhat import Yhat, YhatModel, preprocess, df_to_json

class HousePred(YhatModel):
   @preprocess(in_type=pd.DataFrame, out_type=pd.DataFrame)
   def execute(self, data):
       result = clf.predict(data[features])
       df = pd.DataFrame(data={'predicted_price': result})
       return df

yh = Yhat(
    "YHAT_USERNAME",
    "YHAT_APIKEY",
    "http://cloud.yhathq.com/")

yh.deploy("HouseValuePredictor", HousePred, globals())

print df_to_json(df[:1])
Ejemplo n.º 27
0
        texts = data["texts"]
        return make_prediction(texts)

# example handling a single record
example = {
    "texts": {
        "text": "Alpo dog food"
    }
}
pp.pprint(ProductClassifier().execute(example))

# example handling multiple records
example = {
    "texts": [
        {"text": "Alpo dog food" },
        {"text": "Diet Coke"}
    ]
}
pp.pprint(ProductClassifier().execute(example))

YHAT_USERNAME = ""
YHAT_APIKEY = ""

try:
    yh = Yhat(YHAT_USERNAME, YHAT_APIKEY, "http://cloud.yhathq.com/")
except:
    print "Please add in your YHAT_USERNAME and YHAT_APIKEY"
    sys.exit(1)

print yh.deploy("ProductClassifier", ProductClassifier, globals())
Ejemplo n.º 28
0
from yhat import Yhat, YhatModel , preprocess

class HelloWorld(YhatModel):
    @preprocess(in_type=dict, out_type=dict) 
    def execute(self, data):
        me = data['name']
        greeting = "Hello " + str(me) + "!"
        return { "greeting": greeting }

yh = Yhat("*****@*****.**", "ff7bb725be9e4a32af286f464b316a23", "http://umsi.yhathq.com/")
yh.deploy ("HelloWorld", HelloWorld, globals())
Ejemplo n.º 29
0
 def test_detect_submodule_in_deployment(self):
     yh = Yhat("greg", "test", "http://api.yhathq.com/")
     _, bundle = yh.deploy("TestModel", TestModel, globals(), sure=True, dry_run=True)
     self.assertEqual(len(bundle['modules']), 8)
Ejemplo n.º 30
0
# 1                4.9               3.0                1.4
# 2                4.7               3.2                1.3
y = pd.DataFrame(iris.data[:,3:4], columns=iris.feature_names[3:4])
#    petal width (cm)
# 0               0.2
# 1               0.2
# 2               0.2
regr = linear_model.LinearRegression()
regr.fit(X, y)


class LinReg(YhatModel):
    @preprocess(in_type=pd.DataFrame, out_type=pd.DataFrame)
    def execute(self, data):
       prediction = regr.predict(pd.DataFrame(data))
       return prediction


username = os.environ["USERNAME"]
apikey = os.environ["APIKEY"]
endpoint = os.environ["OPS_ENDPOINT"]

print "%s:%s:%s" % (username, apikey, endpoint,)

yh = Yhat(
    username,
    apikey,
    endpoint
)
yh.deploy("LinearRegression", LinReg, globals(), sure=True)
Ejemplo n.º 31
0
 def test_deployment(self):
     yh = Yhat("foo",  "bar", "http://api.yhathq.com/")
     _, bundle = yh.deploy("HelloWorld", HelloWorld, globals(), dry_run=True)
     self.assertTrue(True)
Ejemplo n.º 32
0
        return self.dv.transform(doc)

    def predict(self, x):
        """
        Evaluate model on array
        delegates to LinearRegression self.lr
        returns a dict (will be json encoded) suppling 
        "predictedPrice", "suspectedOutlier", "x", "threshold" 
        where "x" is the input vector and "threshold" is determined 
        whether or not a listing is a suspected outlier.
        """
        doc = self.dv.inverse_transform(x)[0]
        predicted = self.lr.predict(x)[0]
        err = abs(predicted - doc["price"])
        return {
            "predictedPrice": predicted,
            "x": doc,
            "suspectedOutlier": 1 if (err > self.threshold) else 0,
            "threshold": self.threshold,
        }


pm = PricingModel(dv=dv, lr=LR, threshold=np.percentile(trainingErrs, 95))
print pm.execute(testing.T.to_dict()[0])

if raw_input("Deploy? (y/N): ").lower() == "y":
    username = "******"
    apikey = "abcd1234"
    yh = Yhat(username, apikey, "http://cloud.yhathq.com/")
    print yh.deploy(model_name, fitted_model)
Ejemplo n.º 33
0
from example_app.models.yhat_model import TestModel, Foo
from yhat import Yhat
import json

yh = Yhat(
        "greg",
        "foo",
        "http://api.yhat.com/"
        )
TestModel().execute(1)

# _, bundle = yh.deploy("Foo", TestModel, globals(), dry_run=True)
yh.deploy("Foo", TestModel, globals(), verbose=2)
# print json.dumps(bundle, indent=2)
Ejemplo n.º 34
0
    red_upper = np.array([50, 56, 200], dtype = "uint8")

    mask = cv2.inRange(image, red_lower, red_upper)
    output = cv2.bitwise_and(image, image, mask = mask)
    output_gray = rgb2gray(output)

    total_red = np.sum(output_gray)
    y, x = ndimage.center_of_mass(output_gray)

    data = {
        "x": x,
        "y": y,
        "xmax": output_gray.shape[1],
        "ymax": output_gray.shape[0],
        "total_red": total_red,
        "time": time.time()
    }
    return data

from yhat import Yhat, YhatModel

class DroneModel(YhatModel):
    REQUIREMENTS = [
        "opencv"
    ]
    def execute(self, data):
        return get_coords(data['image64'])

yh = Yhat(username, apikey, url)
yh.deploy("DroneModel", DroneModel, globals(), True)
Ejemplo n.º 35
0
import os

from yhat import Yhat, YhatModel, preprocess

class HelloWorld(YhatModel):

    VERSION = int(os.environ["MODEL_VERSION"])

    @preprocess(in_type=dict, out_type=dict)
    def execute(self, data):
        return { "version": self.VERSION }


username = os.environ["USERNAME"]
apikey = os.environ["APIKEY"]
endpoint = os.environ["OPS_ENDPOINT"]

print "%s:%s:%s" % (username, apikey, endpoint,)

yh = Yhat(
    username,
    apikey,
    endpoint
)
yh.deploy("modelenvvars", HelloWorld, globals(), sure=True)
Ejemplo n.º 36
0

training_val = RFmodel.score(transform_dummies(X_train,False), y_train)
testing_val = RFmodel.score(transform_dummies(X_test,False), y_test)
print "training:", testing_val
print "testing: ", training_val

############ DEPLOYMENT ######################

from yhat import Yhat, YhatModel, preprocess

class TravisModel(YhatModel):
    def fit_val(self):
        testing_val = RFmodel.score(transform_dummies(X_test, False), y_test)
        return testing_val

    def execute(self,data):
        data = transform_dummies(data,False)
        output = RFmodel.predict(data)
        return output.tolist()

########## DEPLOY SET #####################

if __name__ == '__main__':
    yh = Yhat(
        os.environ['YHAT_USERNAME'],
        os.environ['YHAT_APIKEY'],
        os.environ['YHAT_URL'],
    )
    yh.deploy("TravisModel", TravisModel, globals(), True)
Ejemplo n.º 37
0
features = df.columns[df.columns != "MEDVALUE"]

target = "MEDVALUE"
y = df[target]
X = df.drop(target, 1)
X_train, X_test, y_train, y_test = train_test_split(X, y)

clf = linear_model.LinearRegression()
clf.fit(X_train, y_train)

y_pred = clf.predict(X_test)
print r2_score(y_test, y_pred)

from yhat import Yhat, YhatModel, preprocess, df_to_json


class HousePred(YhatModel):
    @preprocess(in_type=pd.DataFrame, out_type=pd.DataFrame)
    def execute(self, data):
        result = clf.predict(data[features])
        df = pd.DataFrame(data={'predicted_price': result})
        return df


yh = Yhat("YHAT_USERNAME", "YHAT_APIKEY", "http://cloud.yhathq.com/")

yh.deploy("HouseValuePredictor", HousePred, globals())

print df_to_json(df[:1])
Ejemplo n.º 38
0
import os

from yhat import Yhat, YhatModel, preprocess


class HelloWorld(YhatModel):
    @preprocess(in_type=dict, out_type=dict)
    def execute(self, data):
        me = data['name']
        greeting = "Hello %s!" % me
        return {"greeting": greeting}


username = os.environ["USERNAME"]
apikey = os.environ["APIKEY"]
endpoint = os.environ["OPS_ENDPOINT"]

print "%s:%s:%s" % (
    username,
    apikey,
    endpoint,
)

yh = Yhat(username, apikey, endpoint)
yh.deploy("HelloWorld", HelloWorld, globals(), sure=True)
Ejemplo n.º 39
0
iris = load_iris()

X = pd.DataFrame(iris.data, columns=iris.feature_names)
y = pd.DataFrame(iris.target, columns=["flower_types"])

clf = SVC()
clf.fit(X, y["flower_types"])


class MySVC(YhatModel):
    @preprocess(in_type=pd.DataFrame, out_type=pd.DataFrame)
    def execute(self, data):
        prediction = clf.predict(pd.DataFrame(data))
        species = ['setosa', 'versicolor', 'virginica']
        result = [species[i] for i in prediction]
        return result

username = os.environ["USERNAME"]
apikey = os.environ["APIKEY"]
endpoint = os.environ["OPS_ENDPOINT"]

print "%s:%s:%s" % (username, apikey, endpoint,)

yh = Yhat(
    username,
    apikey,
    endpoint
)
yh.deploy("SupportVectorClassifier", MySVC, globals(), sure=True)
Ejemplo n.º 40
0
import os

from yhat import Yhat, YhatModel, preprocess
from foo.foo import print_foo
from module import function_in_same_dir


class HelloWorld(YhatModel):
    @preprocess(in_type=dict, out_type=dict)
    def execute(self, data):
        me = data['name']
        greeting = "Hello %s!" % me
        print_foo(me)
        return {"greeting": greeting, "nine": function_in_same_dir()}


username = os.environ["USERNAME"]
apikey = os.environ["APIKEY"]
endpoint = os.environ["OPS_ENDPOINT"]

print "%s:%s:%s" % (
    username,
    apikey,
    endpoint,
)

yh = Yhat(username, apikey, endpoint)
yh.deploy("HelloWorldPkg", HelloWorld, globals(), sure=True, verbose=1)
Ejemplo n.º 41
0
features = [{"name": "x", "na_filler": 0}, {"name": "z", "na_filler": fill_z}]


class MyOtherClass:
    def hello(self, x):
        return "hello: %s" % str(x)


REQS = open("reqs.txt").read()


### <DEPLOYMENT START> ###
# @preprocess(in_type=dict, out_type=pd.DataFrame, null_handler=features)
class MyModel(YhatModel):
    REQUIREMENTS = REQS

    @preprocess(out_type=pd.DataFrame)
    def execute(self, data):
        return predict(data)


# "push" to server would be here

data = {"x": 1, "z": None}

if __name__ == '__main__':
    creds = credentials.read()
    yh = Yhat(creds['username'], creds['apikey'], "http://localhost:3000/")
    yh.deploy("mynewmodel", MyModel, globals())
Ejemplo n.º 42
0
 yahoo_data = aData
 yahoo_data.sort(columns = 'user_id', ascending = True, inplace = True) # no pass by value
 
 # rating-based CF recommendations
 data = {'user': [15], 'products':[123764, 71142],  'n':10}        
 aGraphlab_Model = Graphlab_Recommender(dataset = yahoo_data)
 print aGraphlab_Model.predict(data)
     
 """ USA TODAY """
 # rating-based CF recommendations
 usaToday_data = aData
 param = {'user_id':'Reviewer', 'product_id':'Id', 'ratings': 'Rating'}
 data = {'user': ['Edna Gundersen'], 'products':[123901],  'n':10}  
 aGraphlab_Model = Graphlab_Recommender(dataset = usaToday_data, needed_param = param)
 print aGraphlab_Model.predict(data)
 
 # textual analytics + CF method
 param = {'comment': 'Brief', 'ratings': 'Rating', 'user_id':'Reviewer', 'product_id':'Id'}
 model, ratings_data = rec.sentiment_analysis_regress(usaToday_data, param)
 ratings_data = ratings_data.sort(columns = 'user_id')
 ratings_data['user_id'] = ratings_data['user_id'].fillna('anonymous')
 print ratings_data
 
 aGraphlab_Model = Graphlab_Recommender(dataset = ratings_data)
 data = {'user': ['Edna Gundersen'], 'products':[123901],  'n':10} 
 print aGraphlab_Model.predict(data)
 '''
 # deployment
 yh = Yhat("*****@*****.**", "b36b987283a83e5e4d2814af6ef0eda9", "http://cloud.yhathq.com/")
 yh.deploy("Final_Recommender", Final_Recommender, globals()) 
Ejemplo n.º 43
0
iris = load_iris()

X = pd.DataFrame(iris.data, columns=iris.feature_names)
y = pd.DataFrame(iris.target, columns=["flower_types"])

clf = SVC()
clf.fit(X, y["flower_types"])


class MySVC(YhatModel):
    @preprocess(in_type=pd.DataFrame, out_type=pd.DataFrame)
    def execute(self, data):
        prediction = clf.predict(pd.DataFrame(data))
        species = ['setosa', 'versicolor', 'virginica']
        result = [species[i] for i in prediction]
        return result


username = os.environ["USERNAME"]
apikey = os.environ["APIKEY"]
endpoint = os.environ["OPS_ENDPOINT"]

print "%s:%s:%s" % (
    username,
    apikey,
    endpoint,
)

yh = Yhat(username, apikey, endpoint)
yh.deploy("SupportVectorClassifier", MySVC, globals(), sure=True)
Ejemplo n.º 44
0
    @preprocess(in_type=pd.DataFrame,out_type=pd.DataFrame)
    def execute(self,data):
        # Collect customer meta data
        response = data[['Area Code','Phone']]
        charges = ['Day Charge','Eve Charge','Night Charge','Intl Charge']
        response['customer_worth'] = data[charges].sum(axis=1)
        # Convert yes no columns to bool
        data[yes_no_cols] = data[yes_no_cols] == 'yes'
        # Create feature space
        X = data[features].as_matrix().astype(float)
        X = scaler.transform(X)
        # Make prediction
        churn_prob = clf.predict_proba(X)
        response['churn_prob'] = churn_prob[:,1]
        # Calculate expected loss by churn
        response['expected_loss'] = response['churn_prob'] * response['customer_worth']
        response = response.sort('expected_loss',ascending=False)
        # Return response DataFrame
        return response

yh = Yhat(
    "e[at]yhathq.com", 
    " MY APIKEY ", 
    "http://cloud.yhathq.com/" 
)

print "Deploying model"
response = yh.deploy("PythonChurnModel",ChurnModel,globals())

print json.dumps(response,indent=2)
        data = pd.DataFrame(data)
        data = data[features]
        prob = glm.predict_proba(data)[0][1]
        if prob > 0.3:
            decline_code = "Credit score too low"
        else:
            decline_code = ""
        odds = glm.predict_log_proba(data)[0][1]
        score = calculate_score(odds)

        output = {
            "prob_default": [prob],
            "decline_code": [decline_code],
            "score": [score]
        }

        return output

df_term[features].head()

test = {
    "last_fico_range_low": 705,
    "last_fico_range_high": 732,
    "home_ownership": "MORTGAGE"
}

LoanModel().execute(test)

yh = Yhat("austin", os.environ.get("SCIENCEOPS_API_KEY"), "https://sandbox.c.yhat.com/")
yh.deploy("LendingClub", LoanModel, globals(), True)
Ejemplo n.º 46
0
        {"name": "x", "na_filler": 0},
        {"name": "z", "na_filler": fill_z}
]


class MyOtherClass:
    def hello(self, x):
        return "hello: %s" % str(x)

REQS = open("reqs.txt").read()

### <DEPLOYMENT START> ###
# @preprocess(in_type=dict, out_type=pd.DataFrame, null_handler=features)
class MyModel(YhatModel):
    REQUIREMENTS=REQS
    @preprocess(out_type=pd.DataFrame)
    def execute(self, data):
        return predict(data)

# "push" to server would be here

data = {"x": 1, "z": None}


if __name__ == '__main__':
    creds = credentials.read()
    yh = Yhat(creds['username'], creds['apikey'], "http://localhost:3000/")
    yh.deploy("mynewmodel", MyModel, globals())
    

Ejemplo n.º 47
0
import os
import subprocess

from yhat import Yhat, YhatModel, preprocess

class HelloWorld(YhatModel):

    # ensure the environment has "tree"
    subprocess.check_output(["tree"])

    @preprocess(in_type=dict, out_type=dict)
    def execute(self, data):
        me = data['name']
        greeting = "Hello %s!" % me
        return { "greeting": greeting }


username = os.environ["USERNAME"]
apikey = os.environ["APIKEY"]
endpoint = os.environ["OPS_ENDPOINT"]

print "%s:%s:%s" % (username, apikey, endpoint,)

yh = Yhat(
    username,
    apikey,
    endpoint
)
yh.deploy("PyAptGet", HelloWorld, globals(), sure=True, packages=["tree"])
Ejemplo n.º 48
0
class ChurnModel(YhatModel):
    # Type casts incoming data as a dataframe
    @preprocess(in_type=pd.DataFrame, out_type=pd.DataFrame)
    def execute(self, data):
        # Collect customer meta data
        response = data[['Area Code', 'Phone']]
        charges = ['Day Charge', 'Eve Charge', 'Night Charge', 'Intl Charge']
        response['customer_worth'] = data[charges].sum(axis=1)
        # Convert yes no columns to bool
        data[yes_no_cols] = data[yes_no_cols] == 'yes'
        # Create feature space
        X = data[features].as_matrix().astype(float)
        X = scaler.transform(X)
        # Make prediction
        churn_prob = clf.predict_proba(X)
        response['churn_prob'] = churn_prob[:, 1]
        # Calculate expected loss by churn
        response['expected_loss'] = response['churn_prob'] * response[
            'customer_worth']
        response = response.sort('expected_loss', ascending=False)
        # Return response DataFrame
        return response


yh = Yhat("e[at]yhathq.com", " MY APIKEY ", "http://cloud.yhathq.com/")

print "Deploying model"
response = yh.deploy("PythonChurnModel", ChurnModel, globals())

print json.dumps(response, indent=2)
class CurrencyPortfolio(YhatModel):
    @preprocess(in_type=dict, out_type=dict)
    def execute(self, data):
        P = matrix(data['risk_aversion'] * returns_cov.as_matrix())
        q = matrix(-exp_returns['mean'].as_matrix())
        G = matrix(0.0, (len(q),len(q)))
        G[::len(q)+1] = -1.0
        h = matrix(0.0, (len(q),1))
        A = matrix(1.0, (1,len(q)))
        b = matrix(1.0)

        solution = solvers.qp(P, q, G, h, A, b)
        expected_return = exp_returns['mean'].dot(solution['x'])[0]
        variance = sum(solution['x'] * returns_cov.as_matrix().dot(solution['x']))[0]

        investments = {}
        for i, amount in enumerate(solution['x']):
            # Ignore values that appear to have converged to 0.
            if amount > 10e-5:
                investments[countries[i]] = amount*100

        return {
            'risk_aversion': data['risk_aversion'],
            'investments': investments,
            'expected_return': expected_return,
            'variance': variance
        }

yh = Yhat('USERNAME', 'APIKEY', 'http://cloud.yhathq.com/')
yh.deploy('CurrencyPortfolio', CurrencyPortfolio, globals())
Ejemplo n.º 50
0
import os

from yhat import Yhat, YhatModel, preprocess
from foo.foo import print_foo
from module import function_in_same_dir

class HelloWorld(YhatModel):
    @preprocess(in_type=dict, out_type=dict)
    def execute(self, data):
        me = data['name']
        greeting = "Hello %s!" % me
        print_foo(me)
        return { "greeting": greeting, "nine": function_in_same_dir() }

username = os.environ["USERNAME"]
apikey = os.environ["APIKEY"]
endpoint = os.environ["OPS_ENDPOINT"]

print "%s:%s:%s" % (username, apikey, endpoint,)

yh = Yhat(
    username,
    apikey,
    endpoint
)
yh.deploy("HelloWorldPkg", HelloWorld, globals(), sure=True, verbose=1)
Ejemplo n.º 51
0
def parse_tweet(tweet):
    trees = nltk.ne_chunk(nltk.pos_tag(nltk.word_tokenize(tweet)))

    for tree in trees.subtrees():
        etype = None
        if tree.node == "PERSON":
            etype = "PERSON"
        elif tree.node == "GPE":
            etype = "PLACE"
        if etype is not None:
            ne = " ".join([leaf[0] for leaf in tree.leaves()])
            tweet = tweet.replace(ne,
                                  "<" + etype + ">" + ne + "</" + etype + ">")
    return tweet


class Tagger(YhatModel):
    @preprocess(in_type=dict, out_type=dict)
    def execute(self, raw):
        tweet = raw['text']
        tagged = parse_tweet(tweet)
        raw['tagged'] = tagged
        return raw


tg = Tagger()

yh = Yhat("greg", "mykey", YHAT_URL)
print yh.deploy("NamedEntityTagger" + str(i), Tagger, globals())
Ejemplo n.º 52
0
import os

from yhat import Yhat, YhatModel, preprocess

class HelloWorld(YhatModel):
	@preprocess(in_type=dict, out_type=dict)
	def execute(self, data):
		me = data['name']
		greeting = "Hello %s!" % me
		return { "greeting": greeting }


username = os.environ["USERNAME"]
apikey = os.environ["APIKEY"]
endpoint = os.environ["OPS_ENDPOINT"]

print "%s:%s:%s" % (username, apikey, endpoint,)

yh = Yhat(
    username,
    apikey,
    endpoint
)
yh.deploy("IndentedModel", HelloWorld, globals(), sure=True)
Ejemplo n.º 53
0
    """
    p = dists[products].apply(lambda row: np.sum(row), axis=1)
    p = p.order(ascending=False)
    return p.index[p.index.isin(products)==False]


class BeerRecommender(YhatModel):
    @preprocess(in_type=dict, out_type=dict)
    def execute(self, data):
        # handle uft8 beer names
        beers = [beer.encode('utf8') for beer in data.get("beers", [])]

        suggested_beers = get_sims(beers)
        result = []
        for beer in suggested_beers:
            result.append({"beer": beer})
        return result

username = os.environ["USERNAME"]
apikey = os.environ["APIKEY"]
endpoint = os.environ["OPS_ENDPOINT"]

print "%s:%s:%s" % (username, apikey, endpoint,)

yh = Yhat(
    username,
    apikey,
    endpoint
)
yh.deploy("BeerRecommender", BeerRecommender, globals(), sure=True)
Ejemplo n.º 54
0
from yhat import Yhat, YhatModel, preprocess

x = range(10)


class HelloWorld(YhatModel):
    @preprocess(in_type=dict, out_type=dict)
    def execute(self, data):
        print x[:10]
        me = data['name']
        greeting = "Hello " + str(me) + "!"
        return {"greeting": greeting, "x": x}


# yh = Yhat("greg", "fCVZiLJhS95cnxOrsp5e2VSkk0GfypZqeRCntTD1nHA", "http://cloud.yhathq.com/")
yh = Yhat("greg", "9207b9a2dd9d48848b139b729d4354bc", "http://localhost:8080/")
yh.deploy("NewZippedModel", HelloWorld, globals())
Ejemplo n.º 55
0
    p = dists[products].apply(lambda row: np.sum(row), axis=1)
    p = p.order(ascending=False)
    return p.index[p.index.isin(products) == False]


class BeerRecommender(YhatModel):
    @preprocess(in_type=dict, out_type=dict)
    def execute(self, data):
        # handle uft8 beer names
        beers = [beer.encode('utf8') for beer in data.get("beers", [])]

        suggested_beers = get_sims(beers)
        result = []
        for beer in suggested_beers:
            result.append({"beer": beer})
        return result


username = os.environ["USERNAME"]
apikey = os.environ["APIKEY"]
endpoint = os.environ["OPS_ENDPOINT"]

print "%s:%s:%s" % (
    username,
    apikey,
    endpoint,
)

yh = Yhat(username, apikey, endpoint)
yh.deploy("BeerRecommender", BeerRecommender, globals(), sure=True)