Example #1
0
def main():
    np.random.seed(1)   # 设置一个种子,使得结果是一致的

    # 导入花型的 2 类数据集
    # X: 输入数据集,维度为(输入特征的数量,样本数量)
    # Y: 标签数据集,维度为(输出特征的数量,样本数量)
    X, Y = load_planar_dataset()

    shape_X = X.shape
    shape_Y = Y.shape
    m = Y.shape[1]  # 训练样本数

    print("X 的维度:" + str(shape_X))
    print("Y 的维度:" + str(shape_Y))
    print("训练样本数:" + str(m))

    # 训练得到参数
    parameters = nn_model(X, Y, n_h = 4, num_iterations = 10000, print_cost = True)

    # 预测并绘制分类边界
    plot_decision_boundary(lambda x: predict(parameters, x.T), X, np.squeeze(Y))
    plt.title("Decision Boundary for hidden layer size " + str(4))
    plt.show()  # 否则不显示

    # 预测并打印准确率
    predictions = predict(parameters, X)
    print("准确率:%d" % float((np.dot(Y, predictions.T) + np.dot(1 - Y, 1 - predictions.T)) / float(Y.size) * 100) + '%')
Example #2
0
def loadData():
    np.random.seed(1)  # set a seed so that the results are consistent
    X, Y = load_planar_dataset()
    # plt.figure(1)
    # plt.scatter(X[0, :], X[1, :], c=Y, s=40, cmap=plt.cm.Spectral)
    #
    # shape_X = X.shape
    # shape_Y = Y.shape
    # m = shape_X[1]
    # # Train the logistic regression classifier
    #
    # clf = sklearn.linear_model.LogisticRegressionCV();
    # clf.fit(X.T, Y.T);
    #
    # # Plot the decision boundary for logistic regression
    # plot_decision_boundary(lambda x: clf.predict(x), X, Y)
    # plt.title("Logistic Regression")
    #
    # # Print accuracy
    # LR_predictions = clf.predict(X.T)
    # print ('Accuracy of logistic regression: %d ' % float(
    # (np.dot(Y, LR_predictions) + np.dot(1 - Y, 1 - LR_predictions)) / float(Y.size) * 100) +
    #    '% ' + "(percentage of correctly labelled datapoints)")
    # plt.show()

    return X, Y
Example #3
0
def load():
    X, Y = load_planar_dataset()
    name = 'flower'
    X = X.T
    Y = Y[0]
    # plt.scatter(X[:, 0], X[:, 1], c=Y , s=40, cmap=plt.cm.Spectral);
    # plt.title(name+'_original')
    # plt.savefig(os.path.join('pic', name+'_original'))
    # plt.show()



    # load dataset
    noisy_circles, noisy_moons, blobs, gaussian_quantiles, no_structure = load_extra_datasets()
    datasets = {"noisy_circles": noisy_circles,
                "noisy_moons": noisy_moons,
                "blobs": blobs,
                "gaussian_quantiles": gaussian_quantiles}

    datas = [(name, X, Y), ]
    for name, dataset in datasets.items():
        X, Y = datasets[name]
        datas.append((name, X, Y))

    #     print(X.shape)
    #     print(Y.shape)

        # Visualize the data
    #     plt.scatter(X[:, 0], X[:, 1], c=Y, s=40, cmap=plt.cm.Spectral)
    #     plt.title(name+'_original')
    #     plt.savefig(os.path.join('pic', name+'_original'))
    #     plt.show()
    return datas
Example #4
0
def runML():
    X, Y = load_planar_dataset()
    plt.scatter(X[0, :], X[1, :], c=Y.ravel(), s=40, cmap=plt.cm.Spectral)

    shape_X = X.shape
    shape_Y = Y.shape
    m = len(Y.T)  # training set size

    print('The shape of X is: ' + str(shape_X))
    print('The shape of Y is: ' + str(shape_Y))
    print('I have m = %d training examples!' % (m))

    clf = sklearn.linear_model.LogisticRegression()
    clf.fit(X.T, Y.T)

    # plot_decision_boundary(lambda x: clf.predict(x), X, Y.ravel())
    # plt.title("Logistic Regression")
    # plt.show(block=True)

    # Print accuracy
    LR_predictions = clf.predict(X.T)
    print('Accuracy of logistic regression: %d ' % float(
        (np.dot(Y, LR_predictions) + np.dot(1 - Y, 1 - LR_predictions)) /
        float(Y.size) * 100) + '% ' +
          "(percentage of correctly labelled datapoints)")

    print('\n')
