Skip to content

Instantly share code, notes, and snippets.

@marcelcaraciolo
Created May 2, 2014 05:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcelcaraciolo/24a79acd402e7603f0b6 to your computer and use it in GitHub Desktop.
Save marcelcaraciolo/24a79acd402e7603f0b6 to your computer and use it in GitHub Desktop.
from yhat import BaseModel, Yhat
class DigitModel(BaseModel):
def require(self):
from PIL import Image
from StringIO import StringIO
import base64
def transform(self, 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")
print yh.deploy("DigitRecognizer", digit_model)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment