Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a691be2

Browse filesBrowse files
authored
add example of exploration of a covariance matrix (#652)
* add cov matrix example * new file: examples/machine_learning/README.rst * update conf.py with machine_learning examples section
1 parent b5dff56 commit a691be2
Copy full SHA for a691be2

File tree

3 files changed

+99
-2
lines changed
Filter options

3 files changed

+99
-2
lines changed

‎docs/source/conf.py

Copy file name to clipboardExpand all lines: docs/source/conf.py
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,16 @@
5656
"subsection_order": ExplicitOrder(
5757
[
5858
"../../examples/image",
59+
"../../examples/heatmap",
5960
"../../examples/image_widget",
6061
"../../examples/gridplot",
6162
"../../examples/line",
6263
"../../examples/line_collection",
6364
"../../examples/scatter",
64-
"../../examples/heatmap",
65-
"../../examples/misc",
6665
"../../examples/selection_tools",
66+
"../../examples/machine_learning",
6767
"../../examples/guis",
68+
"../../examples/misc",
6869
"../../examples/qt",
6970
]
7071
),

‎examples/machine_learning/README.rst

Copy file name to clipboard
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Machine Learning Examples
2+
=========================
+94Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
"""
2+
Explore Covariance Matrix
3+
=========================
4+
5+
Example showing how you can explore a covariance matrix with a selector tool.
6+
"""
7+
8+
# test_example = false
9+
# sphinx_gallery_pygfx_docs = 'animate 10s'
10+
11+
12+
import fastplotlib as fpl
13+
from sklearn import datasets
14+
from sklearn.preprocessing import StandardScaler
15+
16+
# load faces dataset
17+
faces = datasets.fetch_olivetti_faces()
18+
data = faces["data"]
19+
20+
# sort the data so it's easier to understand the covariance matrix
21+
targets = faces["target"]
22+
sort_indices = targets.argsort()
23+
targets_sorted = targets[sort_indices]
24+
25+
X = data[sort_indices]
26+
27+
# scale the data w.r.t. mean and standard deviation
28+
X = StandardScaler().fit_transform(X)
29+
30+
# compute covariance matrix
31+
X = X.T
32+
cov = X @ X.T / X.shape[1]
33+
34+
# reshaped image for each sample wil be 64 x 64 pixels
35+
img = cov[0].reshape(64, 64)
36+
37+
# figure kwargs for image widget
38+
# controller_ids = [[0, 1]] so we get independent controllers for each supblot
39+
# the covariance matrix is 4096 x 4096 and the reshaped image ix 64 x 64
40+
figure_kwargs = {"size": (700, 400), "controller_ids": [[0, 1]]}
41+
42+
# create image widget
43+
iw = fpl.ImageWidget(
44+
data=[cov, img], # display the covariance matrix and reshaped image of a row
45+
cmap="bwr", # diverging colormap
46+
names=["covariance", "row image"],
47+
figure_kwargs=figure_kwargs,
48+
)
49+
50+
# graphic that corresponds to image widget data array 0
51+
# 0 is the covariance matrix, 1 is the reshaped image of a row from the covariance matrix
52+
53+
# add a linear selector to select y axis values so we can select rows of the cov matrix
54+
selector_cov = iw.managed_graphics[0].add_linear_selector(axis="y")
55+
56+
# if you are exploring other types of matrices which are not-symmetric
57+
# you can also add a column selector by setting axis="x"
58+
59+
# set vmin vmax
60+
for g in iw.managed_graphics:
61+
g.vmin, g.vmax = -1, 1
62+
63+
64+
# event handler when the covariance matrix row changes
65+
@selector_cov.add_event_handler("selection")
66+
def update_img(ev):
67+
# get the row index
68+
ix = ev.get_selected_index()
69+
70+
# get the image the corresponds to this row
71+
img = cov[ix].reshape(64, 64)
72+
73+
# change the reshaped image graphic data
74+
iw.managed_graphics[1].data = img
75+
76+
77+
figure = iw.figure # not required, just for the docs gallery to pick it up
78+
79+
80+
# move the selector programmatically, this is mainly for the docs gallery
81+
# for real use you can interact with the selector with your mouse
82+
def animate():
83+
selector_cov.selection += 1
84+
85+
86+
iw.figure.add_animations(animate)
87+
88+
iw.show()
89+
90+
# NOTE: `if __name__ == "__main__"` is NOT how to use fastplotlib interactively
91+
# please see our docs for using fastplotlib interactively in ipython and jupyter
92+
if __name__ == "__main__":
93+
print(__doc__)
94+
fpl.run()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.