algorithm - 我想运行一个具有指定假设的监督学习算法,该假设的参数 theta 处于异常位置

标签 algorithm machine-learning linear-regression supervised-learning

我想运行一个带有特定假设的监督学习算法,该假设的参数 theta 处于不寻常的位置。

y = theta1 * (exp(theta2 * X)) + theta0

我尝试使用具有以下函数的梯度下降:

代码:

m = length(y); 
num_iters = 500;
J_history = zeros(num_iters, 1);
alpha = 0.1;
theta = zeros(3, 1);
for q = 1:m
    A(q,:) = [2, (2*exp(theta(3, 1) * X(q, 1))), (2*theta(2, 1)*X(q, 1)*exp(theta(3, 1) * X(q, 1)))];
end    
for iter = 1:num_iters
    num_theta = length(theta);

    for j = 1:num_theta
        inner_sum = 0;
        for i = 1:m
            inner_sum = inner_sum + (theta(2, 1)*(exp(X(i, 1)*theta(3, 1))) + theta(1, 1) - y(i, 1)) * A(i, j);
        end
        theta(j, 1) = theta(j, 1) -  (alpha * inner_sum / m)
    end
    J_history(iter) = compute_cost(X, y);
end

    % Save the cost J in every iteration    
J_history(iter) = compute_cost(X, y);
end

其中 compute_cost 是我的成本函数,它是:

predictions = theta(2, 1)*(exp(X*theta(3, 1))) + theta(1, 1); %hypothesis              
sqrErrors = (predictions - y).^2;    
J = sum(sqrErrors)/(2*m);

现在,当我将 theta 的初始值设为 zeros(3, 1) 时,我的 theta(3, 1)==theta2 变为零,这就是我遇到的中断 当我的初始 theta 为 ones(3, 1) 时,它的值为无穷大

那么,我可以将这个假设用于线性回归,还是可以使用任何其他类似的假设函数来代替当前假设。

最佳答案

Python 函数 scipy.optimize.curve_fit 有一个示例,您的函数恰好适合!

查看:https://docs.scipy.org/doc/scipy-0.19.1/reference/generated/scipy.optimize.curve_fit.html

关于algorithm - 我想运行一个具有指定假设的监督学习算法,该假设的参数 theta 处于异常位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46088701/

相关文章:

python - 当 x = y 时,Numpy 和 R 在线性回归中给出非零截距

java - 断项链问题 USACO 中的错误答案

c++ - 在奇怪的二叉树中搜索节点

python - 获取具有多个不明确元素的数组的真值以进行蛋白变换

python - 将向量 w 投影到向量 v 上并绘制垂直线 - 为 PCA 做准备

r - glmnet 中的汇总统计数据

c# - 这是快速排序吗?

algorithm - 将 float 转换为具有给定总和的整数

r - 与从数据集中移除异常值相关的问题 (R)

python - 设置 Statsmodels 线性回归的数据格式