Visualizing conditional probabilities

By referring to the following steps, you will be able to visualize conditional probabilities:

  1. For this, we will slightly modify the plot function from the previous example. We start out by creating a mesh grid between (x_min, x_max) and (y_min, y_max):
In [18]: def plot_proba(model, X_test, y_test):
... # create a mesh to plot in
... h = 0.02 # step size in mesh
... x_min, x_max = X_test[:, 0].min() - 1, X_test[:, 0].max() + 1
... y_min, y_max = X_test[:, 1].min() - 1, X_test[:, 1].max() + 1
... xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
... np.arange(y_min, y_max, h))
  1. Then, we flatten xx and yy and add them column-wise to the feature matrix, X_hypo:

... X_hypo = np.column_stack((xx.ravel().astype(np.float32),
... yy.ravel().astype(np.float32)))
  1. If we want to make this function work with both OpenCV and scikit-learn, we need to implement a switch for predictProb (in the case of OpenCV) and predict_proba (in the case of scikit-learn). For this, we check whether model has a method called predictProb. If the method exists, we can call it; otherwise, we assume we're dealing with a model from scikit-learn:
...          if hasattr(model, 'predictProb'):
... _, _, y_proba = model.predictProb(X_hypo)
... else:
... y_proba = model.predict_proba(X_hypo)
  1. Like in In [16], which we saw earlier, y_proba will be a 2D matrix containing, for each data point, the probability of the data belonging to class 0 (in y_proba[:, 0]) and to class 1 (in y_proba[:, 1]). An easy way to convert these two values into a color that the contour function can understand is to simply take the difference of the two probability values:
...          zz = y_proba[:, 1] - y_proba[:, 0]
... zz = zz.reshape(xx.shape)
  1. The last step is to plot X_test as a scatter plot on top of the colored mesh grid:
... plt.contourf(xx, yy, zz, cmap=plt.cm.coolwarm, alpha=0.8)
... plt.scatter(X_test[:, 0], X_test[:, 1], c=y_test, s=200)
  1. Now, we are ready to call the function:
In [19]: plot_proba(model_naive, X, y)

The result looks like this:

The preceding screenshot shows conditional probabilities of a Naive Bayes classifier.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.131.10