-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearRegression.py
More file actions
30 lines (25 loc) · 876 Bytes
/
LinearRegression.py
File metadata and controls
30 lines (25 loc) · 876 Bytes
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
# -*- coding:utf-8 -*-
#!/usr/bin/python
import numpy as np
import matplotlib.pyplot as plt
class LinearRegression():
def __init__(self, iteration=100, learning_rate=0.001):
self.iteration = iteration
self.learning_rate = learning_rate
def fit(self, X, y):
n, m = X.shape
self.weight = np.zeros((n, 1))
self.bias = np.zeros(1)
for i in range(self.iteration):
loss = np.sum((np.dot(self.weight.T, X) - y) ** 2)
print(loss)
dw = 2 * np.dot(X, np.dot(X.T, self.weight) - y.T)
self.weight = self.weight - self.learning_rate * dw
print(self.weight)
def predict(self, X):
return np.dot(self.weight, X)
X = np.random.uniform(-10, 10, (2, 10))
w = np.asarray([[1], [2]])
y = np.dot(w.T, X)
lr = LinearRegression()
lr.fit(X, y)