prettyPicture.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/python
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. import pylab as pl
  5. def prettyPicture(clf, X_test, y_test):
  6. x_min = 0.0; x_max = 1.0
  7. y_min = 0.0; y_max = 1.0
  8. # Plot the decision boundary. For that, we will assign a color to each
  9. # point in the mesh [x_min, m_max]x[y_min, y_max].
  10. h = .01 # step size in the mesh
  11. xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
  12. Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
  13. # Put the result into a color plot
  14. Z = Z.reshape(xx.shape)
  15. plt.xlim(xx.min(), xx.max())
  16. plt.ylim(yy.min(), yy.max())
  17. plt.pcolormesh(xx, yy, Z, cmap=pl.cm.seismic)
  18. # Plot also the test points
  19. grade_sig = [X_test[ii][0] for ii in range(0, len(X_test)) if y_test[ii]==0]
  20. bumpy_sig = [X_test[ii][1] for ii in range(0, len(X_test)) if y_test[ii]==0]
  21. grade_bkg = [X_test[ii][0] for ii in range(0, len(X_test)) if y_test[ii]==1]
  22. bumpy_bkg = [X_test[ii][1] for ii in range(0, len(X_test)) if y_test[ii]==1]
  23. plt.scatter(grade_sig, bumpy_sig, color = "b", label="fast")
  24. plt.scatter(grade_bkg, bumpy_bkg, color = "r", label="slow")
  25. plt.legend()
  26. plt.xlabel("bumpiness")
  27. plt.ylabel("grade")
  28. #plt.savefig("test.png")
  29. import base64
  30. import json
  31. def output_image(name, format, bytes):
  32. image_start = "BEGIN_IMAGE_f9825uweof8jw9fj4r8"
  33. image_end = "END_IMAGE_0238jfw08fjsiufhw8frs"
  34. data = {}
  35. data['name'] = name
  36. data['format'] = format
  37. data['bytes'] = base64.encodestring(bytes)
  38. print(image_start+json.dumps(data)+image_end)