Insurance Price Prediction using Machine Learning

Free Machine Learning courses with 130+ real-time projects Start Now!!

Program 1

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model
from sklearn.model_selection import  train_test_split
from sklearn.metrics import r2_score
df=pd.read_csv("D://scikit_data/insurancedata/insurance.csv")
map_dict={'female':0,'male':1}
df['sex']=df['sex'].map(map_dict)
map_dict={'southwest':0,'southeast':1,'northwest':2,'northeast':3}
df['region']=df['region'].map(map_dict)
map_dict={'yes':1,'no':0}
df['smoker']=df['smoker'].map(map_dict)
# print(df)
# print(df.isnull().sum())
x=df.drop('charges',axis='columns')  # Independed Variables
y=df.charges  # Depended Variables
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=0)

# x_train---> Training data set for Independed variable
# y_train---> Training data set for depended variable
# x_test---> Testing data set for Independed variable
# y_test---> Testing data set for depended variable

model=linear_model.LinearRegression()
model.fit(x_train,y_train)
# Predication by training dataset

y_pred=model.predict(x_train)
#print(y_pred)
plt.scatter(y_train,y_pred,color='red')
plt.xlabel("Actual Price")
plt.ylabel("Predicated Price")
plt.show()
print("R2 Score for Training data:",r2_score(y_train,y_pred))

# Predication by test dataset
y_pred=model.predict(x_test)
#print(y_pred)
plt.scatter(y_test,y_pred,color='blue',marker='+')
plt.xlabel("Actual Price")
plt.ylabel("Predicated Price")
plt.show()
print("R2 Score for Testing data:",r2_score(y_test,y_pred))






# print(model)
# print("Coeficent values")
# print(model.coef_)
# print("Intercept values")
# print(model.intercept_)
# print(x)
# print(y)

 

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *