-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
69 lines (49 loc) · 1.78 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import pandas as pd
import pickle
insurance = pd.read_csv("insurance.csv")
insurance.head()
# Replacing string values to numbers
insurance['sex'] = insurance['sex'].apply({'male':0, 'female':1}.get)
insurance['smoker'] = insurance['smoker'].apply({'yes':1, 'no':0}.get)
insurance['region'] = insurance['region'].apply({'southwest':1, 'southeast':2, 'northwest':3, 'northeast':4}.get)
import seaborn as sns
# Correlation betweeen 'charges' and 'age'
sns.jointplot(x=insurance['age'],y=insurance['charges'])
# Correlation betweeen 'charges' and 'smoker'
sns.jointplot(x=insurance['age'],y=insurance['charges'])
# features
X = insurance[['age', 'sex', 'bmi', 'children','smoker','region']]
# predicted variable
y = insurance['charges']
# importing train_test_split model
from sklearn.model_selection import train_test_split
# splitting train and test data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4)
len(X_test) # 402
len(X_train) # 936
len(insurance) # 1338
# importing the model
from sklearn.linear_model import LinearRegression
model = LinearRegression()
# Fit linear model by passing training dataset
model.fit(X_train,y_train)
# Predicting the target variable for test datset
predictions = model.predict(X_test)
import matplotlib.pyplot as plt
plt.scatter(y_test,predictions)
plt.xlabel('Y Test')
plt.ylabel('Predicted Y')
# Predict charges for new customer : Name- devansh
data = {'age' : 40,
'sex' : 1,
'bmi' : 45.50,
'children' : 9,
'smoker' : 1,
'region' : 3}
index = [1]
devansh_df = pd.DataFrame(data,index)
devansh_df
prediction_devansh = model.predict(devansh_df)
print("Medical Insurance cost for devansh is : ",prediction_devansh)
pickle.dump(model,open('model.pkl','wb'))
model=pickle.load(open('model.pkl','rb'))