Skip to content

Commit 4ae98ea

Browse files
author
Mark Hale
committed
Added nearest_node example.
1 parent 3d6262f commit 4ae98ea

3 files changed

Lines changed: 57 additions & 20 deletions

File tree

examples/nearest_node.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
nearest_nodes example based on breast cancer data.
3+
"""
4+
5+
from plot_breast_cancer import *
6+
from sklearn import neighbors, preprocessing
7+
8+
# new patient data incoming
9+
i = np.random.randint(len(X))
10+
new_patient_data = 1.05*X[i]
11+
new_patient_data = new_patient_data.reshape(1, -1)
12+
13+
# re-use lens1 model
14+
newlens1 = model.decision_function(new_patient_data)
15+
16+
# re-construct lens2 model
17+
X_norm = np.linalg.norm(X, axis=1)
18+
scaler = preprocessing.MinMaxScaler()
19+
scaler.fit(X_norm.reshape(-1, 1))
20+
21+
newlens2 = scaler.transform(np.linalg.norm(new_patient_data, axis=1).reshape(1, -1))
22+
23+
newlens = np.c_[newlens1, newlens2]
24+
25+
# find nearest nodes
26+
nn = neighbors.NearestNeighbors(n_neighbors=3)
27+
node_ids = mapper.nearest_nodes(newlens, new_patient_data, graph, mapper.cover, lens, X, nn)
28+
29+
print("Nearest nodes:")
30+
for node_id in node_ids:
31+
diags = y[graph['nodes'][node_id]]
32+
print(" {}: diagnosis {:.1f}%".format(node_id, np.sum(diags)*100.0/len(diags)))

examples/output/breast-cancer.html

Lines changed: 20 additions & 20 deletions
Large diffs are not rendered by default.

examples/plot_breast_cancer.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,13 @@
6868

6969
# Visualization
7070
mapper.visualize(graph,
71+
X=X,
72+
X_names=feature_names,
73+
lens=lens,
74+
lens_names=["Isolation forest", "l2-norm"],
7175
path_html="output/breast-cancer.html",
7276
title="Wisconsin Breast Cancer Dataset",
77+
color_function=y,
7378
custom_tooltips=y)
7479

7580

0 commit comments

Comments
 (0)