本文整理汇总了Python中yhat.Yhat类的典型用法代码示例。如果您正苦于以下问题:Python Yhat类的具体用法?Python Yhat怎么用?Python Yhat使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Yhat类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: home
def home():
if request.method == 'POST':
yh = Yhat("[email protected]", "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)
示例2: index
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)
示例3: is_poor_coverage
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)
示例4: HelloWorld
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)
示例5: HelloWorld
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)
示例6: hello
{"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())
示例7: execute
@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)
示例8: Graphlab_Recommender
""" YAHOO """
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("[email protected]", "b36b987283a83e5e4d2814af6ef0eda9", "http://cloud.yhathq.com/")
yh.deploy("Final_Recommender", Final_Recommender, globals())
示例9: TravisModel
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)
示例10: Exception
"k10": 1,
"k11": 1,
"k12": 1,
"k13": 1,
"k14": 1,
"k15": 1,
}
test_data = pd.read_csv(open("data/test.csv", "r"), quotechar='"')
sub_data = pd.read_csv(open("data/sampleSubmission.csv", "r"), quotechar='"')
if not np.alltrue(test_data["id"] == sub_data["id"]):
raise Exception("IDs do not match")
yh = Yhat(username, apikey)
variabless = sub_data.columns[1:]
raw_tweets = test_data["tweet"].tolist()
for variable in variables:
model_version = best_model[variable]
model_name = "TweetClassifier_%s" % (variable,)
results_from_server = yh.raw_predict(model_name, model_version, raw_tweets)
pred = results_from_server["prediction"]["scores"]
sub_data[variable] = pred
try:
sub_data.to_csv(open(sub_file, "w"), index=False)
except IOError:
sys.stderr.write("IO error: could not write data to file")
示例11: Yhat
import json
import pickle
import time
# raw_data = [[ 0., 0., 5., 13., 9., 1., 0., 0.],
# [ 0., 0., 13., 15., 10., 15., 5., 0.],
# [ 0., 3., 15., 2., 0., 11., 8., 0.],
# [ 0., 4., 12., 0., 0., 8., 8., 0.],
# [ 0., 5., 8., 0., 0., 9., 8., 0.],
# [ 0., 4., 11., 0., 1., 12., 7., 0.],
# [ 0., 2., 14., 5., 10., 12., 0., 0.],
# [ 0., 0., 6., 13., 10., 0., 0., 0.]]
# yh = Yhat("greg", "fCVZiLJhS95cnxOrsp5e2VSkk0GfypZqeRCntTD1nHA", "http://localhost:5000/")
yh = Yhat("clotheshorse", "gwAaXlkkIyasM2ue7iwjUmuoUKCodSZjobNU9a5WmKc", "http://166.78.26.170/")
# yh = Yhat("greg", "fCVZiLJhS95cnxOrsp5e2VSkk0GfypZqeRCntTD1nHA", "http://54.235.251.150/")
# # pp.pprint(skd.show_models())
# # print "*"*80
# s = time.time()
# pp.pprint(yh.raw_predict('gregsTree_v11', [2, 3, 2, 2]))
# print time.time() - s
# # print "*"*80
# # pp.pprint(skd.predict('digits', raw_data))
# # print "*"*80
class DecisionTreePML(BaseModel):
def transform(self, rawData):
pair = [5, 3]
示例12: hello
from yhat import Yhat, BaseModel
def hello():
return "HEY AUSTIN!"
class MyModel(BaseModel):
def require(self):
pass
def transform(self, data):
return "something"
def predict(self, data):
return data * 10
mm = MyModel(clf=range(10), udfs=[hello])
yh = Yhat("greg", "abcd1234")
yh.upload("functest", mm)
示例13: CountVectorizer
pred[np.where(pred < 0.0)] = 0.0
return {"scores" : pred}
train_data = pd.read_csv(open('data/train.csv','r'),quotechar='"')
raw_tweets = train_data['tweet'].tolist()
sanity_raw = raw_tweets[:100]
sentiments = train_data.columns[4:].tolist()
vectorizer = CountVectorizer(tokenizer=nltk.word_tokenize,
stop_words='english',
max_features=3000,
binary=True,
ngram_range=(1,1))
yh = Yhat(username,apikey)
X_train = vectorizer.fit_transform(raw_tweets)
for sentiment in sentiments:
print "Processing '%s'" % sentiment
clf = SVR()
y_train = train_data[sentiment].tolist()
print "Training classifier"
clf.fit(X_train,y_train)
tweet_clf = TweetClassifier(clf=clf,vectorizer=vectorizer)
model_name = "TweetClassifier_%s" % (sentiment,)
print "Uploading to yhat"
示例14: CurrencyPortfolio
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())
开发者ID:finallybiubiu,项目名称:currency-portfolio-optimization,代码行数:30,代码来源:currency-portfolio-scienceops.py
示例15: load_iris
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)