Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def graph_surface(fun, rect, offset=0.5, width=256, height=256):
- '''
- fun ... decizijska funkcija (Nx2)->(Nx1)
- rect ... željena domena prikaza zadana kao:
- ([x_min,y_min], [x_max,y_max])
- offset ... "nulta" vrijednost decizijske funkcije na koju
- je potrebno poravnati središte palete boja;
- tipično imamo:
- offset = 0.5 za probabilističke modele
- (npr. logistička regresija)
- offset = 0 za modele koji ne spljošćuju
- klasifikacijske mjere (npr. SVM)
- width,height ... rezolucija koordinatne mreže
- '''
- # Make linspace vector width x height resolution with rect limits
- linspace_width = np.linspace(rect[0][0], rect[1][0], width)
- linspace_height = np.linspace(rect[0][1], rect[1][1], height)
- # Make a meshgrid
- xv, yv = np.meshgrid(linspace_width, linspace_height)
- # Flatten and stack them in Nx2 shape
- x_flat = np.ndarray.flatten(xv)
- y_flat = np.ndarray.flatten(yv)
- grid_data = np.stack([x_flat, y_flat], axis=1)
- #pdb.set_trace()
- # Get point decisions width x height
- point_decision = fun(grid_data).reshape((width, height))
- # fix the range and offset
- delta = offset if offset else 0
- maxval = max(
- np.max(point_decision)-delta,
- - (np.min(point_decision)-delta))
- # draw the surface and the offset
- plt.pcolormesh(xv, yv, point_decision,
- vmin=delta-maxval, vmax=delta+maxval, cmap='jet')
- plt.colorbar()
- if offset != None:
- plt.contour(xv, yv, point_decision, colors='black', levels=[offset])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement