Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- from numpy.linalg import inv
- # ... [Ваш текущий код до определения функции nadaraya_watson]
- def lawless_regression(X_train, y_train, x, h):
- weights = np.exp(-((X_train - x)**2) / (2 * h**2))
- W = np.diag(weights.ravel())
- A = np.vstack([X_train.T, np.ones(len(X_train))]).T
- AW = A.T.dot(W)
- theta = inv(AW.dot(A)).dot(AW.dot(y_train))
- return x * theta[0] + theta[1]
- # ... [Остальная часть вашего кода для непараметрической регрессии]
- plt.figure(figsize=(24, 8))
- # Отображение результатов метода Надарая-Ватсона
- plt.subplot(1, 2, 1)
- # ... [Код для отображения результатов метода Надарая-Ватсона]
- plt.title('Непараметрическая регрессия: Метод Надарая-Ватсона')
- # Отображение результатов метода Лоулесса
- plt.subplot(1, 2, 2)
- for h in h_values:
- y_pred = np.array([lawless_regression(X, y, x, h) for x in X_test])
- plt.plot(X_test, y_pred, label=f'h = {h}')
- plt.scatter(X, y, color='k', label='Исходные данные')
- plt.xlabel('X')
- plt.ylabel('y')
- plt.title('Непараметрическая регрессия: Метод Лоулесса')
- plt.legend()
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement