昆仑山:眼中无形心中有穴之穴人合一

夫君子之行,静以修身,俭以养德;非澹泊无以明志,非宁静无以致远。夫学须静也,才须学也;非学无以广才,非志无以成学。怠慢则不能励精,险躁则不能冶性。年与时驰,意与岁去,遂成枯落,多不接世。悲守穷庐,将复何及!

 

统计学习方法[最小二乘法和正则化](李航大神版)

最小二乘法 p11

import numpy as np
import scipy as sp
from scipy.optimize import leastsq

import matplotlib.pyplot as plt


# 目标函数
def real_func(x):
    return np.sin(2 * np.pi * x)


# 多项式
def fit_func(p, x):
    f = np.poly1d(p)
    return f(x)


# 残差
def residuals_func(p, x, y):
    return fit_func(p, x) - y


# 十个点
x = np.linspace(0, 1, 10)
# print(x)
x_points = np.linspace(0, 1, 1000)
# print(x_points)
# 加上正态分布噪音的目标函数的值
y_ = real_func(x)
y = [np.random.normal(0, 0.1) + y1 for y1 in y_]


def fitting(M=0):
    '''
    M 为 多项式函数
    :param M: 为 多项式函数
    :return:
    '''

    # 随机初始化多项式参数
    p_init = np.random.rand(M + 1)
    print("参数初始化:\t", p_init)
    # 最小二乘法
    p_lsq = leastsq(residuals_func, p_init, args=(x, y))
    print("拟合参数Fitting Parameters:\t", p_lsq[0])
    # 可视化
    plt.plot(x_points, real_func(x_points), label='real')
    plt.plot(x_points, fit_func(p_lsq[0], x_points), label='fitted_curve')
    plt.plot(x, y, 'bo', label='noise')
    plt.legend()
    plt.show()
    # return p_lsq


# M=0
p_lsq_0 = fitting(M=0)

# M=1
p_lsq_0 = fitting(M=1)

# M=3
p_lsq_0 = fitting(M=3)

# M=9
p_lsq_0 = fitting(M=9)

正则化 p13

import numpy as np
import scipy as sp
from scipy.optimize import leastsq

import matplotlib.pyplot as plt


# 目标函数
def real_func(x):
    return np.sin(2 * np.pi * x)


# 多项式
def fit_func(p, x):
    f = np.poly1d(p)
    return f(x)


# 残差
def residuals_func(p, x, y):
    return fit_func(p, x) - y


# 十个点
x = np.linspace(0, 1, 10)
# print(x)
x_points = np.linspace(0, 1, 1000)
# print(x_points)
# 加上正态分布噪音的目标函数的值
y_ = real_func(x)
y = [np.random.normal(0, 0.1) + y1 for y1 in y_]


def fitting(M=0):
    '''
    M 为 多项式函数
    :param M: 为 多项式函数
    :return:
    '''

    # 随机初始化多项式参数
    p_init = np.random.rand(M + 1)
    print("参数初始化:\t", p_init)
    # 最小二乘法
    p_lsq = leastsq(residuals_func, p_init, args=(x, y))
    print("拟合参数Fitting Parameters:\t", p_lsq[0])
    # 可视化
    plt.plot(x_points, real_func(x_points), label='real')
    plt.plot(x_points, fit_func(p_lsq[0], x_points), label='fitted_curve')
    plt.plot(x, y, 'bo', label='noise')
    plt.legend()
    plt.show()
    return p_lsq


regularization = 0.0001


def residuals_func_regularization(p, x, y):
    return np.append(fit_func(p, x) - y, np.sqrt(0.5 * regularization * np.square(p)))  # L2范数作为正则化项


# 最小二乘法,加正则化项
p_init = np.random.rand(9 + 1)
p_lsq_0 = fitting(9)
p_lsq_regularization = leastsq(residuals_func_regularization, p_init, args=(x, y))
plt.plot(x_points, real_func(x_points), label='real')
plt.plot(x_points, fit_func(p_lsq_0[0], x_points), label="fitted curve")
plt.plot(x_points, fit_func(p_lsq_regularization[0], x_points), label="regularization")
plt.plot(x, y, 'bo', label='noise')
plt.legend()
plt.show()


参考文献

https://blog.csdn.net/z2536083458/article/details/85212427

posted on 2019-04-05 12:52  Indian_Mysore  阅读(573)  评论(1编辑  收藏  举报

导航