You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This section gets us started with displaying basic binary classification using 2D data. We first show how to display training versus testing data using [various marker styles](https://plot.ly/python/marker-style/), then demonstrate how to evaluate a kNN classifier's performance on the **test split** using a continuous color gradient to indicate the model's predicted score.
39
+
This section gets us started with displaying basic binary classification using 2D data. We first show how to display training versus testing data using [various marker styles](https://plot.ly/python/marker-style/), then demonstrate how to evaluate our classifier's performance on the **test split** using a continuous color gradient to indicate the model's predicted score.
40
+
41
+
We will use [Scikit-learn](https://scikit-learn.org/) for training our model and for loading and splitting data. Scikit-learn is a popular Machine Learning (ML) library that offers various tools for creating and training ML algorithms, feature engineering, data cleaning, and evaluating and testing models. It was designed to be accessible, and to work seamlessly with popular libraries like NumPy and Pandas.
42
+
43
+
We will train a [k-Nearest Neighbors (kNN)](https://scikit-learn.org/stable/modules/neighbors.html) classifier. First, the model records the label of each training sample. Then, whenever we give it a new sample, it will look at the `k` closest samples from the training set to find the most common label, and assign it to our new sample.
40
44
41
45
42
46
### Display training and test splits
43
47
44
48
45
-
Here, we display all the negative labels as squares, and positive labels as circles. We differentiate the training and test set by adding a dot to the center of test data.
49
+
Using Scikit-learn, we first generate synthetic data that form the shape of a moon. We then split it into a training and testing set. Finally, we display the ground truth labels using [a scatter plot](https://plotly.com/python/line-and-scatter/).
50
+
51
+
In the graph, we display all the negative labels as squares, and positive labels as circles. We differentiate the training and test set by adding a dot to the center of test data.
46
52
47
53
```python
48
54
import numpy as np
@@ -52,6 +58,7 @@ from sklearn.datasets import make_moons
52
58
from sklearn.model_selection import train_test_split
53
59
from sklearn.neighbors import KNeighborsClassifier
### Visualize predictions on test split with [`plotly.express`](https://plotly.com/python/plotly-express/)
89
+
82
90
91
+
Now, we train the kNN model on the same training data displayed in the previous graph. Then, we predict the confidence score of the model for each of the data points in the test set. We will use shapes to denote the true labels, and the color will indicate the confidence of the model for assign that score.
83
92
84
-
Now, we evaluate the model only on the test set. Notice that `px.scatter` only require 1 function call to plot both negative and positive labels, and can additionally set a continuous color scale based on the `y_score` output by our kNN model.
93
+
Notice that `px.scatter` only require 1 function call to plot both negative and positive labels, and can additionally set a continuous color scale based on the `y_score` output by our kNN model.
85
94
86
95
```python
87
96
import numpy as np
@@ -114,6 +123,56 @@ fig.show()
114
123
115
124
## Probability Estimates with `go.Contour`
116
125
126
+
Just like the previous example, we will first train our kNN model on the training set.
127
+
128
+
Instead of predicting the conference for the test set, we can predict the confidence map for the entire area that wraps around the dimensions of our dataset. To do this, we use [`np.meshgrid`](https://numpy.org/doc/stable/reference/generated/numpy.meshgrid.html) to create a grid, where the distance between each point is denoted by the `mesh_size` variable.
129
+
130
+
Then, for each of those points, we will use our model to give a confidence score, and plot it with a [contour plot](https://plotly.com/python/contour-plots/).
131
+
132
+
```python
133
+
import numpy as np
134
+
import plotly.express as px
135
+
import plotly.graph_objects as go
136
+
from sklearn.datasets import make_moons
137
+
from sklearn.model_selection import train_test_split
138
+
from sklearn.neighbors import KNeighborsClassifier
Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1]
159
+
Z = Z.reshape(xx.shape)
160
+
161
+
162
+
# Plot the figure
163
+
fig = go.Figure(data=[
164
+
go.Contour(
165
+
x=xrange,
166
+
y=yrange,
167
+
z=Z,
168
+
colorscale='RdBu'
169
+
)
170
+
])
171
+
fig.show()
172
+
```
173
+
174
+
Now, let's try to combine our `go.Contour` plot with the first scatter plot of our data points, so that we can visually compare the confidence of our model with the true labels.
175
+
117
176
```python
118
177
import numpy as np
119
178
import plotly.express as px
@@ -178,9 +237,9 @@ fig.add_trace(
178
237
fig.show()
179
238
```
180
239
181
-
## Multi-class prediction confidence with `go.Heatmap`
240
+
## Multi-class prediction confidence with [`go.Heatmap`](https://plotly.com/python/heatmaps/)
182
241
183
-
It is also possible to visualize the prediction confidence of the model using `go.Heatmap`. In this example, you can see how to compute how confident the model is about its prediction at every point in the 2D grid. Here, we define the confidence as the difference between the highest score and the score of the other classes summed, at a certain point.
242
+
It is also possible to visualize the prediction confidence of the model using [heatmaps](https://plotly.com/python/heatmaps/). In this example, you can see how to compute how confident the model is about its prediction at every point in the 2D grid. Here, we define the confidence as the difference between the highest score and the score of the other classes summed, at a certain point.
0 commit comments