Octave is one of the popular and open-source language used in Machine Learning. In this post, I'll discuss sum function, which is commonly used especially if you are following up the Machine Learning course by Andrew Ng on Coursera.
We can access a single element from matrix by specifying row,column index. For example, if we want to access the second element from first row, it would be
If we have to access all the elements from first column, syntax would be :
Same syntax can be extended to access all the elements from a given row, the example below will access all the elements from second row
Now that we know some techniques on accessing Matrix elements, lets move to Sum function.
Sum function is used to sum all the elements accross columns or rows.
To sum all the elements accross columns, we will use sum(Matrix,1) OR sum(Matrix)
To sum all the elements accross rows, we will use sum(Matrix,2)
To sum all the elements of Matrix, we can use the following approach
- Sum all rows and then sum all columns
- Sum all columns and then sum all rows
- Convert Matrix to Column Vector and then sum column
Computing Cost Function in Linear Regression using Octave :-
In excercise #1 in Machine Learning Course by Andrew NG on Coursera, one of the task is to compute cost function. We can implement this with the following code :-
- First we will find hypothesis by multiplying X and theta. Notice that in lecture, it is mentioned to take transpose of theta first but the way the excercise is setup, theta is already in transposed format.
- Next, we will compute the difference between hypothesis and the actual value. This cost function is actually root mean squared error, so we sumup all the values and divide it by 2 * no of training samples.
Let's start by defining a 2x3 Matrix
>> A = [1, 2, 3; 4, 5, 6]
A =
1 2 3
4 5 6
We can access a single element from matrix by specifying row,column index. For example, if we want to access the second element from first row, it would be
>> A(1,2)
ans = 2
If we have to access all the elements from first column, syntax would be :
>> A(:,1)
ans =
1
4
With colon (:), we are saying to access all the rows but only first column. We can also access multiple columns as>> A(:,[1,3])
ans =
1 3
4 6
Same syntax can be extended to access all the elements from a given row, the example below will access all the elements from second row
>> A(2,:)
ans =
4 5 6
What if we want to convert a Matrix into a Column Vector?>> A(:)
ans =
1
4
2
5
3
Now that we know some techniques on accessing Matrix elements, lets move to Sum function.
Sum function is used to sum all the elements accross columns or rows.
To sum all the elements accross columns, we will use sum(Matrix,1) OR sum(Matrix)
>> sum(A,1)
ans =
5 7 9
To sum all the elements accross rows, we will use sum(Matrix,2)
>> sum(A,2)
ans =
6
15
To sum all the elements of Matrix, we can use the following approach
- Sum all rows and then sum all columns
>> sum(sum(A,2),1)
ans = 21
- Sum all columns and then sum all rows
>> sum(sum(A,1),2)
ans = 21
- Convert Matrix to Column Vector and then sum column
>> sum(A(:),1)
ans = 21
Computing Cost Function in Linear Regression using Octave :-
In excercise #1 in Machine Learning Course by Andrew NG on Coursera, one of the task is to compute cost function. We can implement this with the following code :-
- First we will find hypothesis by multiplying X and theta. Notice that in lecture, it is mentioned to take transpose of theta first but the way the excercise is setup, theta is already in transposed format.
- Next, we will compute the difference between hypothesis and the actual value. This cost function is actually root mean squared error, so we sumup all the values and divide it by 2 * no of training samples.
function J = computeCost(X, y, theta)
m = length(y);
J = 0;
h = X * theta;
s = sum((h - y) .^ 2);
J = s/ (2*m);
end
Comments
Post a Comment