Skip to content

Commit 3479995

Browse files
Joseph Damibanicolaskruchten
Joseph Damiba
authored andcommitted
add new section
1 parent 19653b2 commit 3479995

File tree

6 files changed

+300
-1
lines changed

6 files changed

+300
-1
lines changed

_includes/layouts/side-bar.html

+8
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
{% assign financial = true %}
4040
{% elsif page.display_as == "3d_charts" %}
4141
{% assign 3d_charts = true %}
42+
{% elsif page.display_as == "ai_ml" %}
43+
{% assign ai_ml = true %}
4244
{% elsif page.display_as == "advanced_charts"%}
4345
{% assign advanced_charts = true %}
4446
{% elsif page.display_as == "multiple_axes" %}
@@ -259,6 +261,12 @@
259261
</li>
260262
{% endif %}
261263

264+
{% if ai_ml == true %}
265+
<li class="--sidebar-item">
266+
<a href="#ai-ml" class="js-splash--navigation-item">Artificial Intelligence and Machine Learning</a>
267+
</li>
268+
{% endif %}
269+
262270
{% if scientific == true %}
263271
<li class="--sidebar-item">
264272
<a href="/{{langue}}/scientific-charts/" class="js-splash--navigation-item">Scientific Charts</a>

_includes/posts/documentation_eg.html

+32
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
{% assign chart_studio = true %}
3939
{% elsif page.display_as == "advanced_opt" %}
4040
{% assign advanced_opt = true %}
41+
{% elsif page.display_as == "ai_ml" %}
42+
{% assign ai_ml = true %}
4143

4244
<!-- START OF GGPLOT CUSTOM LAYOUT -->
4345
{% elsif page.display_as == "aesthetics" %}
@@ -164,6 +166,36 @@
164166
</section>
165167
{% endif %}
166168

169+
{% if ai_ml %}
170+
<section class="--tutorial-section" id="ai-ml">
171+
<header class="--section-header"><a href="#ai-ml" name="ai-ml" id="basic-charts">Artifical Intelligence and Machine Learning Charts</a>
172+
</header>
173+
<section class="--grid">
174+
<ul class="--grid-list">
175+
{%- for page in languagelist -%}
176+
{% if page.display_as == "ai_ml" %}
177+
178+
<li style="background-image: url({{site.imgurl}}{{page.thumbnail}});" class="--grid-item">
179+
<a href="{% if page.permalink contains 'http' %}{{page.permalink}}{% else %}/{{page.permalink}}{% endif %}">
180+
181+
182+
<!--<a href="{{page.permalink}}">-->
183+
<div class="--item-meta"><span>{{page.name}}</span></div>
184+
<div class="--item-image">
185+
<span>View Tutorial</span>
186+
<img src="{{site.imgurl}}{{page.thumbnail}}" alt="{{page.name}}">
187+
</div>
188+
</a>
189+
190+
</li>
191+
192+
{% endif %}
193+
{%- endfor -%}
194+
</ul>
195+
</section>
196+
</section>
197+
{% endif %}
198+
167199
{% if scientific %}
168200
<section class="--tutorial-section" id="scientific-charts">
169201
<header class="--section-header">
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
permalink: python/ai-ml/
3+
description: Plotly's Python graphing library makes interactive, publication-quality graphs online. Examples of how to make charts related to artificial intelligence and machine learning.
4+
name: More Artificial Intelligence and Machine Learning Charts
5+
layout: langindex
6+
language: python
7+
display_as: ai_ml
8+
thumbnail: thumbnail/mixed.jpg
9+
page_type: example_index
10+
order: 5
11+
---
12+
13+
14+
<header class="--welcome">
15+
<div class="--welcome-body">
16+
<!--div.--wrap-inner-->
17+
<div class="--title">
18+
<div class="--category-img"><img src="https://plot.ly/gh-pages/documentation/static/images/python-small.png" alt=""></div>
19+
<div class="--body">
20+
<h1>Plotly Python Open Source Graphing Library Artificial Intelligence and Machine Learning Charts</h1>
21+
<p>{{page.description}}</p>
22+
</div>
23+
</div>
24+
</div>
25+
</header>
26+
27+
28+
{% assign languagelist = site.posts | where: "language","python" | where: "display_as","ai_ml" | where: "layout","base" | sort: "order" %}
29+
{% include posts/documentation_eg.html %}

_posts/python/2020-02-23-ml-knn.md

