Probar regresión

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score

# DATOS

x = [
    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
]

y = [
    96, 92, 91, 87, 86, 82, 80, 78, 74, 72,
    69, 68, 64, 61, 60, 56, 54, 51, 49, 45,
    43, 40, 38, 35, 33, 29, 28, 24, 23, 19
]
x = np.array(x).reshape(-1, 1)
y = np.array(y)

# SPLIT

X_train, X_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, random_state = 42)

# ENTRENAMIENTO

modelo=LinearRegression()
modelo.fit(X_train, y_train)


# PREDICCION

y_pred = modelo.predict(X_test)

mae  = mean_absolute_error(y_test, y_pred)
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
r2   = r2_score(y_test, y_pred)

print(f"\nResultados en el conjunto de TEST:")
print(f"  MAE  = {mae:.3f}   (error medio absoluto)")
print(f"  RMSE = {rmse:.3f}  (raiz error cuadratico)")
print(f"  R2   = {r2:.3f}   (coef. determinacion)")

Publicado por

Juan Pablo Fuentes

Formador de programación y bases de datos