text_plain_output_1.png
from sklearn.linear_model import LinearRegression

from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score

import numpy as np # linear algebra

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

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

df.describe().T

df.shape

df.isna().sum()

df.duplicated().sum()

df = df.drop_duplicates()
df.duplicated().sum()

df.sex.unique()

df.region.unique()

df.smoker.value_counts()

df.smoker.replace({'no': 0, 'yes': 1}, inplace=True)

df.corr()

df_categorical_col = df.select_dtypes(exclude=np.number).columns
df_categorical_col

df_numeric_col = df.select_dtypes(include=np.number).columns
df_numeric_col

model = LinearRegression()

model.fit(train_x, train_y)

print('Predicting train data')
train_predict = model.predict(train_x)
print('Predicting test data')
test_predict = model.predict(test_x)
print(' ')
print('MAE')
print('Train data: ', mean_absolute_error(train_y, train_predict))
print('Test data: ', mean_absolute_error(test_y, test_predict))
print(' ')
print('MSE')
print('Train data: ', mean_squared_error(train_y, train_predict))
print('Test data: ', mean_squared_error(test_y, test_predict))
print(' ')
print('RMSE')
print('Train data: ', np.sqrt(mean_squared_error(train_y, train_predict)))
print('Test data: ', np.sqrt(mean_squared_error(test_y, test_predict)))
print(' ')
print('R^2')
print('Train data: ', r2_score(train_y, train_predict))
print('Test data: ', r2_score(test_y, test_predict))