Cost Function的原理及实现(Python, matlab)

成本函数(Cost Function)

J(θ0,θ1)=12mi=1m(hθ(x(i))y(i))2

m : Number of training examples.
x : input.
y : output.
Parameters: θ0, θ1 .

以下为MATLAB实现方式:

function J = computeCost(X, y, theta)
    %COMPUTECOST Compute cost for linear regression
    %   J = COMPUTECOST(X, y, theta) computes the cost of using theta as the parameter for linear regression to fit the data points in X and y
    m = length(y); % number of training examples
    J = 0;
    J = sum((X * theta - y) .^2) / 2 / m;
end

以下为Python实现方式:

def computeCost(X, y, theta):
    """Cost Functions

    Parameters
    ----------
    X : np.ndarray, like (49 * 2)
    y : np.ndarray, like (49 * 1)
    theta : np.ndarray, like (2 * 1)

    Returns
    -------
    J : float, cost

    """
    y = np.transpose(y)
    J = sum((np.dot(X, theta) - y.reshape(len(y),1)) **2) / 2.0 / len(y)
    # np.dot(A, B) 矩阵乘积,A * B 矩阵点乘
    # pow(A, 2) 多次乘积,**n 点次方
    return J

注意:np中点乘和矩阵乘积,点次方与多次乘积的区别,与MATLAB不同。

梯度下降法(Gradient Descent)

repeatuntilconvergence{θj:=θjαJ(θ0,θ1)θj(j=0,1)}

α : learning rate.
: 偏微分

以下为MATLAB实现:

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
    %GRADIENTDESCENT Performs gradient descent to learn theta
    %   theta = GRADIENTDESCENT(X, y, theta, alpha, num_iters) updates theta by 
    %   taking num_iters gradient steps with learning rate alpha

    m = length(y); % number of training examples
    J_history = zeros(num_iters, 1);

    for iter = 1:num_iters
        theta = theta -  X' * (X * theta - y) * (alpha / m);  
        J_history(iter) = computeCost(X, y, theta);
    end
end

以下为Python实现:

def gradientDescent(X, y, theta, alpha, num_iters):
    """Gradient Descent for (Multivariate) Linear Regression

    Parameters
    ----------
    X : np.ndarray, like (49 * 2)
    y : np.ndarray, like (49 * 1)
    theta : np.ndarray, like (49 * 1)
    alpha : learning rate
    num_iters : number of iter

    Returns
    -------
    tuple(J_history, theta)
    J_history : np.ndarray, like (num_iters, 1)
    theta : theta of convergence, like (2 * 1)
    """

    J_history = np.zeros((num_iters, 1))
    for n_iter in range(num_iters):
        theta = theta - np.dot(X.T, np.dot(X, theta) - y.reshape(len(y),1)) *alpha / len(y)
        J_history[n_iter, 0] = computeCost(X, y, theta)
    return J_history, theta

线性回归中的梯度下降公式

θ0:=θ0α1mi=1m(hθ(x(i))y(i))

θ1:=θ1α1mi=1m(hθ(x(i))y(i))x(i)1

同理可得:
θj:=θjα1mi=1m(hθ(x(i))y(i))x(i)j

在(多元)线性回归中,由于在代码中直接使用矩阵进行运算,因此代码同上。

粒子群算法(Particle Swarm Optimization, PSO)是一种模拟鸟群觅食行为的全局优化搜索算法。它将个体的位置和速度看作是粒子在多维空间中的移动,通过迭代更新每个粒子的位置和速度,找到问题的最佳解。 以下是简化的PythonMATLAB代码示例: **Python (using the `psopt` library):** ```python from pyswarm import pso # 定义目标函数 def obj_func(x): return sum(x**2) # 参数设置 options = {'c1': 0.5, 'c2': 0.3, 'w': 0.9, 'k': 2} # 加速度系数、惯性权重、社会学习因子等 lb = [-5] * len(x0) # 下界 ub = [5] * len(x0) # 上界 # 运行PSO result = pso(obj_func, lb, ub, options) best_pos, best_val = result.pbest positions[-1], result.best cost ``` **MATLAB (using `particleswarm` function from Global Optimization Toolbox):** ```matlab % 定义目标函数 fun = @(x) norm(x)^2; % 或者自定义其他函数 % 初始化参数 options = optimoptions('particleswarm', ... % 使用默认选项,也可以调整特定参数 'MaxIterations', 1000, ... % 最大迭代次数 'Display', 'iter'); % 显示进度信息 % 范围限制 lb = -ones(1, length(x0)); % 下界数组 ub = ones(1, length(x0)); % 上界数组 % 运行PSO [xBest, fmin, output] = particleswarm(fun, x0, [], [], lb, ub, options); xBest, fmin ``` 注意:以上代码仅用于演示基本的PSO算法应用,实际应用中你需要根据具体的优化问题进行适当修改,并可能需要处理更多细节,如初始化粒子位置、评估函数等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值