栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

鸢尾属植物数据集神经网络模型【C4-神经网络学习1】

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

鸢尾属植物数据集神经网络模型【C4-神经网络学习1】

import os
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

from sklearn.linear_model import LogisticRegression

####
# =====================111准备数据集===========================
####

os.chdir('D:\')
# 读文件数据
iris = pd.read_csv('iris_training.csv')
# Create numeric classes for species (0,1,2)
# 0-setosa-RED
# 1-versicolor-YELLOW
# 2-virginica-BLUE
# Create Input and Output columns
X = iris[['PetalLength', 'PetalWidth']].values.T
Y = iris[['species']].values.T
Y = Y.astype('uint8')
# 设置几个空列表用来绘图
length = len(iris)
SET_l = []
SET_w = []
VER_l = []
VER_w = []
VIR_l = []
VIR_w = []
# 得到不同种类的属性列表
for i in range(length):
    if Y[0, i] == 0:
        SET_l.append(X[0, i])
        SET_w.append(X[1, i])
    else:
        if Y[0, i] == 1:
            VER_l.append(X[0, i])
            VER_w.append(X[1, i])
        else:
            if Y[0, i] == 2:
                VIR_l.append(X[0, i])
                VIR_w.append(X[1, i])
# SET = list(zip(SET_l,SET_w))
# VER = list(zip(VER_l,VER_w))
# VIR = list(zip(VIR_l,VIR_w))
# 绘制散点图
plt.scatter(SET_l, SET_w)
plt.scatter(VER_l, VER_w)
plt.scatter(VIR_l, VIR_w)
plt.title("IRIS DATA ")
plt.xlabel('Petal Length')
plt.ylabel('Petal Width')
plt.legend(["Setosa", "Versicolor", "Virginica"])
plt.show()


# 第二步:初始化参数

####
# =========【改动为:添加assert】============222初始化参数(权重和参数===========================
####
def initialize_parameters(n_x, n_h, n_y):
    # 随机初始化
    np.random.seed(2)  # 生成随机数种子
    # 随机初始化w之后,再乘一个比较小的数,比如0.01
    W1 = np.random.randn(n_h, n_x) * 0.01  # weight matrix of shape (n_h, n_x)
    b1 = np.zeros(shape=(n_h, 1))  # bias vector of shape (n_h, 1)
    W2 = np.random.randn(n_y, n_h) * 0.01  # weight matrix of shape (n_y, n_h)
    b2 = np.zeros(shape=(n_y, 1))  # bias vector of shape (n_y, 1)

    assert (W1.shape == (n_h, n_x))
    assert (b1.shape == (n_h, 1))
    assert (W2.shape == (n_y, n_h))
    assert (b2.shape == (n_y, 1))

    # store parameters into a dictionary
    parameters = {"W1": W1,
                  "b1": b1,
                  "W2": W2,
                  "b2": b2}
    return parameters


####
# ==========【无改动】===========333前向传播(forward propagation)===========================
####
def forward_propagation(X, parameters):
    # retrieve intialized parameters from dictionary
    W1 = parameters['W1']
    b1 = parameters['b1']
    W2 = parameters['W2']
    b2 = parameters['b2']

    # Implement Forward Propagation to calculate A2 (probability)
    Z1 = np.dot(W1, X) + b1
    A1 = np.tanh(Z1)  # tanh activation function
    Z2 = np.dot(W2, A1) + b2
    A2 = 1 / (1 + np.exp(-Z2))  # sigmoid activation function
    assert (A2.shape == (1, X.shape[1]))
    cache = {"Z1": Z1,
             "A1": A1,
             "Z2": Z2,
             "A2": A2}

    return A2, cache


####
# =======【改动为:精度调整】=============444计算代价函数(cost function)===========================
####
def compute_cost(A2, Y, parameters):
    m = Y.shape[1]  # number of training examples
    # Compute the cross-entropy cost
    # 此处逻辑回归时由于数据精度错误导致数据溢出,改精度为1e-6
    logprobs = np.log(A2 + 1e-6) * Y + (1 - Y) * np.log(1 - A2 + 1e-6)
    cost = -1. / m * np.sum(logprobs)
    cost = np.squeeze(cost)
    assert (isinstance(cost, float))

    return cost


####
# ========【改动为:将1改为1.->浮点数运算】=============555反向传播(back propagation)===========================
####
def backward_propagation(parameters, cache, X, Y):
    # Number of training examples
    m = X.shape[1]

    # First, retrieve W1 and W2 from the dictionary "parameters".
    W1 = parameters['W1']
    W2 = parameters['W2']
    #  END CODE HERE ###

    # Retrieve A1 and A2 from dictionary "cache".
    A1 = cache['A1']
    A2 = cache['A2']

    # Backward propagation: calculate dW1, db1, dW2, db2.
    dZ2 = A2 - Y
    dW2 = (1. / m) * np.dot(dZ2, A1.T)
    db2 = (1. / m) * np.sum(dZ2, axis=1, keepdims=True)
    dZ1 = np.multiply(np.dot(W2.T, dZ2), 1 - np.power(A1, 2))
    dW1 = (1. / m) * np.dot(dZ1, X.T)
    db1 = (1. / m) * np.sum(dZ1, axis=1, keepdims=True)

    grads = {"dW1": dW1,
             "db1": db1,
             "dW2": dW2,
             "db2": db2}

    return grads


####
# =========【无改动】===========666更新参数===========================
####
def update_parameters(parameters, grads, learning_rate=1.2):
    # Retrieve each parameter from the dictionary "parameters"
    W1 = parameters['W1']
    b1 = parameters['b1']
    W2 = parameters['W2']
    b2 = parameters['b2']

    # Retrieve each gradient from the dictionary "grads"
    dW1 = grads['dW1']
    db1 = grads['db1']
    dW2 = grads['dW2']
    db2 = grads['db2']

    # Update rule for each parameter
    W1 = W1 - learning_rate * dW1
    b1 = b1 - learning_rate * db1
    W2 = W2 - learning_rate * dW2
    b2 = b2 - learning_rate * db2

    parameters = {"W1": W1,
                  "b1": b1,
                  "W2": W2,
                  "b2": b2}

    return parameters


####
# ==========【改动:添加plot_cost函数】===========777建立神经网络===========================
####

# add
def plot_cost(cost_list):
    plt.figure()
    plt.plot(cost_list[:, 1], cost_list[:, 0], color='blue', linewidth=2)
    plt.xlabel('run iterations')
    plt.ylabel('cost')
    plt.ylim([0.0, 2.0])
    plt.ylim([0, 10000])
    plt.show()


def layer_sizes(X, Y):
    n_x = X.shape[0]
    n_h = 6
    n_y = Y.shape[0]
    return n_x, n_h, n_y


def nn_model(X, Y, n_h, num_iterations=10000, print_cost=False):
    np.random.seed(3)
    n_x = layer_sizes(X, Y)[0]
    n_y = layer_sizes(X, Y)[2]

    # Initialize parameters, then retrieve W1, b1, W2, b2. Inputs: "n_x, n_h, n_y". Outputs = "W1, b1, W2, b2,
    parameters = initialize_parameters(n_x, n_h, n_y)
    W1 = parameters['W1']
    b1 = parameters['b1']
    W2 = parameters['W2']
    b2 = parameters['b2']

    # Loop (gradient descent)
    cost_iter_list = []

    for i in range(0, num_iterations):

        # Forward propagation. Inputs: "X, parameters". Outputs: "A2, cache".
        A2, cache = forward_propagation(X, parameters)

        # Cost function. Inputs: "A2, Y, parameters". Outputs: "cost".
        cost = compute_cost(A2, Y, parameters)
        cost_iter_list.append([cost, i])

        # Backpropagation. Inputs: "parameters, cache, X, Y". Outputs: "grads".
        grads = backward_propagation(parameters, cache, X, Y)  # 梯度

        # Gradient descent parameter update. Inputs: "parameters, grads". Outputs: "parameters".
        parameters = update_parameters(parameters, grads)  # 更新权重

        # END CODE HERE ###

        # Print the cost every 1000 iterations
        if print_cost and i % 1000 == 0:
            print("Cost after iteration %i: %f" % (i, cost))

    plot_cost(np.array(cost_iter_list))

    return parameters


####
# ======================888跑动模型=====================
####
def predict(parameters, X):
    A2, cache = forward_propagation(X, parameters)

    predictions = np.round(A2)

    return predictions


parameters = nn_model(X, Y, n_h=6, num_iterations=10000, print_cost=True)


####
# ==========【有改动】============999画出分类边界=====================
####

def plotDescisionBoundary(X, Y):
    # 逻辑回归模型
    X = X.T
    Y = Y.T
    lr = LogisticRegression(C=1e5)
    lr.fit(X, Y)
    # meshgrid函数生成两个网格矩阵
    h = .01
    x_min, x_max = X[:, 0].min() - .25, X[:, 0].max() + .25
    y_min, y_max = X[:, 1].min() - .25, X[:, 1].max() + .25
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
    # pcolormesh函数将xx,yy两个网格矩阵和对应的预测结果Z绘制在图片上
    Z = lr.predict(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
    plt.figure(1, figsize=(8, 6))
    plt.pcolormesh(xx, yy, Z, cmap=plt.cm.Paired)
    # 绘制散点图
    #plt.scatter(X[:, 0], X[:, 1], c=np.squeeze(Y), camp=plt.cm.Spectral)
    # 设置几个空列表用来绘图
    length = len(X)
    SET_l = []
    SET_w = []
    VER_l = []
    VER_w = []
    VIR_l = []
    VIR_w = []
    # 得到不同种类的属性列表
    for i in range(length):
        if Y[i] == 0:
            SET_l.append(X[i, 0])
            SET_w.append(X[i, 1])
        else:
            if Y[i] == 1:
                VER_l.append(X[i, 0])
                VER_w.append(X[i, 1])
            else:
                if Y[i] == 2:
                    VIR_l.append(X[i, 0])
                    VIR_w.append(X[i, 1])
    # SET = list(zip(SET_l,SET_w))
    # VER = list(zip(VER_l,VER_w))
    # VIR = list(zip(VIR_l,VIR_w))
    # 绘制散点图
    plt.scatter(SET_l, SET_w)
    plt.scatter(VER_l, VER_w)
    plt.scatter(VIR_l, VIR_w)
    # plt.scatter(X[:50, 0], X[:50, 1], color='red', marker='o', label='setosa')
    # plt.scatter(X[50:100, 0], X[50:100, 1], color='blue', marker='x', label='versicolor')
    # plt.scatter(X[100:, 0], X[100:, 1], color='green', marker='s', label='Virginica')
    plt.xlabel('Sepal length', color='black', fontsize=15)
    plt.ylabel('Sepal width', color='black', fontsize=15)
    plt.xlim(xx.min(), xx.max())
    plt.ylim(yy.min(), yy.max())
    plt.xticks(())
    plt.yticks(())
    plt.show()
    plt.title('决策分界', color='red', fontsize=20)


plotDescisionBoundary(X, Y[0, :])

# plot_decision_boundary(lambda x: predict(parameters, X), X, Y)
plt.title("Decision Boundary for hidden layer size " + str(6))
plt.xlabel('Petal Length')
plt.ylabel('Petal Width')



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/971895.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号