sobota 27. dubna 2024

Lineární regrese

 

import numpy as np from sklearn.linear_model import LinearRegression import matplotlib.pyplot as plt x = np.array([0, 1, 2, 3, 4, 5, 6, 7]) y = np.array([1, 3, 5, 7, 9, 11, 13, 15]) model = LinearRegression() model.fit(x[:, np.newaxis], y) koeficienty = model.coef_ print(f"Naučené koeficenty: {koeficienty}") new_x = np.array([8]) y_pred = model.predict(new_x[:, np.newaxis]) print(f"Předpovědi pro x = {new_x}: {y_pred}") plt.scatter(x, y, label="Data") plt.plot(x, model.predict(x[:, np.newaxis]), label="Model") plt.xlabel("X") plt.ylabel("Y") plt.title("Lineární regrese") plt.legend() plt.show()


A co je "pod kapotou"?

Hledáme parametry rovnice přímky y = a * x + b
  • Směrový koeficient (a): a = Σ((xi - x̄)(yi - ȳ)) / Σ(xi - x̄)²
  • Úsekový koeficient (b): b = ȳ - b * x̄

Kde:

  • a reprezentují průměrnou hodnotu x a y
  • xi a yi reprezentují jednotlivé hodnoty x a y

import numpy as np y = np.array([1, 3, 5, 7, 9, 11, 13, 15]) noise = np.random.normal(0, 0.1, len(y)) # Standardní odchylka 0.1 y_with_noise = y + noise model = LinearRegression() model.fit(x[:, np.newaxis], y_with_noise) koeficienty = model.coef_ print(f"Naučené koeficenty: {koeficienty}") new_x = np.array([8]) y_pred = model.predict(new_x[:, np.newaxis]) print(f"Předpovědi pro x = {new_x}: {y_pred}") plt.scatter(x, y_with_noise, label="Data") plt.plot(x, model.predict(x[:, np.newaxis]), label="Model") plt.xlabel("X") plt.ylabel("Y_noyse") plt.title("Lineární regrese") plt.legend() plt.show()