模式识别与机器学习-广义逆、梯度下降

分别编写一个用广义逆和梯度下降法来求最小误差平方和最佳解
的算法
(a)产生两个都具有 200 个二维向量的数据集 ? 1 ? 2 。数据集 ? 1
的样本来自均值向量 ? 1 = [ − 5,0] ? 协方差矩阵 ? 1 = ? 的正态分布,
属于“ +1 ”类,数据集 ? 2 的样本来自均值向量 ? 2 = [0,5] ? 、协方差
矩阵 ? 2 = ? 的正态分布,属于“
-1 ”类,其中 I 是一个 2*2 的单位矩
阵。产生的数据中 80% 用于训练, 20% 用于测试。
(b)在上述数据集上分别第 1 题的两个算法,利用产生的训练样本
集得到分类面,算法中用到的各类超参数自定。
(c)分别在训练集和测试集上统计分类正确率。
(d)画出数据集和分类面。
Data.py 按要求生成数据集
其中 shuju1 生成 m1=
-5,0 】, m2=
0,5
其中 shuju2 生成 m1= 1,0 】, m2=
0,1
Draw.py 用于画图
其中 huatu1 绘制训练集曲线
其中 huatu2 绘制测试集曲线
广义逆:
训练集正确率 :1
测试集正确率: 1
Data.py按要求生成数据集
其中shuju1生成m1=【-5,0】,m2=【0,5】
其中shuju2生成m1=【1,0】,m2=【0,1】
Draw.py用于画图
其中huatu1绘制训练集曲线

模式识别与机器学习-广义逆、梯度下降_第1张图片


其中huatu2绘制测试集曲线

模式识别与机器学习-广义逆、梯度下降_第2张图片

Data.py
import math
import numpy as np
import random
import pylab as pl

def shuju1(num):
    m1 = np.array([-5, 0])
    s1 = np.array([[1, 0], [0, 1]])
    m2 = np.array([0, 5])
    s2 = np.array([[1, 0], [0, 1]])
    X1 = np.empty((num, 2))
    X2 = np.empty((num, 2))
    y1 = np.ones((1, num))
    y2 = -np.ones((1, num))
    for i in range(num):
        X1[i] = np.random.multivariate_normal(m1, s1)
        X2[i] = np.random.multivariate_normal(m2, s2)
    X = np.append(X1, X2, axis=0)
    y0 = np.append(y1, y2)
    x = np.zeros([int(num * 0.8 * 2), 2])
    y = np.zeros(int(num * 0.8 * 2))
    x_test = np.zeros([num * 2 - int(num * 0.8 * 2), 2])
    y_test = np.zeros(num * 2 - int(num * 0.8 * 2))
    st = np.random.get_state()
    np.random.shuffle(X)
    np.random.set_state(st)
    np.random.shuffle(y0)
    for i in range(num * 2):
        if i < num * 0.8 * 2:
            x[i] = X[i]
            y[i] = y0[i]
        else:
            x_test[i - int(num * 0.8 * 2)] = X[i]
            y_test[i - int(num * 0.8 * 2)] = y0[i]
    return x, y, x_test, y_test

def shuju2(num):
    m1 = np.array([1, 0])
    s1 = np.array([[1, 0], [0, 1]])
    m2 = np.array([0, 1])
    s2 = np.array([[1, 0], [0, 1]])
    X1 = np.empty((num, 2))
    X2 = np.empty((num, 2))
    y1 = np.ones((1, num))
    y2 = -np.ones((1, num))
    for i in range(num):
        X1[i] = np.random.multivariate_normal(m1, s1)
        X2[i] = np.random.multivariate_normal(m2, s2)
    X = np.append(X1, X2, axis=0)
    y0 = np.append(y1, y2)
    x = np.zeros([int(num * 0.8 * 2), 2])
    y = np.zeros(int(num * 0.8 * 2))
    x_test = np.zeros([num * 2 - int(num * 0.8 * 2), 2])
    y_test = np.zeros(num * 2 - int(num * 0.8 * 2))
    st = np.random.get_state()
    np.random.shuffle(X)
    np.random.set_state(st)
    np.random.shuffle(y0)
    for i in range(num * 2):
        if i < num * 0.8 * 2:
            x[i] = X[i]
            y[i] = y0[i]
        else:
            x_test[i - int(num * 0.8 * 2)] = X[i]
            y_test[i - int(num * 0.8 * 2)] = y0[i]
    return x, y, x_test, y_test

