-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMLModel.py
More file actions
154 lines (117 loc) · 3.85 KB
/
Copy pathMLModel.py
File metadata and controls
154 lines (117 loc) · 3.85 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# -*- coding: utf-8 -*-
"""model.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1Eil6PhEPgl1OUaTOWOe8-VQpYzbnJe-_
"""
import numpy as np
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.tree import plot_tree
from sklearn.metrics import accuracy_score
from pandas.plotting import parallel_coordinates
import matplotlib.pyplot as plt
import plotly.express as px
#we need to create some sample data in a csv format with parameters like age, race, gender, health history and then their respective policy tier
df = pd.read_csv("insurance_data.csv")
print(df)
X = df[['age', 'gender', 'race', 'income', 'smoker', 'alcohol', 'exercise', 'risk']]
y = df['policy_tier']
#splitting the data into training and test data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
#training the model
clf = DecisionTreeClassifier()
clf.fit(X_train, y_train)
print(X_test)
y_pred = clf.predict(X_test)
print(y_pred)
y_pred = clf.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy of the prediction is:")
print(accuracy * 100)
plt.figure(figsize=(20,10))
plot_tree(clf, feature_names=['age', 'gender', 'race', 'income', 'smoker', 'alcohol', 'exercise', 'risk'], class_names=['1', '2', '3', '4', '5', '6', '7', '8'], filled=True);
plt.show()
#@title Enter Parameters { display-mode: "code" }
#Age
Age = 35 #@param {type:"integer"}
#Gender
Gender = "Female" #@param ["Male", "Female"]
if Gender == "Male":
GENDER = 1
else:
GENDER = 0
#Race
Race = "Malay" #@param ["Chinese", "Malay", "Indian"]
if Race == "Chinese":
RACE = 2.0
elif Race == "Malay":
RACE = 3.0
else:
RACE = 1.0
#Income
Income = 5000 #@param {type:"integer"}
#Smoker
Smoker = False #@param {type:"boolean"}
if Smoker == False:
SMOKER = 0
else:
SMOKER = 1
#Alcohol
Alcohol = "Light" #@param ["Never", "Light", "Moderate", "Heavy"]
if Alcohol == "Never":
ALCOHOL = 0
elif Alcohol == "Light":
ALCOHOL = 1
elif Alcohol == "Moderate":
ALCOHOL = 2
else:
ALCOHOL = 3
#Exercise
Exercise = "Moderate" #@param ["Never", "Light", "Moderate", "Heavy"]
if Exercise == "Never":
EXERCISE = 0
elif Exercise == "Light":
EXERCISE = 1
elif Exercise == "Moderate":
EXERCISE = 2
else:
EXERCISE = 3
#Risk
Risk = "1" #@param ["1", "2", "3", "4", "5"]
new_customer = [[Age, GENDER, RACE, Income, SMOKER, ALCOHOL, EXERCISE, Risk]]
print(new_customer[0])
new_customer_policy_tier = clf.predict(new_customer)
print("Recommended policy tier for new customer:", new_customer_policy_tier[0])
print(new_customer[0][0])
AGE_NC = new_customer[0][0]
#groups the customers by age group
age_groups = df.groupby("age")
#calculates the proportion of customers in each age group that choose each policy tier
age_stats = age_groups["policy_tier"].value_counts(normalize=True)
#prints the statistics for the new customer's age group
print(age_stats)
print(new_customer[0][0])
#groups the customers by age group
age_groups = df.groupby("age")
#calculates the proportion of customers in each age group that choose each policy tier
age_stats = age_groups["policy_tier"].value_counts(normalize=True)
#prints the statistics for the new customer's age group
age = new_customer[0][0]
print(age_stats[age])
#counts the number of customers in each age group
age_counts = df['age'].value_counts()
#prints the counts for the new customer's age group
age = new_customer[0][0]
print(age_counts[age])
df.groupby('age').size()
#creating a scatter plot of income vs policy_tier
plt.scatter(df['income'], df['policy_tier'])
plt.xlabel('Income')
plt.ylabel('Policy Tier')
plt.title('Income vs Policy Tier')
#overlaying a single point for new customer data
plt.scatter(new_customer[0][3], clf.predict(new_customer), c='red', marker='*', s=200)
plt.show()