Skip to content

Commit 1a18e2c

Browse files
committed
! weights, plan matrix
1 parent f70f03e commit 1a18e2c

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

models/linear_regression_model.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66
class LinearRegression:
77
def __init__(self, base_functions: list):
8-
# TODO: init weights using np.random.randn (normal distribution with mean=0 and variance=1)
9-
self.weights = np.random.randn(len(base_functions))
8+
"""init weights using np.random.randn (normal distribution with mean=0 and variance=1)"""
9+
"""we take one more weight, because we need column f0 = 1"""
10+
self.weights = np.random.randn(len(base_functions) + 1)
1011
self.base_functions = base_functions
1112

1213
@staticmethod
@@ -15,8 +16,12 @@ def __pseudoinverse_matrix(matrix: np.ndarray) -> np.ndarray:
1516
pass
1617

1718
def __plan_matrix(self, inputs: np.ndarray) -> np.ndarray:
18-
# TODO build Plan matrix using list of lambda functions defined in config. Use only one loop (for base_functions)
19-
return np.array([function(inputs) for function in lin_reg_cfg.base_functions]).T
19+
"""build Plan matrix using list of lambda functions defined in config.
20+
Use only one loop (for base_functions)""" """about f0 - we need column with only ones"""
21+
matrix = [np.ones_like(inputs)]
22+
for function in self.base_functions:
23+
matrix = np.append(matrix, np.array([function(inputs)]), 0)
24+
return matrix.T
2025

2126
def __calculate_weights(
2227
self, pseudoinverse_plan_matrix: np.ndarray, targets: np.ndarray
@@ -25,7 +30,7 @@ def __calculate_weights(
2530
pass
2631

2732
def calculate_model_prediction(self, plan_matrix) -> np.ndarray:
28-
# TODO calculate prediction of the model (y) using formula from the lecture
33+
"""calculate prediction of the model (y) using formula from the lecture"""
2934
return np.dot(plan_matrix, self.weights.T)
3035

3136
def train_model(self, inputs: np.ndarray, targets: np.ndarray) -> None:

test.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import numpy as np
2+
3+
4+
def func(x):
5+
return x**2
6+
7+
8+
inputs = np.array([1, 2, 3, 4, 5])
9+
ar = np.ones_like(inputs)
10+
mas = func(inputs)
11+
new = np.append(ar, [mas, mas])
12+
print(new)

0 commit comments

Comments
 (0)