python - understand how this lambda function works -
i thought understood how lambda functions work, though don't use them myself. lambda below this tutorial totally stumps me:
import matplotlib.pyplot plt import numpy np import sklearn import sklearn.datasets import sklearn.linear_model import matplotlib
that easy. more:
# generate dataset , plot np.random.seed(0) x, y = sklearn.datasets.make_moons(200, noise=0.20) plt.scatter(x[:,0], x[:,1], s=40, c=y, cmap=plt.cm.spectral) clf = sklearn.linear_model.logisticregressioncv() clf.fit(x, y) # helper function plot decision boundary. # if don't understand function don't worry, generates contour plot below. def plot_decision_boundary(pred_func): # set min , max values , give padding x_min, x_max = x[:, 0].min() - .5, x[:, 0].max() + .5 y_min, y_max = x[:, 1].min() - .5, x[:, 1].max() + .5 h = 0.01 # generate grid of points distance h between them xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) # predict function value whole gid z = pred_func(np.c_[xx.ravel(), yy.ravel()]) z = z.reshape(xx.shape) # plot contour , training examples plt.contourf(xx, yy, z, cmap=plt.cm.spectral) plt.scatter(x[:, 0], x[:, 1], c=y, cmap=plt.cm.spectral)
now line don't understand:
plot_decision_boundary(lambda x: clf.predict(x))
i've read many times how lambdas work, don't how x
here passing correct values before. how x
mapped relevant values?
x
concatenated numpy object pass in here:
z = pred_func(np.c_[xx.ravel(), yy.ravel()])
pred_func
argument plot_decision_boundary()
; calling call function object defined lambda. above line translates to:
clf.predict(np.c_[xx.ravel(), yy.ravel()])
Comments
Post a Comment