Example #5
0
def main():
    X, Y = load_planar_dataset()
    # Visualize the data:
    # plt.scatter(X[0, :], X[1, :], c=Y, s=40, cmap=plt.cm.Spectral);

    shape_X = X.shape
    shape_Y = Y.shape
    m = Y.shape[1]  # training set size
    print('The shape of X is: ' + str(shape_X))
    print('The shape of Y is: ' + str(shape_Y))
    print('I have m = %d training examples!' % (m))
    # log_regression_case(X, Y)
    # test_layer_sizes()
    # test_init_params()
    # test_forward_prop()
    # test_compute_cost()
    # test_backward_prop()
    # test_update_params()
    # test_nn_model()
    # test_predict()

    # Build a model with a n_h-dimensional hidden layer
    parameters = nn_model(X, Y, n_h=4, num_iterations=10000, print_cost=True)
    # Plot the decision boundary
    plot_decision_boundary(lambda x: predict(parameters, x.T), X, Y)
    plt.title("Decision Boundary for hidden layer size " + str(4))
    # Print accuracy
    predictions = predict(parameters, X)
    print('Accuracy: %d' %
          float((np.dot(Y, predictions.T) + np.dot(1 - Y, 1 - predictions.T)) /
                float(Y.size) * 100) + '%')
Example #6
0
def play():
    X, Y = load_planar_dataset()
    # print(X)
    # print(Y)
    # Visualize the data:
    # plt.scatter(X[0, :], X[1, :], c=Y, s=40, cmap=plt.cm.Spectral);

    shape_X = X.shape
    shape_Y = Y.shape
    m = X.shape[1]
    print(shape_X)
    print(shape_Y)
    print(m)

    # Train the logistic regression classifier
    clf = sklearn.linear_model.LogisticRegressionCV()
    clf.fit(X.T, Y.T)
    # Plot the decision boundary for logistic regression
    plot_decision_boundary(lambda x: clf.predict(x), X, Y)
    plt.title("Logistic Regression")

    # Print accuracy
    LR_predictions = clf.predict(X.T)
    print('Accuracy of logistic regression: %d ' % float(
        (np.dot(Y, LR_predictions) + np.dot(1 - Y, 1 - LR_predictions)) /
        float(Y.size) * 100) + '% ' +
          "(percentage of correctly labelled datapoints)")
    def test_planar_dataset_2classification(self):
        """
        对于二分类问题, 检查损失(loss)是否随着迭代而不断降低

        :return:
        """

        X, Y = load_planar_dataset()

        X = X.T
        Y = Y.T.flatten()

        # Visualize the data:
        plt.scatter(X[:, 0], X[:, 1], c=Y, s=40, cmap=plt.cm.Spectral)

        # plt.show() # 展示图片(使用 jupyter notebook 不用指明需要显示)

        shape_X = X.shape
        shape_Y = Y.shape

        N = shape_X[1]  # training set size

        print('The shape of X is: ' + str(shape_X))
        print('The shape of Y is: ' + str(shape_Y))
        print('I have m = %d training examples!' % (N))

        clf = MLP_2Classifier(use_reg=0)
        clf.fit(X=X,
                y=Y,
                layers_dims=[2, 4, 1],
                learning_rate=1,
                max_iter=10000)
Example #8
0
def Test1():
    X, Y = load_planar_dataset()

    model = SingleHiddenLayer(4, 1.2)
    model.initializeData(X, Y)
    model.train_model(num_iterations=10000)
    predictions = model.predict(X)
    print('Accuracy: {0}%'.format(
        float((np.dot(Y, predictions.T) + np.dot(1 - Y, 1 - predictions.T)) /
              float(Y.size) * 100)))
    plot_decision_boundary(lambda x: model.predict(x.T), X, Y.ravel())
    plt.title('Decision boundary for hidden layer size: 5')
    plt.show()
Example #9
0
def main():
    X, Y = load_planar_dataset()
    plt.figure(figsize=(16, 32))
    hidden_layer_sizes = [1, 2, 3, 4, 5, 20, 50]
    for i, n_h in enumerate(hidden_layer_sizes):
        plt.subplot(5, 2, i + 1)
        plt.title("Hidden layer of size: %d" % n_h)
        parameters = nn_model(X, Y, n_h, num_iterations=5000)
        plot_decision_boundary(lambda x: predict(parameters, x.T), X, Y)
        predictions = predict(parameters, X)
        accuracy = np.sum(predictions == Y) / float(Y.shape[1]) * 100.0
        print("Accuracy for {} hidden units: {}%".format(n_h, accuracy))
    plt.show()
Example #10
0
def runNN():
    X, Y = load_planar_dataset()
    plt.figure(figsize=(16, 32))
    hidden_layer_sizes = [1, 2, 3, 4, 5, 20, 50]
    for i, n_h in enumerate(hidden_layer_sizes):
        plt.subplot(5, 2, i + 1)
        plt.title('Hidden Layer of size %d' % n_h)
        parameters = nn_model(X, Y, n_h, num_iterations=5000)
        plot_decision_boundary(lambda x: predict(parameters, x.T), X,
                               Y.ravel())
        predictions = predict(parameters, X)
        accuracy = float(
            (np.dot(Y, predictions.T) + np.dot(1 - Y, 1 - predictions.T)) /
            float(Y.size) * 100)
        print("Accuracy for {} hidden units: {} %".format(n_h, accuracy))
    plt.show(block=True)
