text_plain_output_2.png text_plain_output_1.png
from sklearn.linear_model import Lasso

from sklearn.model_selection import GridSearchCV

from sklearn.model_selection import train_test_split

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import Lasso
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import r2_score
from sklearn.metrics import mean_squared_error
pd.options.display.max_rows = None
pd.options.display.max_columns = None
SEED = 746

data = pd.read_csv('/kaggle/input/body-fat-prediction-dataset/bodyfat.csv')

data.drop('Density', axis=1, inplace=True)

data.isna().any()

data.drop([171, 181], inplace=True)

data.corr()

X = data.iloc[:, 1:].values
y = data.iloc[:, 0].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=SEED)

lasso_model = Lasso()
params = {'alpha': np.linspace(0, 2, 200)}
lasso_grid = GridSearchCV(lasso_model, param_grid=params, cv=4, refit=True, n_jobs=-1, verbose=2)
lasso_grid.fit(X_train, y_train)

print(lasso_grid.best_params_)
print(lasso_grid.best_score_)