La función make_two_spirals — 3:19 min#

  • 3:19 min | Ultima modificación: Septiembre 27, 2021 | YouTube

https://glowingpython.blogspot.com/2017/04/solving-two-spirals-problem-with-keras.html

Problema clásico de clasificación de dos espirales.

[1]:
import matplotlib.pyplot as plt
import numpy as np
from numpy.random import default_rng


def make_two_spirals(
    n_samples=200,
    noise=0,
    random_state=None,
):

    if random_state is None:
        rng = default_rng()
    else:
        rng = default_rng(12345)

    n = np.sqrt(rng.uniform(size=(n_samples, 1))) * 780 * (2 * np.pi) / 360
    d1x = -np.cos(n) * n + np.random.rand(n_samples, 1) * noise
    d1y = np.sin(n) * n + np.random.rand(n_samples, 1) * noise

    X = np.vstack((np.hstack((d1x, d1y)), np.hstack((-d1x, -d1y))))
    y = np.hstack((np.zeros(n_samples), np.ones(n_samples)))

    return X, y


X, y = make_two_spirals(
    n_samples=300,
    noise=0.0,
)

plt.figure(figsize=(7, 7))
plt.scatter(
    X[y == 0, 0],
    X[y == 0, 1],
    color="tab:blue",
    edgecolors="k",
    s=120,
    alpha=0.9,
)

plt.scatter(
    X[y == 1, 0],
    X[y == 1, 1],
    color="tab:red",
    edgecolors="k",
    s=120,
    alpha=0.9,
)

plt.gca().spines["left"].set_color("gray")
plt.gca().spines["bottom"].set_color("gray")
plt.gca().spines["top"].set_visible(False)
plt.gca().spines["right"].set_visible(False)
plt.show()
../_images/53_datasets_24_make_spiral_3_0.png
[2]:
X, y = make_two_spirals(
    n_samples=300,
    noise=1.2,
)

plt.figure(figsize=(7, 7))
plt.scatter(
    X[y == 0, 0],
    X[y == 0, 1],
    color="tab:blue",
    edgecolors="k",
    s=120,
    alpha=0.9,
)

plt.scatter(
    X[y == 1, 0],
    X[y == 1, 1],
    color="tab:red",
    edgecolors="k",
    s=120,
    alpha=0.9,
)

plt.gca().spines["left"].set_color("gray")
plt.gca().spines["bottom"].set_color("gray")
plt.gca().spines["top"].set_visible(False)
plt.gca().spines["right"].set_visible(False)
plt.show()
../_images/53_datasets_24_make_spiral_4_0.png