Example #11
0
 def __init__(self):
     self.X, self.Y = load_planar_dataset()
     self.sample_amount = self.X.shape[1]
     self.n_x = self.X.shape[0]
     self.n_h = 4
     self.n_y = self.Y.shape[0]
     np.random.seed(2)
     self.W1 = np.random.randn(self.n_h, self.n_x) * 0.01  # (后一层,前一层)
     print self.W1
     self.b1 = np.zeros((self.n_h, 1))
     self.W2 = np.random.randn(self.n_y, self.n_h) * 0.01
     print self.W2
     self.b2 = np.zeros((self.n_y, 1))
     self.A1 = np.zeros((self.n_h, self.sample_amount))
     self.A2 = np.zeros((self.n_y, self.sample_amount))
     self.current_cost = 0
     self.is_trained = False
Example #12
0
def baseline_lr():

    X, Y = load_planar_dataset()

    # 1. baseline: logistic regression
    # Train the logistic regression classifier
    clf = sklearn.linear_model.LogisticRegressionCV()
    clf.fit(X.T, Y.T)

    # Plot the decision boundary for logistic regression
    plot_decision_boundary(lambda x: clf.predict(x), X, Y)
    plt.title("Logistic Regression")

    # Print accuracy
    LR_predictions = clf.predict(X.T)
    print('Accuracy of logistic regression: %d ' % float(
        (np.dot(Y, LR_predictions) + np.dot(1 - Y, 1 - LR_predictions)) /
        float(Y.size) * 100) + '% ' +
          "(percentage of correctly labelled datapoints)")
def main():
    """
        参数:
        返回值:
    """
    # 加载数据
    train_x, train_y, test_x, test_y = planar_utils.load_planar_dataset()
    # 训练模型
    parameters = nn_model(train_x, train_y, n_h=4, num_iterations=10000, print_cost=True)
    # 预测训练集
    predictions = predict(parameters, train_x)
    # 输出准确率
    print('Train Accuracy: %d' % float((np.dot(train_y, predictions.T) +
                                        np.dot(1 - train_y, 1 - predictions.T)) /
                                       float(train_y.size) * 100) + '%')
    # 预测测试集
    predictions = predict(parameters, test_x)
    print('Test Accuracy: %d' % float((np.dot(test_y, predictions.T) +
                                       np.dot(1 - test_y, 1 - predictions.T)) /
                                      float(test_y.size) * 100) + '%')
Example #14
0
def load_data():
    """
    载入数据,数据项包括:
        train_set_x_orig:原始训练数据集
        train_set_y:原始训练数据标签
        test_set_x_orig:原始测试数据集
        test_set_y:原始测试数据标签
        classes(cat/non-cat):分类list

    Args:
    Return:
    """
    global TRAINING_SET, TEST_SET, DATADIM

    train_set_x, train_set_y, test_set_x, test_set_y = planar_utils.load_planar_dataset()

    # 定义纬度
    DATADIM = 2

    TRAINING_SET = np.hstack((train_set_x.T, train_set_y.T))
    TEST_SET = np.hstack((test_set_x.T, test_set_y.T))
Example #15
0
def LogisticRegression():
    # - a numpy-array (matrix) X that contains your features (x1, x2)
    # - a numpy-array (vector) Y that contains your labels (red:0, blue:1).
    X, Y = load_planar_dataset()

    # Visualize the data:
    # plt.scatter 画散列点图,参数c指定类别label,
    # cmap = plt.cm.Spectral实现的功能是给label为1的点一种颜色,给label为0的点另一种颜色。
    plt.scatter(X[0, :], X[1, :], c=Y[0, :], s=40, cmap=plt.cm.Spectral)
    plt.show()

    ### START CODE HERE ### (≈ 3 lines of code)
    shape_X = X.shape
    shape_Y = Y.shape
    m = Y.shape[1]  # training set size
    ### END CODE HERE ###

    print('The shape of X is: ' + str(shape_X))
    print('The shape of Y is: ' + str(shape_Y))
    print('I have m = %d training examples!' % (m))

    # Simple Logistic Regression
    # Train the logistic regression classifier
    # sklearn's built-in functions
    clf = sklearn.linear_model.LogisticRegressionCV()
    clf.fit(X.T, Y.T)

    # Plot the decision boundary for logistic regression
    # plot_decision_boundary在planar_utils.py里
    plot_decision_boundary(lambda x: clf.predict(x), X, Y)
    plt.title("Logistic Regression")

    # Print accuracy
    LR_predictions = clf.predict(X.T)
    print('Accuracy of logistic regression: %d ' % float(
        (np.dot(Y, LR_predictions) + np.dot(1 - Y, 1 - LR_predictions)) /
        float(Y.size) * 100) + '% ' +
          "(percentage of correctly labelled datapoints)")
