【问题标题】:How to solve logistic regression using gradient descent in octave?如何使用八度音阶中的梯度下降来解决逻辑回归?
【发布时间】:2019-03-22 15:03:31
【问题描述】:

我正在 Andrews Ng 的 coursera 中学习机器学习课程。我写了一个八度逻辑回归的代码。但是,它不起作用。有人可以帮我吗?

我已从以下链接获取数据集: Titanic survivors

这是我的代码:

pkg load io;

[An, Tn, Ra, limits] = xlsread("~/ML/ML Practice/dataset/train_and_test2.csv", "Sheet2", "A2:H1000");

# As per CSV file we are reading columns from 1 to 7. 8-th column is Survived, which is what we are going to predict
X = [An(:, [1:7])];

Y = [An(:, 8)];

X = horzcat(ones(size(X,1), 1), X);

# Initializing theta values as zero for all
#theta = zeros(size(X,2),1);
theta = [-3;1;1;-3;1;1;1;1];
learningRate = -0.00021;
#learningRate = -0.00011;

# Step 1: Calculate Hypothesis
function g_z = estimateHypothesis(X, theta)
  z = theta' * X';
  z = z';

  e_z = -1 * power(2.72, z);
  denominator = 1.+e_z;
  g_z = 1./denominator;
endfunction

# Step 2: Calculate Cost function
function cost = estimateCostFunction(hypothesis, Y)
  log_1 = log(hypothesis);
  log_2 = log(1.-hypothesis);

  y1 = Y;
  term_1 = y1.*log_1;

  y2 = 1.-Y;
  term_2 = y2.*log_2;

  cost = term_1 + term_2;
  cost = sum(cost);

  # no.of.rows
  m = size(Y, 1);
  cost = -1 * (cost/m); 

endfunction

# Step 3: Using gradient descent I am updating theta values
function updatedTheta = updateThetaValues(_X, _Y, _theta, _hypothesis, learningRate)
  #s1 = _X * _theta;
  #s2 = s1 - _Y;
  #s3 = _X' * s2;

  # no.of.rows
  #m = size(_Y, 1);

  #s4 = (learningRate * s3)/m;

  #updatedTheta = _theta - s4;

  s1 = _hypothesis - _Y;

  s2 = s1 .* _X;
  s3 = sum(s2);

  # no.of.rows
  m = size(_Y, 1);

  s4 = (learningRate * s3)/m;
  updatedTheta = _theta .- s4';
endfunction

costVector = [];
iterationVector = [];
for i = 1:1000
  # Step 1
  hypothesis = estimateHypothesis(X, theta);
  #disp("hypothesis");
  #disp(hypothesis);

  # Step 2
  cost = estimateCostFunction(hypothesis, Y);
  costVector = vertcat(costVector, cost);
  #disp("Cost");
  #disp(cost);

  # Step 3 - Updating theta values
  theta = updateThetaValues(X, Y, theta, hypothesis, learningRate);

  iterationVector = vertcat(iterationVector, i);
endfor

function plotGraph(iterationVector, costVector)
  plot(iterationVector, costVector);
  ylabel('Cost Function');
  xlabel('Iteration');  
endfunction

plotGraph(iterationVector, costVector);

这是我在绘制迭代次数和成本函数时得到的图表。

调整 theta 值和学习率让我感到疲倦。谁能帮我解决这个问题。

谢谢。

【问题讨论】:

  • 我已经从 CSV 文件中删除了列名为“zero”和“id”的特征。
  • 请不要将 cmets 空间用于此类附加信息 - 改为编辑和更新您的帖子。另外,你应该已经添加了octave 标签(这次是为你添加的)。

标签: machine-learning octave logistic-regression gradient-descent


【解决方案1】:

我犯了一个数学错误。我应该使用 power(2.72, -z) 或 exp(-z)。相反,我使用了 -1 * power(2.72, z)。现在,我得到了一条合适的曲线。

谢谢。

【讨论】:

    猜你喜欢
    • 2015-06-08
    • 2021-12-22
    • 2020-05-01
    • 2014-03-21
    • 2014-08-26
    • 2015-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多