-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathcreateDatabase.py
426 lines (346 loc) · 11.9 KB
/
createDatabase.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import sqlite3
from mimesis import Person
from random import randint, uniform
from faker import Faker
from datetime import date, datetime
import pickle
import calendar
import time
import openpyxl
from openpyxl.utils.dataframe import dataframe_to_rows
'''
Globals
'''
m_status = ("Married", "Single", "Divorced", "Widowed", "Separated")
c_loss = ("Fire", "Water", "Theft", "Natural Disaster")
n_loss = ("Borrowed", "Misplaced", "Donated")
i_insurer = ("Santam", "Hollard", "Outsurance", "Discovery", "Absa", "Mutual & Federal", "First for Woman", "Budget",
"Miway")
fraud_reasons = ("No Date of birth", "Date of birth calculated Age and Age do not match",
"Claim amount is more than Sum Insured",
"No Policy start date", "No Policy end date", "Policy end date before start date",
"Claim Date before loss", "No kind of loss", "Invalid kind of loss", "No premium but has claim",
"Claim after Policy end date", "Claim before Policy start", "Age is not in requirements")
person = Person('en')
fake = Faker()
mindate = datetime.strptime('Jun 1 1900 1:33PM', '%b %d %Y %I:%M%p')
maxdate = datetime.today()
'''
Functions
'''
'''
:param n - number of claims to insert
:param f - number of fraud claims
'''
def create_database_excel(n, f):
print("Creating Excel DB")
fraud = set([int(randint(0, n)) for i in range(f)])
pickle.dump(fraud, open("fraud-pickle.txt", "wb"))
text_file = open("fraud-index.txt", "w")
text_file.write("%s" % ', '.join(str(e) for e in fraud))
text_file.close()
from openpyxl import load_workbook, Workbook
wb = Workbook()
ws = wb.active
ws.append(["Calim_ID","Name","Surname","Age","Gender","Marital_Status","Date_Of_Birth","Sum_Insured","Policies_Revenue","Policy_Start","Policy_End",
"Fraudulent_Claim","Fraudulent_Claim_Reason","Date_Of_Loss","Date_Of_Claim","Broker_ID","Insured_ID","Kind_Of_Loss","Claim_Amount",
"Party_Name","Party_Surname","Service_Provider","Policy_Holder_Street","Policy_Holder_Province","Policy_Holder_City",
"Policy_Holder_Area","Policy_Holder_Postal","Province","City","Area","Postal_Code"])
for i in range(0, n):
if i not in fraud:
ws.append(get_data(True))
print("\rInserted: " + str(i), end="")
else:
ws.append(get_data(False))
print("\rInserted: " + str(i), end="")
print("\nAll data inserted successfully")
wb.save("insurance.xlsx")
print("Created Database table successfully!")
'''
:param n - number of claims to insert
:param f - number of fraud claims
'''
def create_database(n, f):
print("Creating SQLITE DB")
fraud = set([int(randint(0, n)) for i in range(f)])
# print(fraud)
pickle.dump(fraud, open("fraud-pickle.txt", "wb"))
text_file = open("fraud-index.txt", "w")
text_file.write("%s" % ', '.join(str(e) for e in fraud))
text_file.close()
conn = sqlite3.connect('insurance.db')
cur = conn.cursor()
print("Opened database successfully")
cur.execute('''CREATE TABLE IF NOT EXISTS Claims
(Claim_ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Name TEXT,
Surname TEXT,
Age INT,
Gender VARCHAR(8),
Marital_Status TEXT,
Date_Of_Birth DATE,
Sum_Insured REAL,
Policies_Revenue REAL,
Policy_Start DATE,
Policy_End DATE,
Fraudulent_Claim VARCHAR(1),
Fraudulent_Claim_Reason TEXT,
Date_Of_Loss DATE,
Date_Of_Claim DATE,
Broker_ID TEXT,
Insured_ID TEXT,
Kind_Of_Loss TEXT,
Claim_Amount REAL,
Party_Name TEXT,
Party_Surname TEXT,
Service_Provider TEXT,
Policy_Holder_Street TEXT,
Policy_Holder_Province TEXT,
Policy_Holder_City TEXT,
Policy_Holder_Area TEXT,
Policy_Holder_Postal TEXT,
Province TEXT,
City TEXT,
Area TEXT,
Postal_Code TEXT);''')
print("Created Database table successfully!")
for i in range(0, n):
if i not in fraud:
cur.execute("INSERT INTO Claims VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
get_data(True))
print("\rInserted: " + str(i), end="")
else:
cur.execute("INSERT INTO Claims VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
get_data(False))
print("\rInserted: " + str(i), end="")
conn.commit()
conn.close()
print("\nAll data inserted successfully")
'''
:param status - sends the function to generate a fraud claim or valid claim
'''
def get_data(status):
dob = random_date()
dateloss = rand_date("-40y", "now")
policystart = rand_date("-40y", "now")
suminsured = random_real(200000, 10000000)
dobiso = dob.isoformat()
policystartiso = policystart.isoformat()
datelossiso = dateloss.isoformat()
policyend = policy_end(policystart, True)
if policyend != None:
policyendiso = policyend.isoformat()
else:
policyendiso = None
if not status:
return get_fraud_data()
return (
null_val(),
person.name(),
person.surname(),
calculate_age(dob, status),
person.gender(),
marital_status(),
dobiso,
suminsured,
random_real(100, 5000),
policystartiso,
policyendiso,
"F", "",
datelossiso,
date_claim(dateloss, policystart, policyend, True),
"BKR" + str(randint(1000, 9999)), i_insurer[randint(0, len(i_insurer) - 1)],
c_loss[randint(0, len(c_loss) - 1)],
claim_amount(suminsured, True),
person.name(),
person.surname(),
fake.company(),
fake.street_name(),
fake.country(),
fake.city(),
fake.state(),
fake.postalcode(),
fake.country(),
fake.city(),
fake.state(),
fake.postalcode()
)
'''
function to generate fake data based on the status - False to the helper functions
'''
def get_fraud_data():
dob = random_date()
dateloss = rand_date("-40y", "now")
policystart = rand_date("-40y", "now")
suminsured = random_real(200000, 10000000)
r = randint(1, len(fraud_reasons)-1)
dobiso = dob.isoformat()
policystartiso = policystart.isoformat()
datelossiso = dateloss.isoformat()
if r == 1:
dob = ""
dobiso = ""
if r == 4:
policystart = ""
policystartiso = ""
policyend = policy_end(policystart, r)
if policyend != None:
policyendiso = policyend.isoformat()
else:
policyendiso = None
return (
null_val(),
person.name(),
person.surname(),
calculate_age(dob, r),
person.gender(),
marital_status(),
dobiso,
suminsured,
premium(r),
policystartiso,
policyendiso,
"T", fraud_reasons[r - 1],
datelossiso,
date_claim(dateloss, policystart, policyend, r),
"BKR" + str(randint(1000, 9999)), i_insurer[randint(0, len(i_insurer) - 1)],
c_loss[randint(0, len(c_loss) - 1)],
claim_amount(suminsured, r),
person.name(),
person.surname(),
fake.company(),
fake.street_name(),
fake.country(),
fake.city(),
fake.state(),
fake.postalcode(),
fake.country(),
fake.city(),
fake.state(),
fake.postalcode()
)
def premium(s):
if s == 10:
return 0
return random_real(100, 5000)
def kind_loss(s):
if s == 8:
return None
if s == 9:
return n_loss[randint(0, len(c_loss) - 1)]
return c_loss[randint(0, len(c_loss) - 1)]
def date_claim(loss, policystart, policyend, s):
if loss == "":
return None
if policyend == "":
return None
if policystart == "":
return None
if s == 7:
return date_between(mindate, loss).isoformat()
if s == 11:
return date_between(policyend, maxdate)
if s == 12:
return date_between(mindate, policystart)
return date_between(loss, maxdate).isoformat()
def policy_end(start, s):
if start == "":
return None
if s == 6:
return date_between(mindate, start)
if s == 5:
return None
return date_between(start, maxdate)
"""
This function will randomly generate a date between start and end dates provided
:param s - start date
:param e - end date
"""
def date_between(s, e):
y = randint(s.year, e.year)
m = randint(1, 12)
d = randint(1, 30)
if calendar.isleap(y):
if m == 2:
d = randint(1, 29)
if m == 2:
d = randint(1, 28)
h = randint(0, 12)
i = randint(0, 59)
s = randint(0, 59)
return datetime(y, m, d, h, i, s)
def claim_amount(val, s):
if s == 3:
r = randint(0, 100)
if r < 60:
return random_real(val, 9000)
if r > 60 & r < 80:
return random_real(val, 50000)
if r > 80 & r < 90:
return random_real(val, 90000)
if r > 90:
return random_real(val, 9000000)
return random_real(1, val)
"""
This function will calculate the age depending on the provided date of birth and status, where the status
will determine if a actual or fraudulent age must be created.
:param born - date of birth
:param s - status
"""
def calculate_age(born, s):
if s == 2:
return person.age()
if s == 4:
return person.age()
if s == 13:
if randint(0, 1) == 0:
return randint(-10, 15)
else:
return randint(120, 300)
if born == "":
return None
today = date.today()
return today.year - born.year - ((today.month, today.day) < (born.month, born.day))
def marital_status():
return m_status[randint(0, len(m_status) - 1)]
"""
This function will generate a random date between 1920 and 1999, which is all validated and caters for leap years
"""
def random_date():
y = randint(1920, 1999)
m = randint(1, 12)
d = randint(1, 30)
if calendar.isleap(y):
if m == 2:
d = randint(1, 29)
if m == 2:
d = randint(1, 28)
h = randint(0, 12)
i = randint(0, 59)
s = randint(0, 59)
return datetime(y, m, d, h, i, s)
def rand_date(start, end):
return fake.date_time_between(start, end)
def random_real(m, mm):
return round(uniform(m, mm), 2)
def null_val():
return None
'''
SCRIPT
eg. create_database(number of claims, number of fraud claims)
'''
start_time = time.time()
create_database_excel(100, 175)
print("--- %s seconds ---" % (time.time() - start_time))
print("\n")
start_time = time.time()
create_database(100, 175)
print("--- %s seconds ---" % (time.time() - start_time))
'''
Data Cleaning
- Check if DOB and age is correct
- check claim date
- policy expire
- amount claim vs insured
- check empty cells
'''