def main():
    X, Y = load_planar_dataset()

    #hyper-parameters
    num_hidden_units = 4
    num_iterations = 10000
    learning_rate = 1.2

    learner_parameters = nn_model(X,
                                  Y,
                                  num_hidden_units,
                                  num_iterations,
                                  learning_rate,
                                  print_cost=True)

    predictions = predict(learner_parameters, X)
    print('Accuracy: %d' %
          float((np.dot(Y, predictions.T) + np.dot(1 - Y, 1 - predictions.T)) /
                float(Y.size) * 100) + '%')

    plot_decision_boundary(lambda x: predict(learner_parameters, x.T), X, Y)
    plt.title("Decision Boundary for hidden layer size " + str(4))
    plt.show()
import numpy as np  # fundamental package for scientific computing with Python
import matplotlib.pyplot as plt  # a library for plotting graphs in Python
from testCases_v2 import *  # provides some test examples
import sklearn  # provides simple and efficent tools for data mining and data analysis
import sklearn.datasets
import sklearn.linear_model
from planar_utils import plot_decision_boundary, sigmoid, load_planar_dataset, load_extra_datasets  # provides various usefull functions used in this assignment

np.random.seed(1)  # set a seed so that the results are consistent

X, Y = load_planar_dataset(
)  # will load "flower" 2-class dataset into variables X and Y
plt.scatter(X[0, :], X[1, :], c=Y.ravel(), s=40, cmap=plt.cm.Spectral)
# Returns a contiguous flattened array
plt.show()

# - a numpy-array (matrix) X that contains your features (x1, x2)
# - a numpy-array (vector) Y that contains your labels (red:0, blue:1)

shape_X = X.shape
shape_Y = Y.shape
m = X.shape[1]

print("The shape of X is: " + str(shape_X))
print("The shape of Y is: " + str(shape_Y))
print("I have m = %d training examples!" % (m))

# Before building a full neural network, lets first see how logistic regression performs on this problem.
# You can use sklearn's built-in functions to do that. Run the code below to train a logistic regression classifier on the dataset.

# Train the logistic regression classifier
    Returns
    predictions -- vector of predictions of our model (red: 0 / blue: 1)
    """
    
    A2, cache = forward_propagation(X, parameters)
    predictions = (A2 > 0.5)
    
    return predictions

while True:
    print("Select the data you want:")
    print("0. Flower \n1. Noisy Circles \n2. Noisy Moons \n3. Blobs \n4. Gaussian Quantiles\n5. Testing with differend hidden neurons")
    i = int(input("Enter no: "))
    noisy_circles, noisy_moons, blobs, gaussian_quantiles = load_extra_datasets()
    if i != 5:
        datasets = [load_planar_dataset(), noisy_circles, noisy_moons, blobs, gaussian_quantiles]
        X, Y = datasets[i]
    
        if i >0:
            X, Y = X.T, Y.reshape(1, Y.shape[0])
        if i == 3:
            Y = Y%2
        plt.scatter(X[0, :], X[1, :], c=np.squeeze(Y), s=40, cmap=plt.cm.Spectral)
        plt.show()
        n_h = int(input("Enter number of hidden layers: "))
        num_iterations = int(input("Enter number of iterations"))
        parameters = nn_model(X, Y, n_h, num_iterations, print_cost=True)
        plot_decision_boundary(lambda x: predict(parameters, x.T), X, np.squeeze(Y))
        predictions = predict(parameters, X)
        accuracy = float((np.dot(Y,predictions.T) + np.dot(1-Y,1-predictions.T))/float(Y.size)*100)
        plt.title("Accuracy for {} hidden units: {} %".format(n_h, accuracy))
def load_data():
    global TRAINING_DATA, TEST_DATA_X, TEST_DATA_Y, TEST_DATA
    X, Y = load_planar_dataset()
    TRAINING_DATA = np.hstack([X.T, Y.T])
Example #20
0
@author: Francis Chen
"""

# Package Area
import numpy as np
import matplotlib.pyplot as plt
from testCases import *
import sklearn
import sklearn.datasets
import sklearn.linear_model
from planar_utils import plot_decision_boundary, sigmoid, load_planar_dataset, load_extra_datasets

np.random.seed(1)

# Here comes the Function
X, Y = load_planar_dataset()  # X is point and Y is label

plt.scatter(X[0, :], X[1, :], c=Y, s=40, cmap=plt.cm.Spectral)

shape_X = X.shape  # The raw data
shape_Y = Y.shape  # The label of data
m = Y.shape[1]  # Total Data number, in here is 400

print("The dimension for X is " + str(shape_X))
print("THe dimension for Y is " + str(shape_Y))
print("The dataset has total data " + str(m))

clf = sklearn.linear_model.LogisticRegressionCV()
clf.fit(X.T, Y.T)

plot_decision_boundary(lambda x: clf.predict(x), X, Y)
import numpy as np
import matplotlib.pyplot as plt
from testCases import *  # 提供了一些测试示例来评估函数的正确性
import sklearn  # sklearn:为数据挖掘和数据分析提供的简单高效的工具。
import sklearn.datasets
import sklearn.linear_model
from planar_utils import plot_decision_boundary, sigmoid, load_planar_dataset, load_extra_datasets
# 提供了在这个任务中使用的各种有用的功能

