-
Notifications
You must be signed in to change notification settings - Fork 0
/
prediction.py
145 lines (111 loc) · 4.64 KB
/
prediction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import httplib2
import settings
import sys
import math
from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run
client_id = settings.client_id
client_secret = settings.client_secret
scope = {'https://www.googleapis.com/auth/devstorage.full_control',
'https://www.googleapis.com/auth/devstorage.read_only',
'https://www.googleapis.com/auth/devstorage.read_write',
'https://www.googleapis.com/auth/prediction'}
flow = OAuth2WebServerFlow(client_id, client_secret, scope)
storage = Storage("credentials.dat")
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = run(flow, storage)
http = httplib2.Http()
http = credentials.authorize(http)
service = build('prediction', 'v1.6', http=http)
class TrainedModel:
def __init__(self, project_id, model_name):
self.p = project_id
self.m = model_name
#Train a Prediction API model
def insert(self, storage_data_location=None, output_value=None, features=None):
body= {
"storageDataLocation": storage_data_location,
"id": self.m,
"trainingInstances": [
{"output": output_value,
"csvInstance": features
}
]
}
return service.trainedmodels().insert(project=self.p, body=body).execute()
#Train a Prediction API model using a dataset
def insert_dataset(self, training_data):
body= {
"id": self.m,
"trainingInstances": training_data
}
return service.trainedmodels().insert(project=self.p, body=body).execute()
#Check training status of your model
def get(self):
return service.trainedmodels().get(project=self.p, id=self.m).execute()
#Submit model id and request a prediction
def predict(self, features):
body={
"input": {
"csvInstance":[
features
]
}
}
return service.trainedmodels().predict(project=self.p, id=self.m, body=body).execute()
#List available models
def list(self):
return service.trainedmodels().list(project=self.p).execute()
#Delete a trained model
def delete(self):
return service.trainedmodels().delete(project=self.p, id=self.m).execute()
#Get analysis of the model and the data the model was trained on
def analyze(self):
return service.trainedmodels().analyze(project=self.p, id=self.m).execute()
#Add new data to a trained model
def update(self, output, features):
body= {
"output": output,
"csvInstance":[
features
]
}
return service.trainedmodels().update(project=self.p, id=self.m, body=body).execute()
class HostedModel(object):
Hosted_model_id = 414649711441
#Submit input and request an output against a hosted model
def predict(self, model_name, csv_instances):
body={
"input":{
"csvInstance": csv_instances
}
}
return service.hostedmodels().predict()
try:
threshold=float(sys.argv[2])
fin=open(sys.argv[1], 'r')
fin.close()
except:
print "Invoke command : prdiction.py <test file name> <threshold value>"
print "exiting..."
sys.exit(0)
fout=open("result.csv","w")
example = TrainedModel("spry-starlight-90710","traingSentiData")
with open(sys.argv[1], 'r') as f:
for line in f:
#print line.split('''","''')[0][1:]
predict=example.predict(line.split('''","''')[0][1:])
ll=str(predict).split("score")
negative_score=float(ll[1].split("'u")[0][5:13])
positive_score=float(ll[2].split("'u")[0][5:13])
if (math.fabs(negative_score-positive_score)<threshold):
fout.write('''"2","'''+'''negative : '''+str(negative_score)+'''","'''+'''positive : '''+str(positive_score)+'''",'''+line)
elif (negative_score < positive_score):
fout.write('''"4","'''+'''negative : '''+str(negative_score)+'''","'''+'''positive : '''+str(positive_score)+'''",'''+line)
else:
fout.write('''"0","'''+'''negative : '''+str(negative_score)+'''","'''+'''positive : '''+str(positive_score)+'''",'''+line)
print "prediction finished check the result.csv file"
fout.close()