Skip to content

Commit 7735c35

Browse files
committed
gradient desent
1 parent 4283667 commit 7735c35

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
%Gradient desecent for Linear regression model
2+
%used for training theta(parameter)
3+
%LinearReg train theta vector using training dataset
4+
%X is matrix m rows i.e. number of training examples
5+
% and n columns i.e. number of features.
6+
%y is vector of size m, containing actual values from dataset
7+
8+
%Loading data
9+
data = load('filename.txt');
10+
dim=size(data);
11+
num_row=dim(1,1);
12+
num_col=dim(1,2);
13+
14+
for i=1:(num_col-1)
15+
if(i==1)
16+
X=data(:,1);
17+
else
18+
X=[X , data(:,i)];
19+
end
20+
end;
21+
22+
y=data(:,num_col);
23+
24+
%adding a biased term in X matrix
25+
X=[ones(m,1) X];
26+
27+
%initialization of theta
28+
theta=zeros(size(X,2),1);
29+
30+
%number of features
31+
num_fea=size(X,2)
32+
33+
%numbers of training examples
34+
m=length(y);
35+
36+
%Gradient desecent
37+
for j=1: max_iter
38+
h=(X*theta-y);
39+
for i=1: num_fea
40+
theta(i)=theta(i)-(learning_rate*(X(:,i)'*h)/m);
41+
end
42+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# **Gradient desecent for linear regression**
2+
Gradient descent is one of most used algorithm in Machine Learning.Gradient descent is a first-order iterative optimization algorithm for finding the minimum of a function. To find a local minimum of a function using gradient descent.
3+
4+
#Example
5+
![image](gradient.png)
6+
7+
Supoose we are at any point, and we want to minimize the cost function. Thus this can be achieve by taking derivative of cost function.
8+
The slope of the tangent will give a direction in which the value is decreases. Thus this algorithm make steps down the function.
9+
The size of each step is determine by the learning rate.
10+
11+
#Gradient descent for linear regression
12+
In this model, gradient descent is used to calculating the theta vector(or parameter vector). Once the parameter vector is calculated using
13+
training examples, we can predict the output of unknown input.
Loading

0 commit comments

Comments
 (0)