# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + [markdown] colab_type="text" id="view-in-github"
# <a href="https://colab.research.google.com/github/DJCordhose/ml-workshop/blob/master/notebooks/unsupervised/umap.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] colab_type="text" id="cwLsLRZf7fjN"
# # UMAP Experiments
#
# * https://github.com/lmcinnes/umap
# * https://www.youtube.com/watch?v=nq6iPZVUxZU
# * https://umap-learn.readthedocs.io/en/latest/
# * https://github.com/scikit-learn-contrib/hdbscan

# + colab={} colab_type="code" id="J9s56l5k8051"
# # !pip install umap-learn

# + colab={} colab_type="code" id="cEp8WAKQ7fjT"
# %matplotlib inline

# + colab={} colab_type="code" id="yrcSKSb57fjW"
import numpy as np

# + colab={} colab_type="code" id="7XhXDmSQ7fjY"
# noisy line
# X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2], [4, 3], [4, -1]])

# clear line
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2], [4, 3], [0, 0]])

# V shape
# X = np.array([[-1, 1], [-2, 2], [-3, 3], [1, 1], [2, 2], [3, 3], [4, 4]])

# + colab={"base_uri": "https://localhost:8080/", "height": 153} colab_type="code" id="tk-r0gY47fjb" outputId="20128822-ace9-40ec-e7bf-b464816de56b"
X

# + colab={} colab_type="code" id="2735tPwd7fjg"
import matplotlib.pyplot as plt

# + colab={"base_uri": "https://localhost:8080/", "height": 612} colab_type="code" id="AwO8x0NW7fjj" outputId="e38f566b-eca1-4ad5-9669-537c45992c03"
plt.figure(figsize=(10,10))
plt.scatter(X[:, 0], X[:, 1])

# + colab={} colab_type="code" id="dVovQBvQ8Cn7"
import umap

# + colab={"base_uri": "https://localhost:8080/", "height": 34} colab_type="code" id="zs3IzA-KeayU" outputId="f7258c45-4cce-44c2-ccef-b1791cce19b6"
umap.__version__

# + colab={} colab_type="code" id="U_e75U1z7fjn"
umap_clf = umap.UMAP()

# + colab={"base_uri": "https://localhost:8080/", "height": 187} colab_type="code" id="MC1qMDlU7fjq" outputId="197f7301-3789-4b48-9337-2505ccfc6a7e"
umap_clf.fit(X)

# + colab={"base_uri": "https://localhost:8080/", "height": 51} colab_type="code" id="T_b6mdECgv5T" outputId="2434e2ac-6878-4145-9dfd-7648ea3cfa37"
x_points = umap_clf.transform([[-1, 0], [1, 0]])
x_points

# + colab={"base_uri": "https://localhost:8080/", "height": 51} colab_type="code" id="HLjue46zgx6Y" outputId="5e152820-1167-4ca5-e971-b94b525c1428"
y_points = umap_clf.transform([[0, -1], [0, 1]])
y_points

# + colab={"base_uri": "https://localhost:8080/", "height": 612} colab_type="code" id="lm85t1-Qg_Ej" outputId="36993638-89ed-4437-948e-93aa7bdc046d"
plt.figure(figsize=(10,10))
plt.scatter(X[:, 0], X[:, 1])
plt.plot(x_points.transpose()[0], x_points.transpose()[1], 'go-')
plt.plot(y_points.transpose()[0], y_points.transpose()[1], 'mo-')
# plt.xlim(-4, 4)
# plt.ylim(-4, 4)

# + colab={"base_uri": "https://localhost:8080/", "height": 153} colab_type="code" id="gXaYxILc7fju" outputId="5abef357-bc22-4071-906a-6c31b5776a9e"
X_transformed = umap_clf.transform(X)
X_transformed

# + colab={"base_uri": "https://localhost:8080/", "height": 612} colab_type="code" id="QNVsLwXZ7fjz" outputId="5538039e-b0e2-4190-ace3-d2e32117141e"
plt.figure(figsize=(10,10))
plt.scatter(X_transformed[:, 0], X_transformed[:, 1])

# + [markdown] colab_type="text" id="u2GolB4tgjHA"
# ## Reduction 1 dimension

# + colab={} colab_type="code" id="f9jcwlXx7fj4"
umap_clf = umap.UMAP(n_components=1)

# + colab={"base_uri": "https://localhost:8080/", "height": 51} colab_type="code" id="j5kaNQF87fj6" outputId="1a6c121b-a5a3-4b6c-92c6-9623a5745467"
X_transformed = umap_clf.fit_transform(X)

# + colab={"base_uri": "https://localhost:8080/", "height": 153} colab_type="code" id="8EoeUhXM7fj9" outputId="88c5de08-de5b-449e-c499-3d633af87309"
X_transformed

# + colab={"base_uri": "https://localhost:8080/", "height": 178} colab_type="code" id="57p-nCwy7fkA" outputId="e5ef731a-6f38-41ef-f83e-f2d834290e8a"
plt.figure(figsize=(10,2))
plt.scatter(X_transformed, np.zeros(len(X_transformed)))
# plt.xlim(-4, 4)
plt.ylim(-1, 1)

# + colab={} colab_type="code" id="I8Z1dm-uhwd8"


StarcoderdataJupyterScriptsDedupFiltered