np.random.seed(1)  # 设置一个固定的随机种子,以保证接下来的步骤中我们的结果是一致的。
# 只要random.seed( * ) seed里面的值一样,那随机出来的结果就一样。
# seed的作用是让随机结果可重现

#==================================Load and Visualize Dataset===============================

X, Y = load_planar_dataset()  # 生成400个样本集,正负样本各一半
plt.scatter(X[0, :], X[1, :], c=np.squeeze(Y), s=40,
            cmap=plt.cm.Spectral)  # 绘制散点图
# c-标记颜色
# s-标记大小
# cmap-色彩盘,实际上就是一个三列的矩阵(shape 为 [N,3]的array),每一行代表一个颜色(RGB)
# np.squeeze(Y)生成的(400,)是秩为1的数组,(1,400)是一个二维数组,它们是不匹配的
plt.show()
# plt.imshow()函数负责对图像进行处理,并显示其格式
# plt.show()则是将plt.imshow()处理后的函数显示出来

shape_X = X.shape
shape_Y = Y.shape
m = shape_Y[1]

print('The shape of X is: ' + str(shape_X))
Example #22
0
def main():
    np.random.seed(1)  # set a seed so that the results are consistent

    X, Y = load_planar_dataset()
    shape_X = X.shape
    shape_Y = Y.shape
    m = X.shape[1]  # training set size
    print('The shape of X is: {}'.format(shape_X))
    print('The shape of Y is: {}'.format(shape_Y))
    print('I have m = {} training examples!'.format(m))

    # Visualize the data:
    plt.figure()
    plt.scatter(X[0, :],
                X[1, :],
                c=Y.ravel(),
                s=40,
                cmap=plt.cm.get_cmap('Spectral'))

    # Train the logistic regression classifier
    clf = sklearn.linear_model.LogisticRegression(solver='lbfgs')
    clf.fit(X.T, Y.flatten())

    # Plot the decision boundary for logistic regression
    plt.figure()
    plot_decision_boundary(lambda x: clf.predict(x), X, Y)
    plt.title("Logistic Regression")

    # Print accuracy
    LR_predictions = clf.predict(X.T)
    print(
        'Accuracy of logistic regression: {}% (percentage of correctly labelled datapoints)'
        .format(
            float(
                (np.dot(Y, LR_predictions) + np.dot(1 - Y, 1 - LR_predictions))
                / float(Y.size) * 100)))

    # Build a model with a n_h-dimensional hidden layer
    parameters = nn_model(X, Y, n_h=4, num_iterations=10000, print_cost=True)

    # Plot the decision boundary
    plt.figure()
    plot_decision_boundary(lambda x: predict(parameters, x.T), X, Y)
    plt.title('Decision Boundary for hidden layer size {}'.format(4))

    # Print accuracy
    predictions = predict(parameters, X)
    print('Accuracy: {}%'.format(
        float((np.dot(Y, predictions.T) + np.dot(1 - Y, 1 - predictions.T)) /
              float(Y.size) * 100)))

    # # Tuning hidden layer size
    # plt.figure(figsize=(16, 32))
    # hidden_layer_sizes = [1, 2, 3, 4, 5, 20, 50]
    # for i, n_h in enumerate(hidden_layer_sizes):
    #     plt.subplot(5, 2, i+1)
    #     plt.title('Hidden Layer of size {}'.format(n_h))
    #     parameters = nn_model(X, Y, n_h, num_iterations = 5000)
    #     plot_decision_boundary(lambda x: predict(parameters, x.T), X, Y)
    #     predictions = predict(parameters, X)
    #     accuracy = float((np.dot(Y,predictions.T) + np.dot(1-Y,1-predictions.T))/float(Y.size)*100)
    #     print ("Accuracy for {} hidden units: {} %".format(n_h, accuracy))

    # # Performance on other datasets
    # noisy_circles, noisy_moons, blobs, gaussian_quantiles, no_structure = load_extra_datasets()
    # datasets = {"noisy_circles": noisy_circles,
    #             "noisy_moons": noisy_moons,
    #             "blobs": blobs,
    #             "gaussian_quantiles": gaussian_quantiles}

    # dataset = "noisy_moons"
    # X, Y = datasets[dataset]
    # X, Y = X.T, Y.reshape(1, Y.shape[0])

    # # make blobs binary
    # if dataset == "blobs":
    #     Y = Y%2

    # # Visualize the data
    # plt.figure()
    # plt.scatter(X[0, :], X[1, :], c=Y.ravel(), s=40, cmap=plt.cm.get_cmap('Spectral'))

    # # Build a model with a n_h-dimensional hidden layer
    # parameters = nn_model(X, Y, n_h = 4, num_iterations = 10000, print_cost=True)

    # # Plot the decision boundary with other datasets
    # plt.figure()
    # plot_decision_boundary(lambda x: predict(parameters, x.T), X, Y)
    # plt.title('Decision Boundary for hidden layer size {}'.format(4))

    # # Print accuracy
    # predictions = predict(parameters, X)
    # print ('Accuracy: {}%'.format(float((np.dot(Y,predictions.T) + np.dot(1-Y,1-predictions.T))/float(Y.size)*100)))

    plt.show()
