{"id":3763,"date":"2020-08-13T19:00:23","date_gmt":"2020-08-13T19:00:23","guid":{"rendered":"https:\/\/www.aiproblog.com\/index.php\/2020\/08\/13\/plot-a-decision-surface-for-machine-learning-algorithms-in-python\/"},"modified":"2020-08-13T19:00:23","modified_gmt":"2020-08-13T19:00:23","slug":"plot-a-decision-surface-for-machine-learning-algorithms-in-python","status":"publish","type":"post","link":"https:\/\/www.aiproblog.com\/index.php\/2020\/08\/13\/plot-a-decision-surface-for-machine-learning-algorithms-in-python\/","title":{"rendered":"Plot a Decision Surface for Machine Learning Algorithms in Python"},"content":{"rendered":"<p>Author: Jason Brownlee<\/p>\n<div>\n<p>Classification algorithms learn how to assign class labels to examples, although their decisions can appear opaque.<\/p>\n<p>A popular diagnostic for understanding the decisions made by a classification algorithm is the <strong>decision surface<\/strong>. This is a plot that shows how a fit machine learning algorithm predicts a coarse grid across the input feature space.<\/p>\n<p>A decision surface plot is a powerful tool for understanding how a given model &ldquo;<em>sees<\/em>&rdquo; the prediction task and how it has decided to divide the input feature space by class label.<\/p>\n<p>In this tutorial, you will discover how to plot a decision surface for a classification machine learning algorithm.<\/p>\n<p>After completing this tutorial, you will know:<\/p>\n<ul>\n<li>Decision surface is a diagnostic tool for understanding how a classification algorithm divides up the feature space.<\/li>\n<li>How to plot a decision surface for using crisp class labels for a machine learning algorithm.<\/li>\n<li>How to plot and interpret a decision surface using predicted probabilities.<\/li>\n<\/ul>\n<p>Let&rsquo;s get started.<\/p>\n<div id=\"attachment_10553\" style=\"width: 809px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" aria-describedby=\"caption-attachment-10553\" loading=\"lazy\" class=\"size-full wp-image-10553\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2020\/07\/Plot-a-Decision-Surface-for-Machine-Learning-Algorithms-in-Python.jpg\" alt=\"Plot a Decision Surface for Machine Learning Algorithms in Python\" width=\"799\" height=\"409\" srcset=\"http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/07\/Plot-a-Decision-Surface-for-Machine-Learning-Algorithms-in-Python.jpg 799w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/07\/Plot-a-Decision-Surface-for-Machine-Learning-Algorithms-in-Python-300x154.jpg 300w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/07\/Plot-a-Decision-Surface-for-Machine-Learning-Algorithms-in-Python-768x393.jpg 768w\" sizes=\"(max-width: 799px) 100vw, 799px\"><\/p>\n<p id=\"caption-attachment-10553\" class=\"wp-caption-text\">Plot a Decision Surface for Machine Learning Algorithms in Python<br \/>Photo by <a href=\"https:\/\/flickr.com\/photos\/diversey\/15398123174\/\">Tony Webster<\/a>, some rights reserved.<\/p>\n<\/div>\n<h2>Tutorial Overview<\/h2>\n<p>This tutorial is divided into three parts; they are:<\/p>\n<ol>\n<li>Decision Surface<\/li>\n<li>Dataset and Model<\/li>\n<li>Plot a Decision Surface<\/li>\n<\/ol>\n<h2>Decision Surface<\/h2>\n<p>Classification machine learning algorithms learn to assign labels to input examples.<\/p>\n<p>Consider numeric input features for the classification task defining a continuous input feature space.<\/p>\n<p>We can think of each input feature defining an axis or dimension on a feature space. Two input features would define a feature space that is a plane, with dots representing input coordinates in the input space. If there were three input variables, the feature space would be a three-dimensional volume.<\/p>\n<p>Each point in the space can be assigned a class label. In terms of a two-dimensional feature space, we can think of each point on the planing having a different color, according to their assigned class.<\/p>\n<p>The goal of a classification algorithm is to learn how to divide up the feature space such that labels are assigned correctly to points in the feature space, or at least, as correctly as is possible.<\/p>\n<p>This is a useful geometric understanding of classification predictive modeling. We can take it one step further.<\/p>\n<p>Once a classification machine learning algorithm divides a feature space, we can then classify each point in the feature space, on some arbitrary grid, to get an idea of how exactly the algorithm chose to divide up the feature space.<\/p>\n<p>This is called a <strong>decision surface<\/strong> or <strong>decision boundary<\/strong>, and it provides a diagnostic tool for understanding a model on a classification predictive modeling task.<\/p>\n<p>Although the notion of a &ldquo;<em>surface<\/em>&rdquo; suggests a two-dimensional feature space, the method can be used with feature spaces with more than two dimensions, where a surface is created for each pair of input features.<\/p>\n<p>Now that we are familiar with what a decision surface is, next, let&rsquo;s define a dataset and model for which we later explore the decision surface.<\/p>\n<h2>Dataset and Model<\/h2>\n<p>In this section, we will define a classification task and predictive model to learn the task.<\/p>\n<h3>Synthetic Classification Dataset<\/h3>\n<p>We can use the <a href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.datasets.make_blobs.html\">make_blobs() scikit-learn function<\/a> to define a classification task with a two-dimensional class numerical feature space and each point assigned one of two class labels, e.g. a binary classification task.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# generate dataset\r\nX, y = make_blobs(n_samples=1000, centers=2, n_features=2, random_state=1, cluster_std=3)<\/pre>\n<p>Once defined, we can then create a scatter plot of the feature space with the first feature defining the x-axis, the second feature defining the y axis, and each sample represented as a point in the feature space.<\/p>\n<p>We can then color points in the scatter plot according to their class label as either 0 or 1.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# create scatter plot for samples from each class\r\nfor class_value in range(2):\r\n\t# get row indexes for samples with this class\r\n\trow_ix = where(y == class_value)\r\n\t# create scatter of these samples\r\n\tpyplot.scatter(X[row_ix, 0], X[row_ix, 1])\r\n# show the plot\r\npyplot.show()<\/pre>\n<p>Tying this together, the complete example of defining and plotting a synthetic classification dataset is listed below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># generate binary classification dataset and plot\r\nfrom numpy import where\r\nfrom matplotlib import pyplot\r\nfrom sklearn.datasets import make_blobs\r\n# generate dataset\r\nX, y = make_blobs(n_samples=1000, centers=2, n_features=2, random_state=1, cluster_std=3)\r\n# create scatter plot for samples from each class\r\nfor class_value in range(2):\r\n\t# get row indexes for samples with this class\r\n\trow_ix = where(y == class_value)\r\n\t# create scatter of these samples\r\n\tpyplot.scatter(X[row_ix, 0], X[row_ix, 1])\r\n# show the plot\r\npyplot.show()<\/pre>\n<p>Running the example creates the dataset, then plots the dataset as a scatter plot with points colored by class label.<\/p>\n<p>We can see a clear separation between examples from the two classes and we can imagine how a machine learning model might draw a line to separate the two classes, e.g. perhaps a diagonal line right through the middle of the two groups.<\/p>\n<div id=\"attachment_10550\" style=\"width: 1290px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" aria-describedby=\"caption-attachment-10550\" loading=\"lazy\" class=\"size-full wp-image-10550\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2020\/03\/Scatter-Plot-of-Binary-Classification-Dataset-With-2D-Feature-Space.png\" alt=\"Scatter Plot of Binary Classification Dataset With 2D Feature Space\" width=\"1280\" height=\"960\" srcset=\"http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/03\/Scatter-Plot-of-Binary-Classification-Dataset-With-2D-Feature-Space.png 1280w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/03\/Scatter-Plot-of-Binary-Classification-Dataset-With-2D-Feature-Space-300x225.png 300w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/03\/Scatter-Plot-of-Binary-Classification-Dataset-With-2D-Feature-Space-1024x768.png 1024w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/03\/Scatter-Plot-of-Binary-Classification-Dataset-With-2D-Feature-Space-768x576.png 768w\" sizes=\"(max-width: 1280px) 100vw, 1280px\"><\/p>\n<p id=\"caption-attachment-10550\" class=\"wp-caption-text\">Scatter Plot of Binary Classification Dataset With 2D Feature Space<\/p>\n<\/div>\n<h3>Fit Classification Predictive Model<\/h3>\n<p>We can now fit a model on our dataset.<\/p>\n<p>In this case, we will fit a logistic regression algorithm because we can predict both crisp class labels and probabilities, both of which we can use in our decision surface.<\/p>\n<p>We can define the model, then fit it on the training dataset.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# define the model\r\nmodel = LogisticRegression()\r\n# fit the model\r\nmodel.fit(X, y)<\/pre>\n<p>Once defined, we can use the model to make a prediction for the training dataset to get an idea of how well it learned to divide the feature space of the training dataset and assign labels.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# make predictions\r\nyhat = model.predict(X)<\/pre>\n<p>The predictions can be evaluated using classification accuracy.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# evaluate the predictions\r\nacc = accuracy_score(y, yhat)\r\nprint('Accuracy: %.3f' % acc)<\/pre>\n<p>Tying this together, the complete example of fitting and evaluating a model on the synthetic binary classification dataset is listed below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># example of fitting and evaluating a model on the classification dataset\r\nfrom sklearn.datasets import make_blobs\r\nfrom sklearn.linear_model import LogisticRegression\r\nfrom sklearn.metrics import accuracy_score\r\n# generate dataset\r\nX, y = make_blobs(n_samples=1000, centers=2, n_features=2, random_state=1, cluster_std=3)\r\n# define the model\r\nmodel = LogisticRegression()\r\n# fit the model\r\nmodel.fit(X, y)\r\n# make predictions\r\nyhat = model.predict(X)\r\n# evaluate the predictions\r\nacc = accuracy_score(y, yhat)\r\nprint('Accuracy: %.3f' % acc)<\/pre>\n<p>Running the example fits the model and makes a prediction for each example.<\/p>\n<p>Your specific results may vary given the stochastic nature of the learning algorithm. Try running the example a few times.<\/p>\n<p>In this case, we can see that the model achieved a performance of about 97.2 percent.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">Accuracy: 0.972<\/pre>\n<p>Now that we have a dataset and model, let&rsquo;s explore how we can develop a decision surface.<\/p>\n<h2>Plot a Decision Surface<\/h2>\n<p>We can create a decision surface by fitting a model on the training dataset, then using the model to make predictions for a grid of values across the input domain.<\/p>\n<p>Once we have the grid of predictions, we can plot the values and their class label.<\/p>\n<p>A scatter plot could be used if a fine enough grid was taken. A better approach is to use a contour plot that can interpolate the colors between the points.<\/p>\n<p>The <a href=\"https:\/\/matplotlib.org\/api\/_as_gen\/matplotlib.pyplot.contourf.html\">contourf() Matplotlib function<\/a> can be used.<\/p>\n<p>This requires a few steps.<\/p>\n<p>First, we need to define a grid of points across the feature space.<\/p>\n<p>To do this, we can find the minimum and maximum values for each feature and expand the grid one step beyond that to ensure the whole feature space is covered.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# define bounds of the domain\r\nmin1, max1 = X[:, 0].min()-1, X[:, 0].max()+1\r\nmin2, max2 = X[:, 1].min()-1, X[:, 1].max()+1<\/pre>\n<p>We can then create a uniform sample across each dimension using the <a href=\"https:\/\/docs.scipy.org\/doc\/numpy\/reference\/generated\/numpy.arange.html\">arange() function<\/a> at a chosen resolution. We will use a resolution of 0.1 in this case.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# define the x and y scale\r\nx1grid = arange(min1, max1, 0.1)\r\nx2grid = arange(min2, max2, 0.1)<\/pre>\n<p>Now we need to turn this into a grid.<\/p>\n<p>We can use the <a href=\"https:\/\/docs.scipy.org\/doc\/numpy\/reference\/generated\/numpy.meshgrid.html\">meshgrid() NumPy function<\/a> to create a grid from these two vectors.<\/p>\n<p>If the first feature x1 is our x-axis of the feature space, then we need one row of x1 values of the grid for each point on the y-axis.<\/p>\n<p>Similarly, if we take x2 as our y-axis of the feature space, then we need one column of x2 values of the grid for each point on the x-axis.<\/p>\n<p>The <em>meshgrid()<\/em> function will do this for us, duplicating the rows and columns for us as needed. It returns two grids for the two input vectors. The first grid of x-values and the second of y-values, organized in an appropriately sized grid of rows and columns across the feature space.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# create all of the lines and rows of the grid\r\nxx, yy = meshgrid(x1grid, x2grid)<\/pre>\n<p>We then need to flatten out the grid to create samples that we can feed into the model and make a prediction.<\/p>\n<p>To do this, first, we flatten each grid into a vector.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# flatten each grid to a vector\r\nr1, r2 = xx.flatten(), yy.flatten()\r\nr1, r2 = r1.reshape((len(r1), 1)), r2.reshape((len(r2), 1))<\/pre>\n<p>Then we stack the vectors side by side as columns in an input dataset, e.g. like our original training dataset, but at a much higher resolution.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# horizontal stack vectors to create x1,x2 input for the model\r\ngrid = hstack((r1,r2))<\/pre>\n<p>We can then feed this into our model and get a prediction for each point in the grid.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# make predictions for the grid\r\nyhat = model.predict(grid)\r\n# reshape the predictions back into a grid<\/pre>\n<p>So far, so good.<\/p>\n<p>We have a grid of values across the feature space and the class labels as predicted by our model.<\/p>\n<p>Next, we need to plot the grid of values as a contour plot.<\/p>\n<p>The <a href=\"https:\/\/matplotlib.org\/api\/_as_gen\/matplotlib.pyplot.contourf.html\">contourf() function<\/a> takes separate grids for each axis, just like what was returned from our prior call to <em>meshgrid()<\/em>. Great!<\/p>\n<p>So we can use <em>xx<\/em> and <em>yy<\/em> that we prepared earlier and simply reshape the predictions (<em>yhat<\/em>) from the model to have the same shape.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# reshape the predictions back into a grid\r\nzz = yhat.reshape(xx.shape)<\/pre>\n<p>We then plot the decision surface with a two-color colormap.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# plot the grid of x, y and z values as a surface\r\npyplot.contourf(xx, yy, zz, cmap='Paired')<\/pre>\n<p>We can then plot the actual points of the dataset over the top to see how well they were separated by the logistic regression decision surface.<\/p>\n<p>The complete example of plotting a decision surface for a logistic regression model on our synthetic binary classification dataset is listed below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># decision surface for logistic regression on a binary classification dataset\r\nfrom numpy import where\r\nfrom numpy import meshgrid\r\nfrom numpy import arange\r\nfrom numpy import hstack\r\nfrom sklearn.datasets import make_blobs\r\nfrom sklearn.linear_model import LogisticRegression\r\nfrom matplotlib import pyplot\r\n# generate dataset\r\nX, y = make_blobs(n_samples=1000, centers=2, n_features=2, random_state=1, cluster_std=3)\r\n# define bounds of the domain\r\nmin1, max1 = X[:, 0].min()-1, X[:, 0].max()+1\r\nmin2, max2 = X[:, 1].min()-1, X[:, 1].max()+1\r\n# define the x and y scale\r\nx1grid = arange(min1, max1, 0.1)\r\nx2grid = arange(min2, max2, 0.1)\r\n# create all of the lines and rows of the grid\r\nxx, yy = meshgrid(x1grid, x2grid)\r\n# flatten each grid to a vector\r\nr1, r2 = xx.flatten(), yy.flatten()\r\nr1, r2 = r1.reshape((len(r1), 1)), r2.reshape((len(r2), 1))\r\n# horizontal stack vectors to create x1,x2 input for the model\r\ngrid = hstack((r1,r2))\r\n# define the model\r\nmodel = LogisticRegression()\r\n# fit the model\r\nmodel.fit(X, y)\r\n# make predictions for the grid\r\nyhat = model.predict(grid)\r\n# reshape the predictions back into a grid\r\nzz = yhat.reshape(xx.shape)\r\n# plot the grid of x, y and z values as a surface\r\npyplot.contourf(xx, yy, zz, cmap='Paired')\r\n# create scatter plot for samples from each class\r\nfor class_value in range(2):\r\n\t# get row indexes for samples with this class\r\n\trow_ix = where(y == class_value)\r\n\t# create scatter of these samples\r\n\tpyplot.scatter(X[row_ix, 0], X[row_ix, 1], cmap='Paired')\r\n# show the plot\r\npyplot.show()<\/pre>\n<p>Running the example fits the model and uses it to predict outcomes for the grid of values across the feature space and plots the result as a contour plot.<\/p>\n<p>We can see, as we might have suspected, logistic regression divides the feature space using a straight line. It is a linear model, after all; this is all it can do.<\/p>\n<p>Creating a decision surface is almost like magic. It gives immediate and meaningful insight into how the model has learned the task.<\/p>\n<p><strong>Try it with different algorithms, like an SVM or decision tree.<\/strong><br \/>\nPost your resulting maps as links in the comments below!<\/p>\n<div id=\"attachment_10551\" style=\"width: 1290px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" aria-describedby=\"caption-attachment-10551\" loading=\"lazy\" class=\"size-full wp-image-10551\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2020\/03\/Decision-Surface-for-Logistic-Regression-on-a-Binary-Classification-Task.png\" alt=\"Decision Surface for Logistic Regression on a Binary Classification Task\" width=\"1280\" height=\"960\" srcset=\"http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/03\/Decision-Surface-for-Logistic-Regression-on-a-Binary-Classification-Task.png 1280w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/03\/Decision-Surface-for-Logistic-Regression-on-a-Binary-Classification-Task-300x225.png 300w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/03\/Decision-Surface-for-Logistic-Regression-on-a-Binary-Classification-Task-1024x768.png 1024w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/03\/Decision-Surface-for-Logistic-Regression-on-a-Binary-Classification-Task-768x576.png 768w\" sizes=\"(max-width: 1280px) 100vw, 1280px\"><\/p>\n<p id=\"caption-attachment-10551\" class=\"wp-caption-text\">Decision Surface for Logistic Regression on a Binary Classification Task<\/p>\n<\/div>\n<p>We can add more depth to the decision surface by using the model to predict probabilities instead of class labels.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# make predictions for the grid\r\nyhat = model.predict_proba(grid)\r\n# keep just the probabilities for class 0\r\nyhat = yhat[:, 0]<\/pre>\n<p>When plotted, we can see how confident or likely it is that each point in the feature space belongs to each of the class labels, as seen by the model.<\/p>\n<p>We can use a <a href=\"https:\/\/matplotlib.org\/tutorials\/colors\/colormaps.html\">different color map<\/a> that has gradations, and show a legend so we can interpret the colors.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# plot the grid of x, y and z values as a surface\r\nc = pyplot.contourf(xx, yy, zz, cmap='RdBu')\r\n# add a legend, called a color bar\r\npyplot.colorbar(c)<\/pre>\n<p>The complete example of creating a decision surface using probabilities is listed below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># probability decision surface for logistic regression on a binary classification dataset\r\nfrom numpy import where\r\nfrom numpy import meshgrid\r\nfrom numpy import arange\r\nfrom numpy import hstack\r\nfrom sklearn.datasets import make_blobs\r\nfrom sklearn.linear_model import LogisticRegression\r\nfrom matplotlib import pyplot\r\n# generate dataset\r\nX, y = make_blobs(n_samples=1000, centers=2, n_features=2, random_state=1, cluster_std=3)\r\n# define bounds of the domain\r\nmin1, max1 = X[:, 0].min()-1, X[:, 0].max()+1\r\nmin2, max2 = X[:, 1].min()-1, X[:, 1].max()+1\r\n# define the x and y scale\r\nx1grid = arange(min1, max1, 0.1)\r\nx2grid = arange(min2, max2, 0.1)\r\n# create all of the lines and rows of the grid\r\nxx, yy = meshgrid(x1grid, x2grid)\r\n# flatten each grid to a vector\r\nr1, r2 = xx.flatten(), yy.flatten()\r\nr1, r2 = r1.reshape((len(r1), 1)), r2.reshape((len(r2), 1))\r\n# horizontal stack vectors to create x1,x2 input for the model\r\ngrid = hstack((r1,r2))\r\n# define the model\r\nmodel = LogisticRegression()\r\n# fit the model\r\nmodel.fit(X, y)\r\n# make predictions for the grid\r\nyhat = model.predict_proba(grid)\r\n# keep just the probabilities for class 0\r\nyhat = yhat[:, 0]\r\n# reshape the predictions back into a grid\r\nzz = yhat.reshape(xx.shape)\r\n# plot the grid of x, y and z values as a surface\r\nc = pyplot.contourf(xx, yy, zz, cmap='RdBu')\r\n# add a legend, called a color bar\r\npyplot.colorbar(c)\r\n# create scatter plot for samples from each class\r\nfor class_value in range(2):\r\n\t# get row indexes for samples with this class\r\n\trow_ix = where(y == class_value)\r\n\t# create scatter of these samples\r\n\tpyplot.scatter(X[row_ix, 0], X[row_ix, 1], cmap='Paired')\r\n# show the plot\r\npyplot.show()<\/pre>\n<p>Running the example predicts the probability of class membership for each point on the grid across the feature space and plots the result.<\/p>\n<p>Here, we can see that the model is unsure (lighter colors) around the middle of the domain, given the sampling noise in that area of the feature space. We can also see that the model is very confident (full colors) in the bottom-left and top-right halves of the domain.<\/p>\n<p>Together, the crisp class and probability decision surfaces are powerful diagnostic tools for understanding your model and how it divides the feature space for your predictive modeling task.<\/p>\n<div id=\"attachment_10552\" style=\"width: 1290px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" aria-describedby=\"caption-attachment-10552\" loading=\"lazy\" class=\"size-full wp-image-10552\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2020\/03\/Probability-Decision-Surface-for-Logistic-Regression-on-a-Binary-Classification-Task.png\" alt=\"Probability Decision Surface for Logistic Regression on a Binary Classification Task\" width=\"1280\" height=\"960\" srcset=\"http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/03\/Probability-Decision-Surface-for-Logistic-Regression-on-a-Binary-Classification-Task.png 1280w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/03\/Probability-Decision-Surface-for-Logistic-Regression-on-a-Binary-Classification-Task-300x225.png 300w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/03\/Probability-Decision-Surface-for-Logistic-Regression-on-a-Binary-Classification-Task-1024x768.png 1024w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/03\/Probability-Decision-Surface-for-Logistic-Regression-on-a-Binary-Classification-Task-768x576.png 768w\" sizes=\"(max-width: 1280px) 100vw, 1280px\"><\/p>\n<p id=\"caption-attachment-10552\" class=\"wp-caption-text\">Probability Decision Surface for Logistic Regression on a Binary Classification Task<\/p>\n<\/div>\n<h2>Further Reading<\/h2>\n<p>This section provides more resources on the topic if you are looking to go deeper.<\/p>\n<ul>\n<li><a href=\"https:\/\/matplotlib.org\/api\/_as_gen\/matplotlib.pyplot.contourf.html\">matplotlib.pyplot.contourf API<\/a>.<\/li>\n<li><a href=\"https:\/\/matplotlib.org\/tutorials\/colors\/colormaps.html\">Matplotlib Colormaps<\/a><\/li>\n<li><a href=\"https:\/\/docs.scipy.org\/doc\/numpy\/reference\/generated\/numpy.meshgrid.html\">numpy.meshgrid API<\/a>.<\/li>\n<li><a href=\"https:\/\/scikit-learn.org\/stable\/auto_examples\/tree\/plot_iris_dtc.html\">Plot the decision surface of a decision tree on the iris dataset, sklearn example<\/a>.<\/li>\n<\/ul>\n<h2>Summary<\/h2>\n<p>In this tutorial, you discovered how to plot a decision surface for a classification machine learning algorithm.<\/p>\n<p>Specifically, you learned:<\/p>\n<ul>\n<li>Decision surface is a diagnostic tool for understanding how a classification algorithm divides up the feature space.<\/li>\n<li>How to plot a decision surface for using crisp class labels for a machine learning algorithm.<\/li>\n<li>How to plot and interpret a decision surface using predicted probabilities.<\/li>\n<\/ul>\n<p><strong>Do you have any questions?<\/strong><br \/>\nAsk your questions in the comments below and I will do my best to answer.<\/p>\n<p>The post <a rel=\"nofollow\" href=\"https:\/\/machinelearningmastery.com\/plot-a-decision-surface-for-machine-learning\/\">Plot a Decision Surface for Machine Learning Algorithms in Python<\/a> appeared first on <a rel=\"nofollow\" href=\"https:\/\/machinelearningmastery.com\/\">Machine Learning Mastery<\/a>.<\/p>\n<\/div>\n<p><a href=\"https:\/\/machinelearningmastery.com\/plot-a-decision-surface-for-machine-learning\/\">Go to Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Author: Jason Brownlee Classification algorithms learn how to assign class labels to examples, although their decisions can appear opaque. A popular diagnostic for understanding the [&hellip;] <span class=\"read-more-link\"><a class=\"read-more\" href=\"https:\/\/www.aiproblog.com\/index.php\/2020\/08\/13\/plot-a-decision-surface-for-machine-learning-algorithms-in-python\/\">Read More<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":3764,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[24],"tags":[],"_links":{"self":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/posts\/3763"}],"collection":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/comments?post=3763"}],"version-history":[{"count":0,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/posts\/3763\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/media\/3764"}],"wp:attachment":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/media?parent=3763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/categories?post=3763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/tags?post=3763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}