| import pandas as pd |
|
|
| import sklearn |
| import joblib |
|
|
| from sklearn.preprocessing import StandardScaler, OneHotEncoder |
| from sklearn.compose import make_column_transformer |
|
|
| from sklearn.pipeline import make_pipeline |
|
|
| from sklearn.model_selection import train_test_split |
|
|
| from sklearn.linear_model import LinearRegression |
| from sklearn.metrics import mean_squared_error, r2_score |
| from math import sqrt |
|
|
| sklearn.set_config(display='diagram') |
|
|
| data = pd.read_csv('insurance.csv') |
| df = data.copy(deep=True) |
|
|
| df = df.drop(columns=['index']) |
| df.drop_duplicates(inplace=True) |
|
|
| target = 'charges' |
| numeric_features = ['age', 'bmi', 'children'] |
| categorical_features = ['sex', 'smoker', 'region'] |
|
|
| print('Creating data subsets') |
|
|
| X = df[numeric_features + categorical_features] |
| y = df[target] |
|
|
| Xtrain, Xtest, ytrain, ytest = train_test_split( |
| X, y, |
| test_size=0.2, |
| random_state=42 |
| ) |
|
|
| Xtest = Xtest[['age', 'bmi', 'children', 'sex', 'smoker', 'region']] |
|
|
| preprocessor = make_column_transformer( |
| (StandardScaler(), numeric_features), |
| (OneHotEncoder(handle_unknown='ignore'), categorical_features) |
| ) |
|
|
| model_linear_regression = LinearRegression(n_jobs=-1) |
|
|
| print('Estimating Best Model Pipeline') |
|
|
| model_pipeline = make_pipeline( |
| preprocessor, |
| model_linear_regression |
| ) |
|
|
| model_pipeline.fit(Xtrain, ytrain) |
|
|
| print("Logging Metrics") |
| print(f"R-squared: {r2_score(ytest, model_pipeline.predict(Xtest))}") |
|
|
| print("Serializing Model") |
|
|
| saved_model_path = "model.joblib" |
|
|
| joblib.dump(model_pipeline, saved_model_path) |
|
|