# # Planar data classification with one hidden layer

import matplotlib.pyplot as plt
import sklearn.linear_model
from planar_utils import plot_decision_boundary, load_planar_dataset
from assignment02_utils import *

np.random.seed(1)  # set a seed so that the results are consistent
# import pdb; pdb.set_trace

# ========================================================
# Load and visualise the data

# load the data
X, Y = load_planar_dataset()

# plot a scatter of the data
plt.scatter(X[0, :], X[1, :], c=Y, s=40, cmap=plt.cm.Spectral)
plt.show()

shape_X = X.shape
shape_Y = Y.shape
m = shape_X[1]  # training set size

print ('The shape of X is: ' + str(shape_X))
print ('The shape of Y is: ' + str(shape_Y))
print ('I have m = %d training examples!' % (m))

# ========================================================
# Simple Logistic Regression
# Package imports
import numpy as np
import matplotlib.pyplot as plt
from testCases_v2 import *
import sklearn
import sklearn.datasets
import sklearn.linear_model
from planar_utils import plot_decision_boundary, sigmoid, load_planar_dataset, load_extra_datasets
from funcs import *

np.random.seed(1)  # set a seed so that the results are consistent
""" 
2 - Dataset
"""
X, Y = load_planar_dataset()  # X: 2 x 400 , Y: 1 x 400
# Visualize the data:
plt.scatter(X[0, :], X[1, :], c=np.reshape(Y, -1), s=40, cmap=plt.cm.Spectral)

### START CODE HERE ### (≈ 3 lines of code)
shape_X = X.shape
shape_Y = Y.shape
m = X.shape[1]  # training set size
### END CODE HERE ###

print('The shape of X is: ' + str(shape_X))
print('The shape of Y is: ' + str(shape_Y))
print('I have m = %d training examples!' % (m))
""" 
3 - Simple Logistic Regression
 """
# Train the logistic regression classifier
Example #25
0
import numpy as np
import matplotlib.pyplot as plt
from testCases_v2 import *
import sklearn
import sklearn.datasets
import sklearn.linear_model
from planar_utils import plot_decision_boundary, sigmoid, load_planar_dataset, load_extra_datasets

%matplotlib inline

np.random.seed(1) # set a seed so that the results are consistent

X, Y = load_planar_dataset() # Load dataset

# Visualize the data:
plt.scatter(X[0, :], X[1, :], c=Y, s=40, cmap=plt.cm.Spectral);


#Shape of X, Y and the size of the training data set

shape_X = X.shape
shape_Y = Y.shape
m = X.shape[1]  # training set size

# Train the logistic regression classifier
clf = sklearn.linear_model.LogisticRegressionCV();
clf.fit(X.T, Y.T);

# Plot the decision boundary for logistic regression
plot_decision_boundary(lambda x: clf.predict(x), X, Y)
import matplotlib.pyplot as plt
from testCases_v2 import * 
import sklearn                      #provides simple and efficient tools for data mining and data analysis
import sklearn.datasets
import sklearn.linear_model
from planar_utils import plot_decision_boundary, sigmoid, load_planar_dataset, load_extra_datasets # various various useful functions needed here

get_ipython().magic('matplotlib inline')

np.random.seed(1) # set a seed so that the results are consistent


# ## 2 - Dataset ##
# 

X, Y = load_planar_dataset() # Obtain the data set, which will load a flow 2-class data set into variables 'X' and 'Y'


# Visualize the dataset using matplotlib. The data looks like a "flower" with some red (label y=0) and some blue (y=1) points.
# Goal is to build a model to fit this data. In other words, want the classifier to define regions as either red or blue.


# Visualize the data:
plt.scatter(X[0, :], X[1, :], c=Y, s=40, cmap=plt.cm.Spectral);


# Have:
#     - a numpy-array (matrix) X that contains your features (x1, x2)
#     - a numpy-array (vector) Y that contains your labels (red:0, blue:1).
# 
# To get the shape of a numpy array: [(help)](https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.shape.html)
Example #27
0
            self.dz[self.l - 1] = self.a[self.l - 1] - y  # 确定最后一层是sigmoid
            self.dw[self.l - 1] = 1.0 / m * np.dot(self.dz[self.l - 1],
                                                   self.a[self.l - 2].T)
            self.db[self.l - 1] = 1.0 / m * np.sum(
                self.dz[self.l - 1], axis=1, keepdims=True)
            self.da[self.l - 2] = np.dot(self.w[self.l - 1].T,
                                         self.dz[self.l - 1])
            for i in range(self.l - 2, -1, -1):  # 1 0
                self.backPpRelu(i)

            #print(self.w)
            self.update(lr)

    def predict(self, x):
        for i in range(self.l - 1):  # 0,1
            self.forPpRelu(i, x)
        self.forPpSig(self.l - 1)  # 2
        return np.where(self.a[self.l - 1] > 0.5, 1, 0)


