# Untitled

Dec 12th, 2023
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import numpy as np
2. import matplotlib.pyplot as plt
3. from sklearn.neighbors import KernelDensity
4. from scipy.interpolate import interp1d
5.
6. # Функция для генерации данных
7. def generate_data(n_samples=100, noise_level=0.3):
8. X = np.linspace(0, 10, n_samples)
9. y = np.sin(X) + np.random.normal(0, noise_level, n_samples) # синусоида с шумом
10. return X, y
11.
12. # Метод Надарая-Ватсона
13. def nadaraya_watson(X, y, X_test, bandwidth=0.5):
14. kde = KernelDensity(kernel='gaussian', bandwidth=bandwidth).fit(X[:, None], y)
15. log_density = kde.score_samples(X_test[:, None])
16. return np.exp(log_density)
17.
18. # Метод Лоулесса (метод локальных полиномов)
19. def lowess(X, y, f=0.25, iter=3):
20. n = len(X)
21. r = int(np.ceil(f * n))
22. y_est = np.zeros(n)
23. delta = np.ones(n)
24.
25. for iteration in range(iter):
26. for i in range(n):
27. weights = delta * np.exp(-np.abs((X[i] - X) / X[r]) ** 2)
28. b = np.array([np.sum(weights * y), np.sum(weights * y * X)])
29. A = np.array([[np.sum(weights), np.sum(weights * X)],
30. [np.sum(weights * X), np.sum(weights * X * X)]])
31. theta = np.linalg.solve(A, b)
32. y_est[i] = theta[0] + theta[1] * X[i]
33.
34. delta = np.abs(y - y_est)
35. delta = delta / np.max(delta)
36.
37. return y_est
38.
39. # Генерируем данные
40. X, y = generate_data()
41. X_noise, y_noise = generate_data(noise_level=1)
42.
43. # Тестовые данные
44. X_test = np.linspace(0, 10, 500)
45.
46. # Применение методов
47. y_nw = nadaraya_watson(X[:, None], y, X_test[:, None])
48. y_lowess = lowess(X, y)
49.
50. # Визуализация
51. plt.figure(figsize=(12, 6))
52. plt.subplot(1, 2, 1)
53. plt.scatter(X, y, color='blue', label='Original Data')
56. plt.legend()
57.
58. plt.subplot(1, 2, 2)
59. plt.scatter(X, y, color='blue', label='Original Data')
60. plt.plot(X, y_lowess, color='green', label='Lowess')
61. plt.title('Lowess Regression')
62. plt.legend()
63.
64. plt.show()
65.