Draw.py

import matplotlib.pyplot as plt
import numpy as np
import pylab as pl

def huatu1(x, y, w):
    xc1 = []
    yc1 = []
    xc2 = []
    yc2 = []
    x_num = x.shape[0]
    for i in range(x_num):
        if (y[i] == 1):
            xc1.append(x[i, 0])
            yc1.append(x[i, 1])
        else:
            xc2.append(x[i, 0])
            yc2.append(x[i, 1])
    plt.figure()
    plt.scatter(xc1, yc1, s=40, c='red', marker='o')
    plt.scatter(xc2, yc2, s=40, c='blue')
    plt.xlabel('x1')
    plt.ylabel('x2')
    # 绘制分类线
    x = np.arange(-10.0, 5.0, 0.1)
    y = (-w[0] - w[1] * x) / w[2]
    plt.plot(x, y)
    plt.savefig('xunlian.png', dpi=75)
    plt.show()


def huatu2(x, y, w):
    xc1 = []
    yc1 = []
    xc2 = []
    yc2 = []
    x_num = x.shape[0]
    for i in range(x_num):
        if (y[i] == 1):
            xc1.append(x[i, 0])
            yc1.append(x[i, 1])
        else:
            xc2.append(x[i, 0])
            yc2.append(x[i, 1])
    plt.figure()
    plt.scatter(xc1, yc1, s=40, c='red', marker='x')
    plt.scatter(xc2, yc2, s=40, c='blue', marker='x')
    plt.xlabel('x1')
    plt.ylabel('x2')
    # 绘制分类线
    x = np.arange(-10.0, 5.0, 0.1)
    y = (-w[0] - w[1] * x) / w[2]
    plt.plot(x, y)
    plt.savefig('ceshi.png', dpi=75)
    plt.show()


guangyini.py

import numpy as np
import time
import Data
import Draw


def guangyini(x,y):
    x_num = x.shape[0]
    x_d = x.shape[1]
    w = np.zeros(x_d + 1)
    a = np.ones([x_num, 1])
    x = np.concatenate((a, x), 1)  
    x_ = np.linalg.pinv(x)  
    w = np.matmul(x_, y)
    return w


x,y,x_test,y_test=Data.shuju2(200)
w1=guangyini(x,y)
Draw.huatu1(x,y,w1)
Draw.huatu2(x_test,y_test,w1)

suijitidu.py

import numpy as np
import time
import Data
import Draw


def gardient_descent(x,y,t,u):
    x_num = x.shape[0]
    x_d = x.shape[1]
    w = np.zeros(x_d + 1)
    a = np.ones([x_num, 1])
    x = np.concatenate((a, x), 1)

    for i in range(t):
        L_G = np.zeros(x_d + 1)
        for j in range(x_num):
            L_G = L_G + (np.dot(w, x[j]) - y[j]) * x[j]
        w = w - u * L_G
        if (abs(L_G.any()) <= 1e-6):
            break
    return w

def test(xt, yt, w):
    xt_num = xt.shape[0]
    xt_d = xt.shape[1]
    a = np.ones([xt_num, 1])
    xt = np.concatenate((a, xt), 1)
    right = 0
    for i in range(xt_num):
        y_hat = np.sign(w @ xt[i])
        if (y_hat == yt[i]):
            right = right + 1
    accuracy = right / xt_num
    for i in range(xt_num):
        Lout = (np.dot(w, xt[i]) - yt[i])**2
    return Lout

x,y,xt,yt=Data.shuju2(200)
Mepoch = 50
Lout = np.zeros(Mepoch)
for t in range(Mepoch):
    w1 =gardient_descent(x, y, t, 0.001)
    Lout[t] = test(xt, yt, w1)
Draw.huatu1(x,y,w1)
Draw.huatu2(xt,yt,w1)

你可能感兴趣的:(机器学习,分类,数据挖掘,python,人工智能)