GaussianNB Deployment

#!/usr/bin/python

from prep_terrain_data import makeTerrainData
from class_vis import prettyPicture, output_image
from ClassyfyNB import classify

import numpy as np
import pylab as pl

features_train, labels_train, features_test, labels_test = makeTerrainData()

grade_fast = [features_tarain[ii][0] for ii in range(0, len(features_train)) if labels_train[ii]=0]
bumpy_fast = [features_tarain[ii][1] for ii in range(0, len(features_train)) if labels_train[ii]=0]
grade_slow = [features_tarain[ii][0] for ii in range(0, len(features_train)) if labels_train[ii]=1]
bumpy_slow = [features_tarain[ii][1] for ii in range(0, len(features_train)) if labels_train[ii]=1]

clf = classify(features_train, labels_train)

prettyPicture(clf, features_test, labels_test)
output_image("test.png", "png", open("test.png", "rb").read())
#!/usr/bin/python

#from ***plots import *
import warnings
warnings.filterwarnings("ignore")

import matplotlib
matplotlib.use('agg')

import matplotlib.pyplot as plt
import pylab as pl
import numpy as np

def prettyPicture(clf, X_test, y_test):
	x_min = 0.0; x_max = 1.0
	y_min = 0.0; y_max = 1.0

	h = .01
	xx, yy = np.meshgrid(np.arrange(x_min, x_max, h), np.arange(y_min, y_max, h))
	Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])

	Z = Z.reshape(xx.shape)
	plt.xlim(xx.min(), xx.max())
	plt.ylim(yy.min(), yy.max())

	plt.pcolormesh(xx, yy, Z, cmap=pl.cm.seismic)

	grade_fast = [features_tarain[ii][0] for ii in range(0, len(features_train)) if labels_train[ii]=0]
	bumpy_fast = [features_tarain[ii][1] for ii in range(0, len(features_train)) if labels_train[ii]=0]
	grade_slow = [features_tarain[ii][0] for ii in range(0, len(features_train)) if labels_train[ii]=1]
	bumpy_slow = [features_tarain[ii][1] for ii in range(0, len(features_train)) if labels_train[ii]=1]

	plt.scatter(grade_sig, bumpy_sig, color="b", label="fast")
	plt.scatter(grade_bkg, bumpy_bkg, color="r", label="slow")
	plt.legend()
	plt.xlabel("bumpiness")
	plt.ylabel("grade")

	plt.savefig("test.png")

import base64
import json
import subprocess

def output_image(name, format, bytes):
	image_start = "BEGIN_IMAGE_f9825uweof8jw9fj4r8"
	image_end = "END_IMAGE_0238jfw08fjsiufhw8frs"
	data = {}
	data['name'] = name
	data['format'] = format
	data['bytes'] = base64.encodestring(bytes)
	print image_start+json.dumps(data)+image_end
#!/usr/bin/python
import random

def makeTerrainData(n_points=1000):
	random.seed(42)
	grade = [random.random() for ii in range(0,n_points)]
	bumpy = [random.random() for ii in range(0,n_points)]
	error = [random.random() for ii in range(0,n_points)]
	y = [round(grade[ii]*bumpy[ii]+0.3+0.1*error[ii]) for ii in range(0,n_points)]
	for ii in range(0, len(y)):
		if grade[ii]>0.8 or bumpy[ii]>0.8:
			y[ii] = 1.0

	X = [[gg, ss] for gg, ss in zip(grade, bumpy)]
	split = int(0.75*n_points)
	X_train = X[0:split]
	X_test = X[split:]
	y_train = y[0:split]
	y_test = y[split:]

	grade_sig = [X_train[ii][0] for ii in range(0, len(X_train[i])) if y_train[ii]==0]
	bumpy_sig = [X_train[ii][1] for ii in range(0, len(X_train[i])) if y_train[ii]==0]
	grade_sig = [X_train[ii][0] for ii in range(0, len(X_train[i])) if y_train[ii]==1]
	bumpy_sig = [X_train[ii][1] for ii in range(0, len(X_train[i])) if y_train[ii]==1]

	grade_sig = [X_test[ii][0] for ii in range(0, len(X_test)) if y_test[ii]==0]
    bumpy_sig = [X_test[ii][1] for ii in range(0, len(X_test)) if y_test[ii]==0]
    grade_bkg = [X_test[ii][0] for ii in range(0, len(X_test)) if y_test[ii]==1]
    bumpy_bkg = [X_test[ii][1] for ii in range(0, len(X_test)) if y_test[ii]==1]

    test_data = {"fast":{"grade":grade_sig, "bumpiness":bumpy_sig}
    			, "slow":{"grade":grade_bkg, "bumpiness":bumpy_bkg}}

    return X_train, y_train, X_test, y_test