Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Matrix:
- @classmethod
- def zeros(cls, n_rows, n_cols):
- return cls([[0.0] * n_cols for _ in range(n_rows)])
- def __init__(self, data):
- self.data = data
- self.n_rows = len(self.data)
- self.n_cols = len(self.data[0])
- def __getitem__(self, ij):
- i, j = ij
- return self.data[i][j]
- def __setitem__(self, ij, value):
- i = ij[0]
- j = ij[1]
- self.data[i][j] = value
- def __eq__(self, other):
- return isinstance(other, Matrix) and self.data == other.data
- def __str__(self):
- return str(self.data)
- def __repr__(self):
- return repr(self.data)
- # ---
- def __matmul__(self, other):
- result = Matrix.zeros(self.n_rows, other.n_cols)
- for i in range (self.n_rows):
- for j in range (other.n_cols):
- for k in range (self.n_cols):
- result[i][j] = result[i][j] + self.__getitem__([i, k]) * other.__getitem__([[k], [j]])
- return Matrix(result)
- def transpose(self):
- result = Matrix.zeros(self.n_cols, self.n_rows)
- for i in range (self.n_cols):
- for j in range (self.n_rows):
- print ([i, j])
- result[i][j] = self.__getitem__([i, j])
- return Matrix(result)
- def inverse(self):
- pass
- class LinearRegression:
- def __init__(self):
- self.beta = None
- self.beta0 = None
- def fit(self, X, y):
- # N.B. здесь и далее y -- вектор-столбец, то есть экземпляр
- # класса Matrix.
- return self
- def predict(self, X):
- y = Matrix.zeros(X.n_row, 1)
- return y
- def rmse(y_true, y_pred):
- return 0.0
- def learn_test_split(X, y):
- X_learn = 0
- X_test = 0
- y_learn = 0
- y_test = 0
- return X_learn, X_test, y_learn, y_test
- def cv_linear_regression(X, y, *, n_iter=1000):
- return 0.0
- a = [[1, 2, 3], [4, 5, 6]]
- b = Matrix(a)
- print (b.transpose())
Advertisement
Add Comment
Please, Sign In to add comment