Ejercicio
Empezamos cargando las librerias necesarias
[1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression, Lasso
from sklearn import datasets
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
Creamos un dataset y lo retocamos, estamos forzando un ejemplo concreto
[2]:
X, y = make_regression(n_samples=100,n_informative=6, n_features=6, coef=False,noise=100.0, random_state=33, bias=10.5)
df = pd.DataFrame(X)
df[7] = df[5] * 3
df[4] = df[4] + df[3]
df[8] = df[4] / 2
X = df.to_numpy()
df.head()
[2]:
0 | 1 | 2 | 3 | 4 | 5 | 7 | 8 | |
---|---|---|---|---|---|---|---|---|
0 | 0.991136 | 1.630796 | -1.900090 | -0.111391 | -1.232109 | 0.932722 | 2.798165 | -0.616054 |
1 | -0.325548 | -0.538166 | -0.261746 | -0.220028 | 0.109686 | 0.252768 | 0.758303 | 0.054843 |
2 | 1.937571 | 0.338847 | 1.876973 | 0.217793 | 0.086090 | 0.813308 | 2.439924 | 0.043045 |
3 | -0.960129 | 0.511255 | 0.853085 | -1.216964 | -1.547833 | -0.213646 | -0.640938 | -0.773917 |
4 | -1.352448 | -0.613847 | -1.060842 | -0.222442 | 0.307362 | 0.087174 | 0.261521 | 0.153681 |
Divide el conjunto en entrenamiento y test :
[ ]:
Crea una regresión Lasso. Visualiza el valor de los coeficientes
[ ]:
Crea una regresión Lineal. Visualiza el valor de los coeficientes
[ ]:
¿Qué puedes observar?
[ ]:
Isaac Lera and Gabriel Moya Universitat de les Illes Balears isaac.lera@uib.edu, gabriel.moya@uib.edu