text_plain_output_1.png
from sklearn import linear_model

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import LabelEncoder

import numpy as np

import numpy as np # linear algebra

import pandas as pd

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

df = pd.read_csv('../input/insurance/insurance.csv')

df.isnull().sum()

le = LabelEncoder()
df['le_region'] = le.fit_transform(df.region)
df['le_smoker'] = le.fit_transform(df.smoker)
df['le_sex'] = le.fit_transform(df.sex)

ml_df = df.loc[:, ('age', 'le_sex', 'bmi', 'children', 'le_region', 'le_smoker', 'charges')]
ml_df.rename(columns={'le_region': 'region', 'le_smoker': 'smoker', 'le_sex': 'sex'}, inplace=True)

columns = ml_df.columns
X = ml_df[columns[:-1]]
y = ml_df[columns[-1]]

X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.8)

lin_regr = linear_model.LinearRegression()
lin_regr.fit(X_train, y_train)
lw = lin_regr.coef_
lb = lin_regr.intercept_

ridge = linear_model.Ridge(alpha=0.5)
ridge.fit(X_train, y_train)
rw = ridge.coef_
rb = ridge.intercept_

lw = np.array(lw).reshape(lw.shape[0], 1).reshape(-1)
rw = np.array(rw).reshape(rw.shape[0], 1).reshape(-1)
coefs = pd.DataFrame(data=np.array([lw, rw, [lb] * lw.shape[0], [rb] * lw.shape[0]]).T, columns=['Linear_W', 'Ridge_W', 'Linear_b', 'Ridge_b'])
coefs.head()