Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # выделим в данных признаки и целевой признак
- features = data.drop(['Страховые выплаты'], axis=1)
- target = data['Страховые выплаты']
- # разделим на обучающую и тестовую выборки
- train_features, test_features, train_target, test_target = train_test_split(
- features, target, test_size = 0.25,
- random_state=1234)
- class LinearRegression:
- def fit(self, train_features, train_target):
- X = np.concatenate((np.ones((train_features.shape[0], 1)), train_features), axis=1)
- y = train_target
- w = (np.linalg.inv(X.T.dot(X)).dot(X.T)).dot(y)
- self.w = w[1:]
- self.w0 = w[0]
- def predict(self, test_features):
- return test_features.dot(self.w) + self.w0
- model = LinearRegression()
- model.fit(train_features, train_target)
- predictions = model.predict(test_features)
- print(r2_score(test_target, predictions))
- #Теперь сгенерируем случайную матрицу функцией numpy.random.normal()
- np.random.seed(100) # фиксируем случайность
- P = np.random.normal(3, 2.5, size=(4, 4))
- A = X @ P
- A.shape
- model.fit(A, train_target)
- predictions = model.predict(A)
- print(r2_score(test_target, predictions))
- ---------------------------------------------------------------------------
- ValueError Traceback (most recent call last)
- /tmp/ipykernel_31/3434024009.py in <module>
- ----> 1 model.fit(A, train_target)
- 2 predictions = model.predict(A)
- 3 print(r2_score(test_target, predictions))
- /tmp/ipykernel_31/3890212866.py in fit(self, train_features, train_target)
- 3 X = np.concatenate((np.ones((train_features.shape[0], 1)), train_features), axis=1)
- 4 y = train_target
- ----> 5 w = (np.linalg.inv(X.T.dot(X)).dot(X.T)).dot(y)
- 6 self.w = w[1:]
- 7 self.w0 = w[0]
- ValueError: shapes (5,4847) and (3635,) not aligned: 4847 (dim 1) != 3635 (dim 0)
Add Comment
Please, Sign In to add comment