-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathlinear_regression.jl
86 lines (67 loc) · 2.17 KB
/
linear_regression.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using Statistics
"""
linear_regression(X::Matrix, Y::Matrix, alpha::Float64, iterations::Int64)
Linear Regression Algorithm using Gradient Descent
to find the best fit line for the data.
Arguments :
- X : Training data set of shape [m x n]
- Y : Labels corresponding to the training data set of shape [m x 1]
- alpha : Learning rate
- iterations : Number of iterations to train
Returns :
- W : Weight vector
- b : Bias
- J_history : Cost Function History
gradient_descent(W::Matrix, b, X::Matrix, Y::Matrix, alpha::Float64, num_iters::Int64)
Performs gradient descent to learn W and b
Arguments
- `W`: matrix of weights
- `b`: bias vector
- `X`: matrix of features
- `Y`: vector of labels
- `alpha`: learning rate ( generally ranges from 0.01 to 0.00001 )
- `num_iters`: number of iterations to run gradient descent
Returns:
- `W`: matrix of weights
- `b`: bias vector
- `J_history`: vector of cost values after each iteration
# Contributors:- [Navaneeth Sharma](https://github.com/Navaneeth-Sharma)
"""
function gradient_descent(
W::Matrix,
b,
X::Matrix,
Y::Matrix,
alpha::Float64,
num_iters::Int64,
)
m = length(Y)
J_history = []
for i in 1:num_iters
W = W - alpha .* (1 / m) * (X * (X'W .+ b - Y'))
b = b - alpha .* (1 / m) * sum((X'W .+ b - Y'))
push!(J_history, 1 / 2m * sum((X'W .+ b - Y') .^ 2))
end
return W, b, J_history
end
function linear_regression(
X::Matrix,
Y::Matrix,
alpha::Float64,
num_iters::Int64,
)
W = zeros(1, size(X, 1))
b = 0
W, b, J_history = gradient_descent(W, b, X, Y, alpha, num_iters)
return W, b, J_history
end
# Function to predict the output of a given data vector
predict(X::Matrix, W::Matrix, b::Float64) = X'W .+ b;
# Example usage
X = [1, 3, 2, 5, 7, 8, 8, 9, 10, 12]
Y = [2, 4, 3, 6, 8, 9, 9, 10, 11, 13]
X = reshape(X, 1, length(X));
Y = reshape(Y, 1, length(Y));
W, b, J_history = linear_regression(X, Y, 0.01, 1000);
output = predict(X, W, b)
println("The predicted is: $output")