Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generated gh-pages for commit d4ed39f
Merge: ebe6bda 6dd07ed Author: Nathaniel Saul <nat@saulgill.com> Merge pull request #167 from cmorph1/remove-mutable-arguments
- Loading branch information
Nathaniel Saul
committed
Apr 24, 2019
1 parent
2abe9ad
commit 2a649ea
Showing
72 changed files
with
5,800 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"%matplotlib inline" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"\nDigits Dataset\n================\n\nsomething something something\n\n\n\n![](../../../examples/images/digits-tooltip.png)\n\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import io\nimport sys\nimport base64\n\nimport numpy as np\nimport sklearn\nfrom sklearn import datasets\nimport kmapper as km\n\ntry:\n from scipy.misc import imsave, toimage\nexcept ImportError as e:\n print(\"imsave requires you to install pillow. Run `pip install pillow` and then try again.\")\n sys.exit()\n\n\n# Load digits dat\ndata, labels = datasets.load_digits().data, datasets.load_digits().target\n\n# Create images for a custom tooltip array\ntooltip_s = []\nfor image_data in data:\n output = io.BytesIO()\n img = toimage(image_data.reshape((8, 8))) # Data was a flat row of 64 \"pixels\".\n img.save(output, format=\"PNG\")\n contents = output.getvalue()\n img_encoded = base64.b64encode(contents)\n img_tag = \"\"\"<img src=\"data:image/png;base64,{}\">\"\"\".format(img_encoded.decode('utf-8'))\n tooltip_s.append(img_tag)\n output.close()\n\ntooltip_s = np.array(tooltip_s) # need to make sure to feed it as a NumPy array, not a list\n\n# Initialize to use t-SNE with 2 components (reduces data to 2 dimensions). Also note high overlap_percentage.\nmapper = km.KeplerMapper(verbose=2)\n\n# Fit and transform data\nprojected_data = mapper.fit_transform(data,\n projection=sklearn.manifold.TSNE())\n\n# Create the graph (we cluster on the projected data and suffer projection loss)\ngraph = mapper.map(projected_data,\n clusterer=sklearn.cluster.DBSCAN(eps=0.3, min_samples=15),\n cover=km.Cover(35, 0.4))\n\n# Create the visualizations (increased the graph_gravity for a tighter graph-look.)\n\n# Tooltips with image data for every cluster member\nmapper.visualize(graph,\n title=\"Handwritten digits Mapper\",\n path_html=\"output/digits_custom_tooltips.html\",\n color_function=labels,\n custom_tooltips=tooltip_s)\n# Tooltips with the target y-labels for every cluster member\nmapper.visualize(graph,\n title=\"Handwritten digits Mapper\",\n path_html=\"output/digits_ylabel_tooltips.html\",\n custom_tooltips=labels)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.7.1" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |
59 changes: 59 additions & 0 deletions
59
_downloads/24aaea14d382dbea62b56766a05e6d9b/plot_breast_cancer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
""" | ||
Breast Cancer | ||
================ | ||
This example generates a Mapper built from the `Wisconsin Breast Cancer Dataset <https://www.kaggle.com/uciml/breast-cancer-wisconsin-data>`_. | ||
.. image:: ../../../examples/images/breast-cancer.png | ||
""" | ||
|
||
import sys | ||
try: | ||
import pandas as pd | ||
except ImportError as e: | ||
print("pandas is required for this example. Please install with `pip install pandas` and then try again.") | ||
sys.exit() | ||
|
||
import numpy as np | ||
import kmapper as km | ||
import sklearn | ||
from sklearn import ensemble | ||
|
||
# For data we use the Wisconsin Breast Cancer Dataset | ||
# Via: | ||
df = pd.read_csv("data/breast-cancer.csv") | ||
feature_names = [c for c in df.columns if c not in ["id", "diagnosis"]] | ||
df["diagnosis"] = df["diagnosis"].apply(lambda x: 1 if x == "M" else 0) | ||
X = np.array(df[feature_names].fillna(0)) # quick and dirty imputation | ||
y = np.array(df["diagnosis"]) | ||
|
||
# We create a custom 1-D lens with Isolation Forest | ||
model = ensemble.IsolationForest(random_state=1729) | ||
model.fit(X) | ||
lens1 = model.decision_function(X).reshape((X.shape[0], 1)) | ||
|
||
# We create another 1-D lens with L2-norm | ||
mapper = km.KeplerMapper(verbose=3) | ||
lens2 = mapper.fit_transform(X, projection="l2norm") | ||
|
||
# Combine both lenses to create a 2-D [Isolation Forest, L^2-Norm] lens | ||
lens = np.c_[lens1, lens2] | ||
|
||
# Create the simplicial complex | ||
graph = mapper.map(lens, | ||
X, | ||
cover=km.Cover(n_cubes=15, perc_overlap=0.4), | ||
clusterer=sklearn.cluster.KMeans(n_clusters=2, | ||
random_state=1618033)) | ||
|
||
# Visualization | ||
mapper.visualize(graph, | ||
path_html="output/breast-cancer.html", | ||
title="Wisconsin Breast Cancer Dataset", | ||
custom_tooltips=y) |
54 changes: 54 additions & 0 deletions
54
_downloads/2eb975460e21ae89ccc5461faf57bc20/plot_horse.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"%matplotlib inline" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"\n3D Horse Data\n================\n\n\nThis example generates a Mapper built from a point-cloud sampled from a 3D model of a horse.\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import matplotlib.pyplot as plt\nimport numpy as np\nimport sklearn\n\nimport kmapper as km\n\ndata = np.genfromtxt('data/horse-reference.csv', delimiter=',')\n\nmapper = km.KeplerMapper(verbose=2)\n\n\nlens = mapper.fit_transform(data)\n\n\ngraph = mapper.map(lens,\n data,\n clusterer=sklearn.cluster.DBSCAN(eps=0.1, min_samples=5),\n cover=km.Cover(30, 0.2))\n\nmapper.visualize(graph,\n path_html=\"output/horse_keplermapper.html\",\n custom_tooltips=np.arange(len(lens)))\n\n\nkm.drawing.draw_matplotlib(graph)\n\nplt.show()" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.7.1" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
""" | ||
3D Cat Data | ||
============ | ||
This example generates a Mapper built from a point-cloud sampled from a 3D model of a cat. | ||
""" | ||
|
||
|
||
import numpy as np | ||
import sklearn | ||
import kmapper as km | ||
|
||
data = np.genfromtxt('data/cat-reference.csv', delimiter=',') | ||
|
||
mapper = km.KeplerMapper(verbose=2) | ||
|
||
|
||
lens = mapper.fit_transform(data) | ||
|
||
graph = mapper.map(lens, | ||
data, | ||
clusterer=sklearn.cluster.DBSCAN(eps=0.1, min_samples=5), | ||
cover=km.Cover(n_cubes=15, perc_overlap=0.2)) | ||
|
||
mapper.visualize(graph, | ||
path_html="output/cat_keplermapper.html") | ||
|
||
km.draw_matplotlib(graph) | ||
|
||
import matplotlib.pyplot as plt | ||
plt.show() |
54 changes: 54 additions & 0 deletions
54
_downloads/8a5b90c8d1e8e24f002588e2741438fd/plot_cat.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"%matplotlib inline" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"3D Cat Data\n============\n\n\nThis example generates a Mapper built from a point-cloud sampled from a 3D model of a cat.\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np\nimport sklearn\nimport kmapper as km\n\ndata = np.genfromtxt('data/cat-reference.csv', delimiter=',')\n\nmapper = km.KeplerMapper(verbose=2)\n\n\nlens = mapper.fit_transform(data)\n\ngraph = mapper.map(lens,\n data,\n clusterer=sklearn.cluster.DBSCAN(eps=0.1, min_samples=5),\n cover=km.Cover(n_cubes=15, perc_overlap=0.2))\n\nmapper.visualize(graph,\n path_html=\"output/cat_keplermapper.html\")\n\nkm.draw_matplotlib(graph)\n\nimport matplotlib.pyplot as plt\nplt.show()" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.7.1" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |
Binary file not shown.
54 changes: 54 additions & 0 deletions
54
_downloads/c36804259dbeba696d63e13c0538f21c/plot_breast_cancer.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"%matplotlib inline" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"\nBreast Cancer\n================\n\n\n\nThis example generates a Mapper built from the `Wisconsin Breast Cancer Dataset <https://www.kaggle.com/uciml/breast-cancer-wisconsin-data>`_.\n\n\n![](../../../examples/images/breast-cancer.png)\n\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import sys\ntry:\n import pandas as pd\nexcept ImportError as e:\n print(\"pandas is required for this example. Please install with `pip install pandas` and then try again.\")\n sys.exit()\n\nimport numpy as np\nimport kmapper as km\nimport sklearn\nfrom sklearn import ensemble\n\n# For data we use the Wisconsin Breast Cancer Dataset\n# Via: \ndf = pd.read_csv(\"data/breast-cancer.csv\")\nfeature_names = [c for c in df.columns if c not in [\"id\", \"diagnosis\"]]\ndf[\"diagnosis\"] = df[\"diagnosis\"].apply(lambda x: 1 if x == \"M\" else 0)\nX = np.array(df[feature_names].fillna(0)) # quick and dirty imputation\ny = np.array(df[\"diagnosis\"])\n\n# We create a custom 1-D lens with Isolation Forest\nmodel = ensemble.IsolationForest(random_state=1729)\nmodel.fit(X)\nlens1 = model.decision_function(X).reshape((X.shape[0], 1))\n\n# We create another 1-D lens with L2-norm\nmapper = km.KeplerMapper(verbose=3)\nlens2 = mapper.fit_transform(X, projection=\"l2norm\")\n\n# Combine both lenses to create a 2-D [Isolation Forest, L^2-Norm] lens\nlens = np.c_[lens1, lens2]\n\n# Create the simplicial complex\ngraph = mapper.map(lens,\n X,\n cover=km.Cover(n_cubes=15, perc_overlap=0.4),\n clusterer=sklearn.cluster.KMeans(n_clusters=2,\n random_state=1618033))\n\n# Visualization\nmapper.visualize(graph,\n path_html=\"output/breast-cancer.html\",\n title=\"Wisconsin Breast Cancer Dataset\",\n custom_tooltips=y)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.7.1" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
""" | ||
Digits Dataset | ||
================ | ||
something something something | ||
.. image:: ../../../examples/images/digits-tooltip.png | ||
""" | ||
|
||
|
||
|
||
import io | ||
import sys | ||
import base64 | ||
|
||
import numpy as np | ||
import sklearn | ||
from sklearn import datasets | ||
import kmapper as km | ||
|
||
try: | ||
from scipy.misc import imsave, toimage | ||
except ImportError as e: | ||
print("imsave requires you to install pillow. Run `pip install pillow` and then try again.") | ||
sys.exit() | ||
|
||
|
||
# Load digits dat | ||
data, labels = datasets.load_digits().data, datasets.load_digits().target | ||
|
||
# Create images for a custom tooltip array | ||
tooltip_s = [] | ||
for image_data in data: | ||
output = io.BytesIO() | ||
img = toimage(image_data.reshape((8, 8))) # Data was a flat row of 64 "pixels". | ||
img.save(output, format="PNG") | ||
contents = output.getvalue() | ||
img_encoded = base64.b64encode(contents) | ||
img_tag = """<img src="data:image/png;base64,{}">""".format(img_encoded.decode('utf-8')) | ||
tooltip_s.append(img_tag) | ||
output.close() | ||
|
||
tooltip_s = np.array(tooltip_s) # need to make sure to feed it as a NumPy array, not a list | ||
|
||
# Initialize to use t-SNE with 2 components (reduces data to 2 dimensions). Also note high overlap_percentage. | ||
mapper = km.KeplerMapper(verbose=2) | ||
|
||
# Fit and transform data | ||
projected_data = mapper.fit_transform(data, | ||
projection=sklearn.manifold.TSNE()) | ||
|
||
# Create the graph (we cluster on the projected data and suffer projection loss) | ||
graph = mapper.map(projected_data, | ||
clusterer=sklearn.cluster.DBSCAN(eps=0.3, min_samples=15), | ||
cover=km.Cover(35, 0.4)) | ||
|
||
# Create the visualizations (increased the graph_gravity for a tighter graph-look.) | ||
|
||
# Tooltips with image data for every cluster member | ||
mapper.visualize(graph, | ||
title="Handwritten digits Mapper", | ||
path_html="output/digits_custom_tooltips.html", | ||
color_function=labels, | ||
custom_tooltips=tooltip_s) | ||
# Tooltips with the target y-labels for every cluster member | ||
mapper.visualize(graph, | ||
title="Handwritten digits Mapper", | ||
path_html="output/digits_ylabel_tooltips.html", | ||
custom_tooltips=labels) |
Oops, something went wrong.