x, y = load_planar_dataset()
y = y.ravel()
para = [4, 5, 1]
dnn = deepNN()
dnn.train(x, y, 10000, 0.8)
yhat = dnn.predict(x)
print(yhat.shape)

print("precision of nn:" + str(100 - (np.sum(np.abs(yhat - y))) / 4) + "%")
plot_decision_boundary(lambda x: dnn.predict(x.T), x, y)
plt.show()
Example #28
0
    # 计算预测的精度
    y_c = np.where(y_c > 0.5, 1, 0)
    # y_c = np.round(y_c)
    return np.mean(y == y_c)


def predict(n_n, x):
    y = n_n.predict(x)
    y = np.where(y > 0.5, 1, 0)

    return y


if __name__ == '__main__':
    np.seterr(all='raise')  # numpy设置异常
    origin_x, origin_y = load_planar_dataset()
    train_x, train_y, test_x, test_y = split_train_test(
        origin_x, origin_y, 0.3)
    layers = (
        Layer(7, origin_x.shape[0], g=ReLu, alpha=0.3),
        # Layer(6, 5, g=ReLu, alpha=0.3, ),
        # Layer(7, 6, g=ReLu, alpha=0.3, ),
        Layer(
            8,
            7,
            g=tanh,
            alpha=0.3,
        ),
        Layer(1, 8, g=sigmoid, alpha=0.3, op=True),
    )
    nn = NN(origin_x, origin_y, layers, num_iterations=10000)
Example #29
0
def do_something():
    X, Y = load_planar_dataset()
    # Visualize the data:
    plt.scatter(X[0, :], X[1, :], c=Y.ravel(), s=40, cmap=plt.cm.Spectral);

    ### Shapes
    ### START CODE HERE ### (≈ 3 lines of code)
    shape_X = X.shape
    shape_Y = Y.shape
    m =  Y.size # training set size
    ### END CODE HERE ###

    print ('The shape of X is: ' + str(shape_X))
    print ('The shape of Y is: ' + str(shape_Y))
    print ('I have m = %d training examples!' % (m))

    ### Logistic Regression
    # Train the logistic regression classifier
    clf = sklearn.linear_model.LogisticRegressionCV()
    clf.fit(X.T, Y.T)
    # Plot the decision boundary for logistic regression
    plot_decision_boundary(lambda x: clf.predict(x), X, Y)
    plt.title("Logistic Regression")

    # Print accuracy
    LR_predictions = clf.predict(X.T)
    print ('Accuracy of logistic regression: %d ' % float((np.dot(Y,LR_predictions) + np.dot(1-Y,1-LR_predictions))/float(Y.size)*100) +
           '% ' + "(percentage of correctly labelled datapoints)")
    # plt.show()

    ### Layer sizes
    X_assess, Y_assess = layer_sizes_test_case()
    (n_x, n_h, n_y) = layer_sizes(X_assess, Y_assess)
    print("The size of the input layer is: n_x = " + str(n_x))
    print("The size of the hidden layer is: n_h = " + str(n_h))
    print("The size of the output layer is: n_y = " + str(n_y))

    ### Initialize parameters
    n_x, n_h, n_y = initialize_parameters_test_case()

    parameters = initialize_parameters(n_x, n_h, n_y)
    print("W1 = " + str(parameters["W1"]))
    print("b1 = " + str(parameters["b1"]))
    print("W2 = " + str(parameters["W2"]))
    print("b2 = " + str(parameters["b2"]))

    ## The Loop
    ### Forward propagation
    X_assess, parameters = forward_propagation_test_case()

    A2, cache = forward_propagation(X_assess, parameters)

    # Note: we use the mean here just to make sure that your output matches ours.
    print(np.mean(cache['Z1']) ,np.mean(cache['A1']),np.mean(cache['Z2']),np.mean(cache['A2']))

    ### Cost function
    A2, Y_assess, parameters = compute_cost_test_case()

    print("cost = " + str(compute_cost(A2, Y_assess, parameters)))

    ### Backward propagation
    parameters, cache, X_assess, Y_assess = backward_propagation_test_case()

    grads = backward_propagation(parameters, cache, X_assess, Y_assess)
    print ("dW1 = "+ str(grads["dW1"]))
    print ("db1 = "+ str(grads["db1"]))
    print ("dW2 = "+ str(grads["dW2"]))
    print ("db2 = "+ str(grads["db2"]))

    ### Update parameters

    parameters, grads = update_parameters_test_case()
    parameters = update_parameters(parameters, grads)

    print("W1 = " + str(parameters["W1"]))
    print("b1 = " + str(parameters["b1"]))
    print("W2 = " + str(parameters["W2"]))
    print("b2 = " + str(parameters["b2"]))

    ## End of Loop

    ## Integrate parts 4.1, 4.2 and 4.3 in nn_model()
    X_assess, Y_assess = nn_model_test_case()

    parameters = nn_model(X_assess, Y_assess, 4, num_iterations=10000, print_cost=False)
    print("W1 = " + str(parameters["W1"]))
    print("b1 = " + str(parameters["b1"]))
    print("W2 = " + str(parameters["W2"]))
    print("b2 = " + str(parameters["b2"]))

    ## Predictions
    parameters, X_assess = predict_test_case()

    predictions = predict(parameters, X_assess)
    print("predictions mean = " + str(np.mean(predictions)))

    ## Neural network
    # Build a model with a n_h-dimensional hidden layer
    parameters = nn_model(X, Y, n_h = 4, num_iterations = 10000, print_cost=True)

    # Plot the decision boundary
    plot_decision_boundary(lambda x: predict(parameters, x.T), X, Y)
    plt.title("Decision Boundary for hidden layer size " + str(4))

    # Print accuracy
    predictions = predict(parameters, X)
    print ('Accuracy: %d' % float((np.dot(Y,predictions.T) + \
        np.dot(1-Y,1-predictions.T))/float(Y.size)*100) + '%')

    ## Tuning hidden layer size (optional)
    # This may take about 2 minutes to run
    plt.figure(figsize=(16, 32))
    hidden_layer_sizes = [1, 2, 3, 4, 5, 20, 50]
    for i, n_h in enumerate(hidden_layer_sizes):
        plt.subplot(5, 2, i+1)
        plt.title('Hidden Layer of size %d' % n_h)
        parameters = nn_model(X, Y, n_h, num_iterations = 5000)
        plot_decision_boundary(lambda x: predict(parameters, x.T), X, Y)
        predictions = predict(parameters, X)
        accuracy = float((np.dot(Y,predictions.T) + np.dot(1-Y,1-predictions.T))/float(Y.size)*100)
        print ("Accuracy for {} hidden units: {} %".format(n_h, accuracy))

    ## Performance on other datasets
    # Datasets
    noisy_circles, noisy_moons, blobs, gaussian_quantiles, no_structure = load_extra_datasets()

    datasets = {"noisy_circles": noisy_circles,
                "noisy_moons": noisy_moons,
                "blobs": blobs,
                "gaussian_quantiles": gaussian_quantiles}

    ### START CODE HERE ### (choose your dataset)
    dataset = "noisy_moons"
    ### END CODE HERE ###

    X, Y = datasets[dataset]
    X, Y = X.T, Y.reshape(1, Y.shape[0])

    # make blobs binary
    if dataset == "blobs":
        Y = Y%2

    # Visualize the data
    plt.scatter(X[0, :], X[1, :], c=Y.ravel(), s=40, cmap=plt.cm.Spectral)
    plt.show()