+229
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
---
2+
description: How to visualize k-Nearest Neighbors (kNN) created using scikit-learn
3+
in Python with Plotly.
4+
display_as: ai_ml
5+
language: python
6+
layout: base
7+
name: K-Nearest Neighbors (kNN) Classification
8+
order: 1
9+
page_type: example_index
10+
permalink: python/knn/
11+
redirect_from: python/machine-learning-tutorials/
12+
thumbnail: thumbnail/line-and-scatter.jpg
13+
---
14+
15+
## Basic Binary Classification with `plotly.express`
16+
17+
```python
18+
import numpy as np
19+
import plotly.express as px
20+
import plotly.graph_objects as go
21+
from sklearn.datasets import make_moons
22+
from sklearn.neighbors import KNeighborsClassifier
23+
24+
X, y = make_moons(noise=0.3, random_state=0)
25+
X_test, _ = make_moons(noise=0.3, random_state=1)
26+
27+
clf = KNeighborsClassifier(15)
28+
clf.fit(X, y.astype(str)) # Fit on training set
29+
y_pred = clf.predict(X_test) # Predict on new data
30+
31+
fig = px.scatter(x=X_test[:, 0], y=X_test[:, 1], color=y_pred, labels={'color': 'predicted'})
32+
fig.update_traces(marker_size=10)
33+
fig.show()
34+
```
35+
36+
## Visualize Binary Prediction Scores
37+
38+
```python
39+
import numpy as np
40+
import plotly.express as px
41+
import plotly.graph_objects as go
42+
from sklearn.datasets import make_classification
43+
from sklearn.neighbors import KNeighborsClassifier
44+
45+
X, y = make_classification(n_features=2, n_redundant=0, random_state=0)
46+
X_test, _ = make_classification(n_features=2, n_redundant=0, random_state=1)
47+
48+
clf = KNeighborsClassifier(15)
49+
clf.fit(X, y) # Fit on training set
50+
y_score = clf.predict_proba(X_test)[:, 1] # Predict on new data
51+
52+
fig = px.scatter(x=X_test[:, 0], y=X_test[:, 1], color=y_score, labels={'color': 'score'})
53+
fig.update_traces(marker_size=10)
54+
fig.show()
55+
```
56+
57+
## Probability Estimates with `go.Contour`
58+
59+
```python
60+
import numpy as np
61+
import plotly.express as px
62+
import plotly.graph_objects as go
63+
from sklearn.datasets import make_moons
64+
from sklearn.neighbors import KNeighborsClassifier
65+
66+
mesh_size = .02
67+
margin = 1
68+
69+
X, y = make_moons(noise=0.3, random_state=0)
70+
71+
# Create a mesh grid on which we will run our model
72+
x_min, x_max = X[:, 0].min() - margin, X[:, 0].max() + margin
73+
y_min, y_max = X[:, 1].min() - margin, X[:, 1].max() + margin
74+
xrange = np.arange(x_min, x_max, mesh_size)
75+
yrange = np.arange(y_min, y_max, mesh_size)
76+
xx, yy = np.meshgrid(xrange, yrange)
77+
78+
# Create classifier, run predictions on grid
79+
clf = KNeighborsClassifier(15, weights='uniform')
80+
clf.fit(X, y)
81+
Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1]
82+
Z = Z.reshape(xx.shape)
83+
84+
fig = px.scatter(X, x=0, y=1, color=y.astype(str), labels={'0':'', '1':''})
85+
fig.update_traces(marker_size=10, marker_line_width=1)
86+
fig.add_trace(
87+
go.Contour(
88+
x=xrange,
89+
y=yrange,
90+
z=Z,
91+
showscale=False,
92+
colorscale=['Blue', 'Red'],
93+
opacity=0.4,
94+
name='Confidence'
95+
)
96+
)
97+
fig.show()
98+
```
99+
100+
## Multi-class prediction confidence with `go.Heatmap`
101+
102+
```python
103+
import numpy as np
104+
import plotly.express as px
105+
import plotly.graph_objects as go
106+
from sklearn.neighbors import KNeighborsClassifier
107+
108+
mesh_size = .02
109+
margin = 1
110+
111+
# We will use the iris data, which is included in px
112+
df = px.data.iris()
113+
X = df[['sepal_length', 'sepal_width']]
114+
y = df.species_id
115+
116+
# Create a mesh grid on which we will run our model
117+
l_min, l_max = df.sepal_length.min() - margin, df.sepal_length.max() + margin
118+
w_min, w_max = df.sepal_width.min() - margin, df.sepal_width.max() + margin
119+
lrange = np.arange(l_min, l_max, mesh_size)
120+
wrange = np.arange(w_min, w_max, mesh_size)
121+
ll, ww = np.meshgrid(lrange, wrange)
122+
123+
# Create classifier, run predictions on grid
124+
clf = KNeighborsClassifier(15, weights='distance')
125+
clf.fit(X, y)
126+
Z = clf.predict(np.c_[ll.ravel(), ww.ravel()])
127+
Z = Z.reshape(ll.shape)
128+
proba = clf.predict_proba(np.c_[ll.ravel(), ww.ravel()])
129+
proba = proba.reshape(ll.shape + (3,))
130+
131+
fig = px.scatter(df, x='sepal_length', y='sepal_width', color='species')
132+
fig.update_traces(marker_size=10, marker_line_width=1)
133+
fig.add_trace(
134+
go.Heatmap(
135+
x=lrange,
136+
y=wrange,
137+
z=Z,
138+
showscale=False,
139+
colorscale=[[0.0, 'blue'], [0.5, 'red'], [1.0, 'green']],
140+
opacity=0.25,
141+
customdata=proba,
142+
hovertemplate=(
143+
'sepal length: %{x} <br>'
144+
'sepal width: %{y} <br>'
145+
'p(setosa): %{customdata[0]:.3f}<br>'
146+
'p(versicolor): %{customdata[1]:.3f}<br>'
147+
'p(virginica): %{customdata[2]:.3f}<extra></extra>'
148+
)
149+
)
150+
)
151+
fig.show()
152+
```
153+
154+
## 3D Classification with `px.scatter_3d`
155+
156+
```python
157+
import numpy as np
158+
import plotly.express as px
159+
import plotly.graph_objects as go
160+
from sklearn.neighbors import KNeighborsClassifier
161+
from sklearn.model_selection import train_test_split
162+
163+
df = px.data.iris()
164+
features = ["sepal_width", "sepal_length", "petal_width"]
165+
166+
X = df[features]
167+
y = df.species
168+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
169+
170+
# Create classifier, run predictions on grid
171+
clf = KNeighborsClassifier(15, weights='distance')
172+
clf.fit(X_train, y_train)
173+
y_pred = clf.predict(X_test)
174+
y_score = clf.predict_proba(X_test)
175+
y_score = np.around(y_score.max(axis=1), 4)
176+
177+
fig = px.scatter_3d(
178+
X_test,
179+
x='sepal_length',
180+
y='sepal_width',
181+
z='petal_width',
182+
symbol=y_pred,
183+
color=y_score,
184+
labels={'symbol': 'prediction', 'color': 'score'}
185+
)
186+
fig.update_layout(legend=dict(x=0, y=0))
187+
fig.show()
188+
```
189+
190+
## High Dimension Visualization with `px.scatter_matrix`
191+
192+
If you need to visualize classifications that go beyond 3D, you can use the [scatter plot matrix](https://plot.ly/python/splom/).
193+
194+
```python
195+
import numpy as np
196+
import plotly.express as px
197+
import plotly.graph_objects as go
198+
from sklearn.neighbors import KNeighborsClassifier
199+
from sklearn.model_selection import train_test_split
200+
201+
df = px.data.iris()
202+
features = ["sepal_width", "sepal_length", "petal_width", "petal_length"]
203+
204+
X = df[features]
205+
y = df.species
206+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
207+
208+
# Create classifier, run predictions on grid
209+
clf = KNeighborsClassifier(15, weights='distance')
210+
clf.fit(X_train, y_train)
211+
y_pred = clf.predict(X_test)
212+
213+
fig = px.scatter_matrix(X_test, dimensions=features, color=y_pred, labels={'color': 'prediction'})
214+
fig.show()
215+
```
216+
217+
### Reference
218+
219+
Learn more about `px`, `go.Contour`, and `go.Heatmap` here:
220+
* https://plot.ly/python/plotly-express/
221+
* https://plot.ly/python/heatmaps/
222+
* https://plot.ly/python/contour-plots/
223+
* https://plot.ly/python/3d-scatter-plots/
224+
* https://plot.ly/python/splom/
225+
226+
This tutorial was inspired by amazing examples from the official scikit-learn docs:
227+
* https://scikit-learn.org/stable/auto_examples/neighbors/plot_classification.html
228+
* https://scikit-learn.org/stable/auto_examples/classification/plot_classifier_comparison.html
229+
* https://scikit-learn.org/stable/auto_examples/datasets/plot_iris_dataset.html

check-or-enforce-order.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
if sys.argv[2] == 'enforce':
1717
enforce = True
1818

19-
categories = ["file_settings", "basic", "financial", "statistical", "scientific", "maps", "3d_charts", "multiple_axes"]
19+
categories = ["file_settings", "basic", "financial", "statistical", "scientific", "maps", "3d_charts", "multiple_axes", "ai_ml"]
2020

2121
def get_post(path):
2222
return fm.load(str(path))

front-matter-ci.py

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def check_noTrailingSlash(meta_to_check):
7575
"maps",
7676
"3d_charts",
7777
"multiple_axes",
78+
"ai_ml"
7879
]
7980
languages = ["python", "python/v3", "plotly_js", "r"]
8081

0 commit comments

Comments
 (0)