0% found this document useful (0 votes)
4 views4 pages

Untitled2 - Jupyter Notebook

The document describes a Jupyter Notebook that predicts whether a person will buy insurance based on their age using logistic regression. It includes data loading, model training, and evaluation, showing a high accuracy of 88.89% on both training and test datasets. The notebook also visualizes the relationship between age and insurance purchase likelihood with a logistic regression line.

Uploaded by

subho_b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Untitled2 - Jupyter Notebook

The document describes a Jupyter Notebook that predicts whether a person will buy insurance based on their age using logistic regression. It includes data loading, model training, and evaluation, showing a high accuracy of 88.89% on both training and test datasets. The notebook also visualizes the relationship between age and insurance purchase likelihood with a logistic regression line.

Uploaded by

subho_b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Untitled2 - Jupyter Notebook http://localhost:8890/notebooks/Desktop/Lakshya%2021-CSE-84/Untitl...

In [ ]: #Predicting if a person will buy an insurance or not based on age using logistic

In [3]: import pandas as pd


import matplotlib pyplot as plt
In [4]: df=pd.read_csv("insurance_data.csv")
df head()
Out[4]:
age bought_insurance

0 22 0

1 25 0

2 47 1

3 52 0

4 46 1

In [7]: df shape

Out[7]: (27, 2)

In [34]: plt scatter(df age df bought_insurance marker '+' color 'teal')

Out[34]: <matplotlib.collections.PathCollection at 0x178d6c14ee0>

In [35]: from sklearn.model_selection import train_test_split


X_train,X_test,y_train,y_test=train_test_split(df[['age']],df.bought_insurance,

1 of 4 14-09-2023, 15:31
Untitled2 - Jupyter Notebook http://localhost:8890/notebooks/Desktop/Lakshya%2021-CSE-84/Untitl...

In [36]: X_test shape

Out[36]: (9, 1)

In [37]: X_train shape

Out[37]: (18, 1)

In [38]: from sklearn.linear_model import LogisticRegression


model LogisticRegression()
In [39]: model fit(X_train y_train)

Out[39]: ▾ LogisticRegression
LogisticRegression()

In [40]: X_test

Out[40]:
age

17 58

14 49

18 19

3 52

23 45

10 18

20 21

21 26

4 46

In [41]: y_pred model predict(X_test)

In [42]: model (X_test y_test)

Out[42]: 0.8888888888888888

In [43]: model (X_train y_train)

Out[43]: 0.8888888888888888

In [47]: from sklearn.metrics import confusion_matrix


confusion_matrix(y_test y_pred)
Out[47]: array([[4, 1],
[0, 4]], dtype=int64)

In [48]: model coef_

Out[48]: array([[0.1312933]])

2 of 4 14-09-2023, 15:31
Untitled2 - Jupyter Notebook http://localhost:8890/notebooks/Desktop/Lakshya%2021-CSE-84/Untitl...

In [49]: model intercept_

Out[49]: array([-4.92847769])

In [50]: import numpy as np


def sigmoid(x):
return 1.0/(1 + np exp( x))
In [51]: def pred_function(age):
z = 0.131 * age - 4.928
y = sigmoid(z)
return
In [52]: age=35
pred_function(age)
Out[52]: 0.4150809257548097

In [55]: age=45
pred_function(age)
Out[55]: 0.7245211304910166

In [56]: response_Vec = sigmoid(0.131 * X_test - 4.928)


plt.scatter(X_test,y_test,marker='+',color='green',label='original data')
plt.plot(X_test,response_Vec,"r",label='logistic regression line')
plt.xlabel('Age')
plt.ylabel('Buy_Insurance')
plt.legend()
plt show()

In [ ]:

3 of 4 14-09-2023, 15:31
Untitled2 - Jupyter Notebook http://localhost:8890/notebooks/Desktop/Lakshya%2021-CSE-84/Untitl...

4 of 4 14-09-2023, 15:31

You might also like