Plotting a decision boundary python ( give a good idea of how contourf matplotlib function works ) -
this can considered duplicate of thread, it's meant granular level explanation intended question.
in machine learning algorithms (let's consider perceptron), having set of data points, spans 2 features. input features of form
## assume classifier has been trained simplicity x = [[a1,b1] , [a2,b2] , .... , [an,bn]] x1_min, x1_max = x[:, 0].min() - 1, x[:, 0].max() + 1 x2_min, x2_max = x[:, 1].min() - 1, x[:, 1].max() + 1 xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution), np.arange(x2_min, x2_max, resolution)) z = classifier.net_input(np.array([xx1.ravel(), xx2.ravel()]).t) ### here z set of prediction has values +1 or -1 , data points in x z = z.reshape(xx1.shape). # plot class samples idx, cl in enumerate(np.unique(y)): plt.scatter(x=x[y == cl, 0], y=x[y == cl, 1], alpha=0.8, c=cmap(idx), marker=markers[idx], label=cl) plt.show()
as of fine. real problem following function or how works, when passing data points xx1
, xx2
along predicted value z
(which nothing bunch of +1 , -1). not familiar contours. can 1 explain how getting decision boundary through contourf
function?
plt.contourf(xx1, xx2, z, alpha=0.4, cmap=cmap)
this generated decision boundary separating 2 classes. can explain happening inside countourf
function or how derive decision boundary xx1
, xx2
, z
(bunch of +1 , -1)?
in
xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution), np.arange(x2_min, x2_max, resolution))
you create grid spans range of data , has given resolution. grid has no connection actual data points, grid laid on input space.
then, in
z = classifier.net_input(np.array([xx1.ravel(), xx2.ravel()]).t) z = z.reshape(xx1.shape)
you let classifier compute output each point of grid. gives +1 or -1 each point on grid , reshapes output array matches shape of gird.
finally, there contourf
function. here documentation. contourf
takes 3-d surface specified grid (i.e. bunch of x-y points along z-value of these points) , plots surface displaying contours. contour line surface has same value. since z
array contains +1s , -1s, real contour @ points "between" +1s , -1s, i.e. decision boundary.
in nutshell, decision boundary extracted finely sampling feature space , plotting output value each sample. boundary comes out implicitly output values vary across feature space.
Comments
Post a Comment