Advertisement
Guest User

surface

a guest
Mar 21st, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. def graph_surface(fun, rect, offset=0.5, width=256, height=256):
  2. '''
  3. fun ... decizijska funkcija (Nx2)->(Nx1)
  4. rect ... željena domena prikaza zadana kao:
  5. ([x_min,y_min], [x_max,y_max])
  6. offset ... "nulta" vrijednost decizijske funkcije na koju
  7. je potrebno poravnati središte palete boja;
  8. tipično imamo:
  9. offset = 0.5 za probabilističke modele
  10. (npr. logistička regresija)
  11. offset = 0 za modele koji ne spljošćuju
  12. klasifikacijske mjere (npr. SVM)
  13. width,height ... rezolucija koordinatne mreže
  14. '''
  15.  
  16. # Make linspace vector width x height resolution with rect limits
  17. linspace_width = np.linspace(rect[0][0], rect[1][0], width)
  18. linspace_height = np.linspace(rect[0][1], rect[1][1], height)
  19.  
  20. # Make a meshgrid
  21. xv, yv = np.meshgrid(linspace_width, linspace_height)
  22.  
  23. # Flatten and stack them in Nx2 shape
  24. x_flat = np.ndarray.flatten(xv)
  25. y_flat = np.ndarray.flatten(yv)
  26. grid_data = np.stack([x_flat, y_flat], axis=1)
  27.  
  28. #pdb.set_trace()
  29. # Get point decisions width x height
  30. point_decision = fun(grid_data).reshape((width, height))
  31.  
  32. # fix the range and offset
  33. delta = offset if offset else 0
  34. maxval = max(
  35. np.max(point_decision)-delta,
  36. - (np.min(point_decision)-delta))
  37.  
  38. # draw the surface and the offset
  39. plt.pcolormesh(xv, yv, point_decision,
  40. vmin=delta-maxval, vmax=delta+maxval, cmap='jet')
  41. plt.colorbar()
  42.  
  43. if offset != None:
  44. plt.contour(xv, yv, point_decision, colors='black', levels=[offset])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement