Work in progress
This commit is contained in:
52
PytorchTutorialCodes/1_linearregression_3.py
Normal file
52
PytorchTutorialCodes/1_linearregression_3.py
Normal file
@ -0,0 +1,52 @@
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from numpy import roots
|
||||
|
||||
# Együtthatók
|
||||
a, b, c, d = 1, -2, 3, -1
|
||||
|
||||
# X értékek
|
||||
x = np.linspace(-2, 2, 400)
|
||||
|
||||
# Polinom értékek
|
||||
y = a + b*x + c*x**2 + d*x**3
|
||||
|
||||
# Gyökök (ahol f(x)=0)
|
||||
gyokok = roots([d, c, b, a])
|
||||
real_gyokok = gyokok[np.isreal(gyokok)].real
|
||||
|
||||
# Első derivált: extrémumok
|
||||
# f'(x) = b + 2c*x + 3d*x**2
|
||||
extr_gyokok = roots([3*d, 2*c, b])
|
||||
real_extr = extr_gyokok[np.isreal(extr_gyokok)].real
|
||||
extr_y = a + b*real_extr + c*real_extr**2 + d*real_extr**3
|
||||
|
||||
# Második derivált: inflexiós pont
|
||||
# f''(x) = 2c + 6d*x
|
||||
iflex_x = -2*c/(6*d)
|
||||
iflex_y = a + b*iflex_x + c*iflex_x**2 + d*iflex_x**3
|
||||
|
||||
# Véletlenszerűen kiválasztott x pontok
|
||||
x_points = np.linspace(-2, 2, 8)
|
||||
y_points = a + b*x_points + c*x_points**2 + d*x_points**3
|
||||
|
||||
# Lineáris regresszió illesztése
|
||||
coeffs = np.polyfit(x_points, y_points, 1) # 1. fokú polinom = egyenes
|
||||
lin_y = coeffs[0]*x + coeffs[1] # y = a*x + b
|
||||
|
||||
|
||||
|
||||
plt.plot(x, y, label='Polinom')
|
||||
plt.scatter(real_gyokok, np.zeros_like(real_gyokok), color='red', label='Gyökök')
|
||||
plt.scatter(real_extr, extr_y, color='green', label='Extrémumok')
|
||||
plt.scatter(iflex_x, iflex_y, color='purple', label='Inflekciós pont')
|
||||
plt.scatter(0, a, color='orange', label='Y-tengely metszéspont')
|
||||
plt.scatter(x_points, y_points, color='blue', marker='x', s=80, label='Közelítendő pontok')
|
||||
plt.plot(x, lin_y, color='black', linestyle='--', label='Lineáris regresszió')
|
||||
plt.legend()
|
||||
plt.xlabel('x')
|
||||
plt.ylabel('f(x)')
|
||||
plt.title('Polinom, pontok és lineáris regresszió')
|
||||
plt.grid(True)
|
||||
plt.show()
|
||||
|
||||
Reference in New Issue
Block a user