Py.Cafe

radu.dogaru/

ml-playground-regresie-polinomiala

🤖 ML Playground: Regresie Polinomială

DocsPricing
  • app.py
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import sklearn


import streamlit as st
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
import matplotlib.pyplot as plt

st.title("🤖 ML Playground: Regresie Polinomială")

# 1. Generare date cu zgomot (Noise)
st.sidebar.header("Parametri Date")
noise = st.sidebar.slider("Nivel de zgomot", 0.0, 1.0, 0.2)
n_points = 50

np.random.seed(42)
X = np.sort(5 * np.random.rand(n_points, 1), axis=0)
y = np.sin(X).ravel() + noise * np.random.randn(n_points)

# 2. Configurare Model ML
st.sidebar.header("Setări Model")
grad_polinomial = st.sidebar.selectbox("Gradul Polinomului", [1, 2, 3, 5, 10])

# Antrenare model
poly = PolynomialFeatures(degree=grad_polinomial)
X_poly = poly.fit_transform(X)
model = LinearRegression()
model.fit(X_poly, y)

# Predicție pentru linia graficului
X_plot = np.linspace(0, 5, 100)[:, np.newaxis]
y_plot = model.predict(poly.transform(X_plot))

# 3. Vizualizare
fig, ax = plt.subplots()
ax.scatter(X, y, color='darkorange', label='Date reale (cu zgomot)')
ax.plot(X_plot, y_plot, color='blue', linewidth=2, label='Predicție Model')
ax.set_title(f"Model antrenat (Grad {grad_polinomial})")
ax.legend()

st.pyplot(fig)

# Afișare metrici simple
mse = np.mean((model.predict(X_poly) - y) ** 2)
st.metric("Eroarea Medie Pătratică (MSE)", f"{mse:.4f}")