Skip to content

Instantly share code, notes, and snippets.

@alexcasalboni
Last active January 21, 2020 14:20
Show Gist options
  • Save alexcasalboni/cf11cc076ad70a445612 to your computer and use it in GitHub Desktop.
Save alexcasalboni/cf11cc076ad70a445612 to your computer and use it in GitHub Desktop.
Google Prediction API - Train a classification model and generate a new Prediction
import httplib2, argparse, os, sys, json
from oauth2client import tools, file, client
from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient import discovery
from googleapiclient.errors import HttpError
#Project and model configuration
project_id = '132567073760'
model_id = 'HAR-model'
#activity labels
labels = {
'1': 'walking', '2': 'walking upstairs',
'3': 'walking downstairs', '4': 'sitting',
'5': 'standing', '6': 'laying'
}
def main():
""" Simple logic: train and make prediction """
try:
make_prediction()
except HttpError as e:
if e.resp.status == 404: #model does not exist
print("Model does not exist yet.")
train_model()
make_prediction()
else: #real error
print(e)
def make_prediction():
""" Use trained model to generate a new prediction """
api = get_prediction_api()
print("Fetching model.")
model = api.trainedmodels().get(project=project_id, id=model_id).execute()
if model.get('trainingStatus') != 'DONE':
print("Model is (still) training. \nPlease wait and run me again!") #no polling
exit()
print("Model is ready.")
"""
#Optionally analyze model stats (big json!)
analysis = api.trainedmodels().analyze(project=project_id, id=model_id).execute()
print(analysis)
exit()
"""
#read new record from local file
with open('record.csv') as f:
record = f.readline().split(',') #csv
#obtain new prediction
prediction = api.trainedmodels().predict(project=project_id, id=model_id, body={
'input': {
'csvInstance': record
},
}).execute()
#retrieve classified label and reliability measures for each class
label = prediction.get('outputLabel')
stats = prediction.get('outputMulti')
#show results
print("You are currently %s (class %s)." % (labels[label], label) )
print(stats)
def train_model():
""" Create new classification model """
api = get_prediction_api()
print("Creating new Model.")
api.trainedmodels().insert(project=project_id, body={
'id': model_id,
'storageDataLocation': 'machine-learning-dataset/dataset.csv',
'modelType': 'CLASSIFICATION'
}).execute()
def get_prediction_api(service_account=True):
scope = [
'https://www.googleapis.com/auth/prediction',
'https://www.googleapis.com/auth/devstorage.read_only'
]
return get_api('prediction', scope, service_account)
def get_api(api, scope, service_account=True):
""" Build API client based on oAuth2 authentication """
STORAGE = file.Storage('oAuth2.json') #local storage of oAuth tokens
credentials = STORAGE.get()
if credentials is None or credentials.invalid: #check if new oAuth flow is needed
if service_account: #server 2 server flow
credentials = ServiceAccountCredentials('service_account.json', scopes=scope)
STORAGE.put(credentials)
else: #normal oAuth2 flow
CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), 'client_secrets.json')
FLOW = client.flow_from_clientsecrets(CLIENT_SECRETS, scope=scope)
PARSER = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter, parents=[tools.argparser])
FLAGS = PARSER.parse_args(sys.argv[1:])
credentials = tools.run_flow(FLOW, STORAGE, FLAGS)
#wrap http with credentials
http = credentials.authorize(httplib2.Http())
return discovery.build(api, "v1.6", http=http)
if __name__ == '__main__':
main()
2.2585820e-001 -6.4337079e-002 -1.2906429e-001 -3.8622497e-001 -1.2799570e-001 -4.6773530e-001 -4.3733455e-001 -1.0040433e-001 -4.4655558e-001 1.1052716e-003 -1.3443835e-001 -4.2222072e-001 3.7355132e-001 2.3643439e-001 6.3083776e-001 -2.7654688e-001 -8.0976397e-001 -8.4846275e-001 -8.6877267e-001 -5.9383966e-001 -2.1872974e-001 -4.3564305e-001 1.3534978e-001 1.8546303e-001 -8.6333534e-002 -2.9852723e-001 1.0659337e-001 -8.5012453e-003 1.9948233e-001 -3.1339403e-001 1.5025406e-001 9.9816074e-002 9.8384724e-002 -8.1678845e-002 9.2325498e-002 -1.9920893e-001 1.1315125e-001 -4.4172111e-001 2.8091853e-002 -4.6398262e-003 9.0684540e-001 -3.3951374e-001 6.4983997e-002 -9.5176161e-001 -8.8035855e-001 -9.1519720e-001 -9.5130928e-001 -8.8313966e-001 -9.1526023e-001 8.4948964e-001 -3.3471054e-001 7.4042580e-002 9.0508978e-001 -3.5292981e-001 4.8071843e-002 -7.9295511e-002 7.5311478e-001 -7.9969327e-001 -9.9271019e-001 -9.5492391e-001 -8.9106633e-001 -9.0466018e-001 -1.9635835e-001 -1.0000000e+000 -6.1979193e-002 -7.0483535e-001 7.5007032e-001 -7.9514346e-001 8.4012757e-001 -6.9576989e-001 7.2247903e-001 -7.7215783e-001 8.2990607e-001 -7.0810216e-001 7.2127135e-001 -7.3373490e-001 7.4245260e-001 8.8806509e-001 6.7908309e-001 8.9739502e-001 1.6038913e-001 7.6787118e-002 1.1296185e-002 -5.3042758e-001 -5.3402968e-001 -7.5910197e-001 -5.3612863e-001 -4.6472329e-001 -7.5824377e-001 -7.6620770e-001 -7.6859086e-001 -8.1373476e-001 4.2132509e-001 6.4764461e-001 6.6783529e-001 -5.6802635e-001 -8.8747930e-001 -8.8797463e-001 -9.6785144e-001 -5.6730384e-001 -4.0263435e-001 -7.6219872e-001 6.1407041e-001 4.3747766e-001 3.5262469e-001 -3.3422469e-001 8.2253347e-002 -5.3773451e-002 1.4894165e-002 -3.7149977e-001 -4.9337536e-002 7.1421642e-002 1.0648387e-001 -1.2413135e-001 1.9132702e-001 -2.4165033e-001 1.1284394e-001 -2.9634537e-001 4.8818787e-002 -3.3439191e-002 1.2378197e-001 -2.4110192e-001 5.5537163e-002 -3.6533033e-001 -3.8362723e-001 -4.9685924e-001 -3.8366793e-001 -3.8497401e-001 -5.0096898e-001 -1.8451287e-001 -6.6349379e-001 -3.8075555e-001 5.2552383e-001 5.6173205e-001 4.6068690e-001 -2.6458058e-001 -8.0148285e-001 -8.0731455e-001 -8.8156164e-001 -3.6756547e-001 -4.0191214e-001 -5.5311194e-001 2.7628352e-001 1.3384881e-001 1.6143102e-001 -4.7314882e-001 3.3955786e-001 2.8282816e-002 -1.5821893e-001 -3.6568463e-001 2.0139763e-001 -5.7519140e-002 1.8966789e-001 -2.7905967e-001 1.5737244e-001 5.2581223e-002 3.5395602e-003 -6.0784075e-001 -1.8801288e-001 -4.3009936e-002 -2.1777800e-001 -6.3568555e-002 -7.3375069e-003 -6.3082675e-001 -8.1803377e-001 -6.9966038e-001 -6.3363425e-001 -8.1368040e-001 -6.8517560e-001 -6.4300425e-001 -8.3927419e-001 -6.3405600e-001 6.4899387e-001 8.9158741e-001 8.0510513e-001 -7.3479744e-001 -9.3057893e-001 -9.8304308e-001 -9.5343023e-001 -6.6000803e-001 -8.0494940e-001 -7.0189904e-001 4.2830904e-001 3.8876794e-001 5.5888676e-001 -2.5816452e-001 1.3164237e-001 1.4739430e-001 2.4958695e-002 -2.9042104e-001 9.8652798e-002 -1.4539374e-002 9.0483747e-003 -1.8617174e-001 -4.9134151e-003 1.8873689e-001 -1.5057135e-001 -3.6574078e-001 -9.1400688e-002 -1.9210412e-001 -2.8483507e-001 -4.2450595e-001 -5.2290654e-001 -2.4344503e-001 -6.3838861e-001 -2.8483507e-001 -7.3985669e-001 -7.0085423e-001 6.7023873e-001 -3.6081913e-001 2.0965642e-001 -3.7712757e-002 9.7816367e-002 -2.8483507e-001 -4.2450595e-001 -5.2290654e-001 -2.4344503e-001 -6.3838861e-001 -2.8483507e-001 -7.3985669e-001 -7.0085423e-001 6.7023873e-001 -3.6081913e-001 2.0965642e-001 -3.7712757e-002 9.7816367e-002 -5.6144277e-001 -5.9343006e-001 -6.1574440e-001 -5.5980134e-001 -8.6150457e-001 -5.6144277e-001 -9.0093913e-001 -6.9411216e-001 5.2614891e-001 4.5718522e-002 1.0664807e-002 3.2420720e-002 -2.1210145e-001 -2.8160293e-001 -3.2360890e-001 -3.1138062e-001 -3.7058912e-001 -4.7887657e-001 -2.8160293e-001 -7.2032853e-001 -4.4919971e-001 7.4524416e-001 -3.5270408e-001 2.4794151e-001 -1.4290312e-001 1.2737058e-001 -7.3735135e-001 -7.9854843e-001 -8.0439117e-001 -8.1603404e-001 -7.8523585e-001 -7.3735135e-001 -9.6806627e-001 -8.0890720e-001 7.4284787e-001 1.3847218e-002 1.1241768e-001 -1.9868795e-001 -2.0163434e-001 -4.6081405e-001 -3.2062223e-001 -6.0155801e-001 -3.5905247e-001 -9.4478660e-002 -4.4183742e-001 -3.8740554e-001 -2.2062818e-001 -5.6029735e-001 -3.7089593e-001 -1.4984590e-001 -3.7262264e-001 -8.6502122e-001 -7.5237455e-001 -9.9023099e-001 -3.8450425e-001 -8.0981307e-001 -6.1486067e-001 -8.5188362e-001 -5.9638733e-001 -4.8060398e-001 -7.4791603e-001 4.5377549e-001 3.3857900e-001 1.9550256e-001 -8.0645161e-001 -8.0000000e-001 -9.2307692e-001 -4.5532838e-001 -3.6767469e-001 -2.8534497e-001 2.5017339e-001 -5.4544122e-002 1.4525833e-001 -1.3954168e-001 1.4582668e-001 -1.2727472e-001 -7.7445563e-001 -9.1716087e-001 -9.4817652e-001 -9.2187918e-001 -9.4373042e-001 -9.3902103e-001 -9.3246255e-001 -9.7782730e-001 -7.9552833e-001 -9.3221743e-001 -9.4197972e-001 -9.4766765e-001 -8.0637961e-001 -9.1633386e-001 -5.1230286e-001 -9.1389832e-001 -9.4856271e-001 -9.6615763e-001 -9.5855087e-001 -9.4170925e-001 -9.6339579e-001 -9.8352225e-001 -5.8202586e-001 -9.4052208e-001 -9.4736860e-001 -9.7070098e-001 -6.0894813e-001 -9.5861063e-001 -8.2724842e-001 -9.7206331e-001 -9.7037611e-001 -9.8480991e-001 -9.6927872e-001 -9.6351820e-001 -9.8874260e-001 -9.9985497e-001 -8.5292351e-001 -9.7563781e-001 -9.6564591e-001 -9.9198784e-001 -8.5203935e-001 -9.7936177e-001 -5.4502801e-001 -5.4720438e-001 -7.1707732e-001 -5.5681524e-001 -5.5143550e-001 -8.0269264e-001 -5.0003266e-001 -5.6976602e-001 -7.8307638e-001 -6.2303112e-001 -5.8000020e-001 -8.1047518e-001 -6.8181195e-001 -7.3904511e-001 -9.5554623e-001 -5.3077349e-001 -8.8728743e-001 -8.8799038e-001 -9.6785059e-001 -5.1411363e-001 -6.6258682e-001 -7.6287605e-001 3.0931570e-001 2.2052780e-001 -3.5568505e-002 -8.8000000e-001 -8.8000000e-001 -2.8000000e-001 -2.5333691e-001 -5.9152475e-001 -2.0731731e-001 -8.9794876e-002 -5.7915750e-001 -2.1730135e-001 -6.8257646e-001 -6.4468117e-001 -8.5501429e-001 -8.5714309e-001 -9.0576982e-001 -9.5814326e-001 -9.2817528e-001 -9.4885765e-001 -9.1831654e-001 -9.2500200e-001 -9.8628450e-001 -8.7576706e-001 -9.3520485e-001 -9.3204466e-001 -9.2364930e-001 -8.8464828e-001 -9.0131923e-001 -6.4386121e-001 -9.3517915e-001 -9.4125898e-001 -9.6181830e-001 -9.5466644e-001 -9.3071725e-001 -9.5301943e-001 -9.8653676e-001 -8.5735889e-001 -9.3914344e-001 -9.3357853e-001 -9.5725167e-001 -8.7038396e-001 -9.5042653e-001 -9.3937570e-001 -9.6849655e-001 -9.7075369e-001 -9.8422672e-001 -9.6920858e-001 -9.6096278e-001 -9.8038789e-001 -9.9916872e-001 -9.5155184e-001 -9.7737021e-001 -9.6466110e-001 -9.8129611e-001 -9.5851809e-001 -9.7656582e-001 -4.1680301e-001 -5.9655939e-001 -5.0034352e-001 -3.6161061e-001 -2.8709136e-001 -5.4159368e-001 -4.1609773e-001 -4.9868968e-001 -4.6197189e-001 -1.8324959e-001 -3.5524968e-001 -6.4457993e-001 -8.7880828e-001 -9.6886477e-001 -8.9097711e-001 -4.8166991e-001 -7.9775110e-001 -8.0892864e-001 -8.7172679e-001 -5.0467938e-001 -7.6619659e-001 -5.1331316e-001 4.0905472e-001 3.8068340e-001 3.7435573e-001 -1.0000000e+000 -9.3548387e-001 -7.9310345e-001 -3.9139669e-001 -7.6739145e-001 -3.8312749e-001 4.9431554e-001 3.0167607e-001 1.6699661e-001 -2.6295226e-001 -2.0728369e-001 -5.7267370e-001 -7.9919432e-001 -9.0558948e-001 -9.5165126e-001 -9.6526419e-001 -9.7551944e-001 -9.7297745e-001 -9.7153142e-001 -9.8179995e-001 -7.9719972e-001 -9.4694051e-001 -9.7202386e-001 -9.7607510e-001 -7.9803924e-001 -9.6683704e-001 -6.8481679e-001 -9.7830551e-001 -9.9520635e-001 -9.9540753e-001 -9.9635247e-001 -9.8405804e-001 -9.9310198e-001 -9.9952538e-001 -7.7366744e-001 -9.9412547e-001 -9.9379065e-001 -9.9494670e-001 -7.8758617e-001 -9.9454471e-001 -8.8457123e-001 -9.6332362e-001 -9.5545079e-001 -9.9297237e-001 -9.9242317e-001 -9.7600831e-001 -9.8205532e-001 -9.9611832e-001 -8.7789805e-001 -9.5411919e-001 -9.8797745e-001 -9.8816966e-001 -8.7248820e-001 -9.9142446e-001 -4.4373869e-001 -5.0288194e-001 -3.7879654e-001 -6.7748958e-001 -9.0918991e-001 -4.4373869e-001 -8.3023242e-001 -5.7835224e-001 3.9837172e-001 -7.9310345e-001 -1.7670578e-001 -4.9610605e-001 -8.0682012e-001 -5.7562296e-001 -6.1936559e-001 -6.3228609e-001 -5.9678605e-001 -5.6688271e-001 -5.7562296e-001 -9.1366450e-001 -6.8191558e-001 -2.6177842e-002 -9.0476190e-001 2.5831766e-001 3.0018902e-001 8.1301920e-002 -5.2212408e-001 -3.2033116e-001 -3.5732783e-001 -4.3774484e-001 -9.0491048e-001 -5.2212408e-001 -7.6970276e-001 -5.3155831e-001 3.9147198e-001 -1.0000000e+000 -4.3363881e-001 -1.0266016e-001 -5.3617449e-001 -7.9214558e-001 -8.2131345e-001 -7.8447374e-001 -8.5677465e-001 -9.9220136e-001 -7.9214558e-001 -9.7912709e-001 -7.5061513e-001 1.1197467e-001 -1.0000000e+000 -7.4587239e-002 -5.0945572e-001 -8.2872706e-001 7.0566817e-002 -4.8906867e-001 -8.8170939e-001 6.3032682e-001 -6.7558274e-001 3.1849899e-001 -2.3469958e-002
@alexcasalboni
Copy link
Author

alexcasalboni commented Jul 19, 2016

Note: oauth2client.client.SignedJwtAssertionCredentials has been moved (see here).

You should now use oauth2client.service_account.ServiceAccountCredentials, as follows:

credentials = ServiceAccountCredentials.from_json_keyfile_name('service_account.json', scopes=scope)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment