Skip to content

Commit

Permalink
Added nearest_node example.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Hale committed Mar 19, 2021
1 parent 3d6262f commit 4ae98ea
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 20 deletions.
32 changes: 32 additions & 0 deletions examples/nearest_node.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
nearest_nodes example based on breast cancer data.
"""

from plot_breast_cancer import *
from sklearn import neighbors, preprocessing

# new patient data incoming
i = np.random.randint(len(X))
new_patient_data = 1.05*X[i]
new_patient_data = new_patient_data.reshape(1, -1)

# re-use lens1 model
newlens1 = model.decision_function(new_patient_data)

# re-construct lens2 model
X_norm = np.linalg.norm(X, axis=1)
scaler = preprocessing.MinMaxScaler()
scaler.fit(X_norm.reshape(-1, 1))

newlens2 = scaler.transform(np.linalg.norm(new_patient_data, axis=1).reshape(1, -1))

newlens = np.c_[newlens1, newlens2]

# find nearest nodes
nn = neighbors.NearestNeighbors(n_neighbors=3)
node_ids = mapper.nearest_nodes(newlens, new_patient_data, graph, mapper.cover, lens, X, nn)

print("Nearest nodes:")
for node_id in node_ids:
diags = y[graph['nodes'][node_id]]
print(" {}: diagnosis {:.1f}%".format(node_id, np.sum(diags)*100.0/len(diags)))
40 changes: 20 additions & 20 deletions examples/output/breast-cancer.html

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions examples/plot_breast_cancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,13 @@

# Visualization
mapper.visualize(graph,
X=X,
X_names=feature_names,
lens=lens,
lens_names=["Isolation forest", "l2-norm"],
path_html="output/breast-cancer.html",
title="Wisconsin Breast Cancer Dataset",
color_function=y,
custom_tooltips=y)


Expand Down

0 comments on commit 4ae98ea

Please sign in to comment.