<gh_stars>0
import h5py
import numpy
import sklearn
import sklearn.datasets
from matplotlib import pyplot


def load_dataset():
    data_dir = '/Users/fpena/Courses/Coursera-Deep-Learning/Assignments/datasets/'

    train_dataset = h5py.File(data_dir + 'train_catvnoncat.h5', "r")
    train_set_x_orig = numpy.array(train_dataset["train_set_x"][:])  # your train set features
    train_set_y_orig = numpy.array(train_dataset["train_set_y"][:])  # your train set labels

    test_dataset = h5py.File(data_dir + 'test_catvnoncat.h5', "r")
    test_set_x_orig = numpy.array(test_dataset["test_set_x"][:])  # your test set features
    test_set_y_orig = numpy.array(test_dataset["test_set_y"][:])  # your test set labels

    classes = numpy.array(test_dataset["list_classes"][:])  # the list of classes

    train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
    test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))

    return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes


def preprocess_dataset(train_set_x_orig, test_set_x_orig):
    train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0], -1).T
    test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0], -1).T

    train_set_x = train_set_x_flatten / 255.
    test_set_x = test_set_x_flatten / 255.

    return train_set_x, test_set_x


def plot_decision_boundary(model, X, y):
    # Set min and max values and give it some padding
    x_min, x_max = X[0, :].min() - 1, X[0, :].max() + 1
    y_min, y_max = X[1, :].min() - 1, X[1, :].max() + 1
    h = 0.01
    # Generate a grid of points with distance h between them
    xx, yy = numpy.meshgrid(numpy.arange(x_min, x_max, h), numpy.arange(y_min, y_max, h))
    # Predict the function value for the whole grid
    Z = model(numpy.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
    # Plot the contour and training examples
    pyplot.contourf(xx, yy, Z, cmap=pyplot.cm.Spectral)
    pyplot.ylabel('x2')
    pyplot.xlabel('x1')
    pyplot.scatter(X[0, :], X[1, :], c=y, cmap=pyplot.cm.Spectral)
    # pyplot.show()


def load_planar_dataset():
    numpy.random.seed(1)
    m = 400  # number of examples
    N = int(m / 2)  # number of points per class
    D = 2  # dimensionality
    X = numpy.zeros((m, D))  # data matrix where each row is a single example
    Y = numpy.zeros((m, 1), dtype='uint8')  # labels vector (0 for red, 1 for blue)
    a = 4  # maximum ray of the flower

    for j in range(2):
        ix = range(N * j, N * (j + 1))
        t = numpy.linspace(j * 3.12, (j + 1) * 3.12, N) + numpy.random.randn(N) * 0.2  # theta
        r = a * numpy.sin(4 * t) + numpy.random.randn(N) * 0.2  # radius
        X[ix] = numpy.c_[r * numpy.sin(t), r * numpy.cos(t)]
        Y[ix] = j

    X = X.T
    Y = Y.T

    return X, Y


def load_extra_datasets():
    N = 200
    noisy_circles = sklearn.datasets.make_circles(n_samples=N, factor=.5, noise=.3)
    noisy_moons = sklearn.datasets.make_moons(n_samples=N, noise=.2)
    blobs = sklearn.datasets.make_blobs(n_samples=N, random_state=5, n_features=2, centers=6)
    gaussian_quantiles = sklearn.datasets.make_gaussian_quantiles(mean=None, cov=0.5, n_samples=N, n_features=2,
                                                                  n_classes=2, shuffle=True, random_state=None)
    no_structure = numpy.random.rand(N, 2), numpy.random.rand(N, 2)

    return noisy_circles, noisy_moons, blobs, gaussian_quantiles, no_structure

import h5py
import numpy
import sklearn
import sklearn.datasets
from matplotlib import pyplot


def load_dataset():
    data_dir = '/Users/fpena/Courses/Coursera-Deep-Learning/Assignments/datasets/'

    train_dataset = h5py.File(data_dir + 'train_catvnoncat.h5', "r")
    train_set_x_orig = numpy.array(train_dataset["train_set_x"][:])  # your train set features
    train_set_y_orig = numpy.array(train_dataset["train_set_y"][:])  # your train set labels

    test_dataset = h5py.File(data_dir + 'test_catvnoncat.h5', "r")
    test_set_x_orig = numpy.array(test_dataset["test_set_x"][:])  # your test set features
    test_set_y_orig = numpy.array(test_dataset["test_set_y"][:])  # your test set labels

    classes = numpy.array(test_dataset["list_classes"][:])  # the list of classes

    train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
    test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))

    return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes


def preprocess_dataset(train_set_x_orig, test_set_x_orig):
    train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0], -1).T
    test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0], -1).T

    train_set_x = train_set_x_flatten / 255.
    test_set_x = test_set_x_flatten / 255.

    return train_set_x, test_set_x


def plot_decision_boundary(model, X, y):
    # Set min and max values and give it some padding
    x_min, x_max = X[0, :].min() - 1, X[0, :].max() + 1
    y_min, y_max = X[1, :].min() - 1, X[1, :].max() + 1
    h = 0.01
    # Generate a grid of points with distance h between them
    xx, yy = numpy.meshgrid(numpy.arange(x_min, x_max, h), numpy.arange(y_min, y_max, h))
    # Predict the function value for the whole grid
    Z = model(numpy.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
    # Plot the contour and training examples
    pyplot.contourf(xx, yy, Z, cmap=pyplot.cm.Spectral)
    pyplot.ylabel('x2')
    pyplot.xlabel('x1')
    pyplot.scatter(X[0, :], X[1, :], c=y, cmap=pyplot.cm.Spectral)
    # pyplot.show()


def load_planar_dataset():
    numpy.random.seed(1)
    m = 400  # number of examples
    N = int(m / 2)  # number of points per class
    D = 2  # dimensionality
    X = numpy.zeros((m, D))  # data matrix where each row is a single example
    Y = numpy.zeros((m, 1), dtype='uint8')  # labels vector (0 for red, 1 for blue)
    a = 4  # maximum ray of the flower

    for j in range(2):
        ix = range(N * j, N * (j + 1))
        t = numpy.linspace(j * 3.12, (j + 1) * 3.12, N) + numpy.random.randn(N) * 0.2  # theta
        r = a * numpy.sin(4 * t) + numpy.random.randn(N) * 0.2  # radius
        X[ix] = numpy.c_[r * numpy.sin(t), r * numpy.cos(t)]
        Y[ix] = j

    X = X.T
    Y = Y.T

    return X, Y


def load_extra_datasets():
    N = 200
    noisy_circles = sklearn.datasets.make_circles(n_samples=N, factor=.5, noise=.3)
    noisy_moons = sklearn.datasets.make_moons(n_samples=N, noise=.2)
    blobs = sklearn.datasets.make_blobs(n_samples=N, random_state=5, n_features=2, centers=6)
    gaussian_quantiles = sklearn.datasets.make_gaussian_quantiles(mean=None, cov=0.5, n_samples=N, n_features=2,
                                                                  n_classes=2, shuffle=True, random_state=None)
    no_structure = numpy.random.rand(N, 2), numpy.random.rand(N, 2)

    return noisy_circles, noisy_moons, blobs, gaussian_quantiles, no_structure
# your train set features # your train set labels # your test set features # your test set labels # the list of classes # Set min and max values and give it some padding # Generate a grid of points with distance h between them # Predict the function value for the whole grid # Plot the contour and training examples # pyplot.show() # number of examples # number of points per class # dimensionality # data matrix where each row is a single example # labels vector (0 for red, 1 for blue) # maximum ray of the flower # theta # radius