Example #30
0
'''
# Package imports
import numpy as np
import matplotlib.pyplot as plt
from testCases_v2 import *
import sklearn
import sklearn.datasets
import sklearn.linear_model
from planar_utils import plot_decision_boundary, sigmoid, load_planar_dataset, load_extra_datasets

#%matplotlib inline

np.random.seed(1) # set a seed so that the results are consistent

#loading dataset
X, Y = load_planar_dataset()

#Visualize the data:
plt.scatter(X[0, :], X[1, :], c=Y, s=40, cmap=plt.cm.Spectral);


#How many training examples do you have
shape_X = X.shape
shape_Y = Y.shape
m = shape_X[1]  # training set size

print ('The shape of X is: ' + str(shape_X))
print ('The shape of Y is: ' + str(shape_Y))
print ('I have m = %d training examples!' % (m))

########## first simple logistic regression
    np.random.seed(1)  # set a seed so that the results are consistent
    imageNp = imgNp.getImageNpArray('classification_kiank.png')
    print(imageNp.shape)
    imgNp.drawImageWithNpArray(imageNp)

    imageNp = imgNp.getImageNpArray('grad_summary.png')
    imgNp.drawImageWithNpArray(imageNp)

    imageNp = imgNp.getImageNpArray('sgd.gif')
    imgNp.drawImageWithNpArray(imageNp)

    imageNp = imgNp.getImageNpArray('sgd_bad.gif')
    imgNp.drawImageWithNpArray(imageNp)

    X, Y = planarUtils.load_planar_dataset()
    n_x, n_h, n_y = testCases.initialize_parameters_test_case()

    try:
        print(
            "layer 1 is the first layer, it inputs X (x1 and x2) and outputs A1."
        )
        print("layer 2 is the output layer, it inputs A1 and outputs A2")
        n_h = int(
            input('Give me the number of hiddent units for the layer 1:'))
        num_iterations = int(input('Give me the number of iterations:'))
    except ValueError:
        print("This is not a number.")
        quit()

    print("\n------------Start------------")