Loading...

Credit Card Fraud

In this example, we use vastorbit to detect fraudulent credit card transactions. You can download the dataset here.

The Credit Card Fraud Detection dataset contains credit card transactions from September 2013 by European cardholders. It contains numerical input variables from a principal component analysis (PCA) transformation.

To preserve the cardholders’ confidentiality, we cannot access the original features and background information about the data.

Time and Amount are the only features that have not been transformed with PCA.

  • V1, V2,…, V28: principal components from PCA.

  • Time: Number of seconds elapsed between this transaction and the first transaction in the dataset.

  • Amount: Transaction amount.

  • Class: Response variable, where a value of 1 indicates fraudulent activity.

Amount will be useful for example-dependent cost-sensitive learning.

We will follow the entire Data Science cycle (Data Exploration, Data Preparation, Data Modeling, Model Evaluation, Model Deployment) to solve this problem.

Initialization

This example uses the following version of vastorbit:

import vastorbit as vo

vo.__version__

Connect to VAST. This example uses an existing connection called VASTDSN. For details on how to create a connection, see the Connection tutorial. You can skip the below cell if you already have an established connection.

vo.connect("VASTDSN")

Let’s create a VastFrame of the dataset.

creditcard = vo.read_csv("creditcard.csv")
creditcard.head(5)
123
time
Integer
100%
123
v1
Double
100%
123
v2
Double
100%
123
v3
Double
100%
123
v4
Double
100%
123
v5
Double
100%
123
v6
Double
100%
123
v7
Double
100%
123
v8
Double
100%
123
v9
Double
100%
123
v10
Double
100%
123
v11
Double
100%
123
v12
Double
100%
123
v13
Double
100%
123
v14
Double
100%
123
v15
Double
100%
123
v16
Double
100%
123
v17
Double
100%
123
v18
Double
100%
123
v19
Double
100%
123
v20
Double
100%
123
v21
Double
100%
123
v22
Double
100%
123
v23
Double
100%
123
v24
Double
100%
123
v25
Double
100%
123
v26
Double
100%
123
v27
Double
100%
123
v28
Double
100%
123
amount
Double
100%
123
class
Integer
100%
175260.00843036489558254.13783683497998-6.240696571947446.67573216313440.768307024571449-3.35305954788994-1.631734672718090.15461244822474-2.79589246446281-6.187890629706475.66439470857116-9.85448482287037-0.306166658250084-10.6911962118171-0.638498192673322-2.04197379107768-1.129055877035850.116452521226364-1.934665738897270.4883782211347150.36451420978479-0.608057133838703-0.5395279418200930.1289399829918131.488481210068680.507962677823850.7358216361196620.5135737406794371.01
276720.7027099000987532.42643280600508-5.234513295840524.41666124290876-2.17080621591773-2.66755356121463-3.878088454835720.911337122229195-0.166199039175942-5.009248502127514.67572941865677-8.167188051730890.638559282180499-6.763334390623221.29686025605627-3.81175840977789-3.75412806618729-1.049177402279061.554197263458970.4227431291987020.551179689117248-0.00980235731325310.7216982300694150.473245751402033-1.95930377116870.319475540107460.6004849164863590.1293052250965661.01
38415-0.251470960068234.31352332575143-6.891437716501476.796796680551030.616296505250374-2.96632654187403-2.436652526877140.489327898838014-3.37163857159343-6.810813099382157.62008905836908-10.2852825163315-0.342444334069655-11.5434979157865-1.3349879032835-2.68928420445067-3.204383402957620.0865218956911394-1.314495096653070.6327099637898260.536892074447128-0.546126023729244-0.605239547613772-0.2637434595935931.539915830778680.5235736966808710.8910246883652690.5727406665495841.01
48614-2.169928975590823.6396539992044-4.508497786177282.73066814884895-2.12269287053048-2.34101682766366-4.235253082906131.70353765763508-1.30527913344307-6.71672002271276.35361231915591-8.601648262764640.449930038237225-7.50616937408145-0.438081781812467-3.69451599830956-6.30475338603049-1.267587127881150.3579870304842530.50077905395070.645103275621378-0.503529448911197-0.00052282181829920.07169577876767420.09200743342472080.3084979315770360.5525909146538410.2989544787093991.01
58757-1.863755553437323.44264397684973-4.468259731455112.80533625940251-2.1184124774924-2.33228488731467-4.26123719744041.70168184397858-1.43939587542941-6.999906633865226.31620967716699-8.670818004762260.316023991908777-7.41771206475053-0.436537471366146-3.65280196004741-6.29314531907376-1.243248291338520.3648104821938870.360924003720930.66792657242141-0.516242362128804-0.0122178117468390.0706137031034450.05850446695606280.3048828395885840.4180124671852580.2088582781458391.01

Warning

This example uses a sample dataset. For the full analysis, you should consider using the complete dataset.

Data Exploration and Preparation

Let’s explore the data by displaying descriptive statistics of all the columns.

creditcard.describe()
countmeanstdminapprox_25%approx_50%approx_75%max
"time"29014.095426.7250637623247789.8786676195450.054819.089833.0140026.0172788.0
"v1"29014.0-0.082812144271871782.2301169191918975-36.8023199088745-0.9548255831015930.020043476718130561.32847220015657452.42250773211137
"v2"29014.00.0739603433428471.7867361964240793-63.3446983175027-0.56604877507558390.090045210797978270.83572437619520922.0577289904909
"v3"29014.0-0.125791970220911351.9668616216741108-31.8135859546007-0.95768535401437360.159518461875061471.00056718912868164.06986478102804
"v4"29014.00.074740845687112811.562890013936606-5.68317119816995-0.83603727409329980.0197391206933018450.823636975646927316.7155373723131
"v5"29014.0-0.037933604634512511.577142811267928-25.7911323347667-0.6897514588154391-0.046566427854059220.631725674510688823.5898035308963
"v6"29014.0-0.01973746072833691.3557673956514278-17.1185917920642-0.7866909465522046-0.287136614758598860.3737671399216463416.4932270978583
"v7"29014.0-0.07412789354965941.654085625219281-43.5572415712451-0.56699618655220230.0364120614744858350.572106930164331628.0698221389603
"v8"29014.00.0152687320402927591.4120563863339206-41.0442609210741-0.209773349404995150.0222074946561475060.3346414570686670520.0072083651213
"v9"29014.0-0.018744482031525951.1879693490174459-13.4340663182301-0.6532385452310063-0.0492718109266053060.597109566456782710.3139737345822
"v10"29014.0-0.091712251384880771.4401948070195507-24.5882624372475-0.57282736983338-0.115749523428321480.417379479427003715.2360282040071
"v11"29014.00.054298835411478861.1644026569523815-4.04989491435061-0.7518711485072506-0.0166974426250604820.760506856764548212.0189131816199
"v12"29014.0-0.09411647915468171.3802096346921473-18.6837146333443-0.432560523351783970.12459777218847240.60935025510598874.14780434342835
"v13"29014.00.0058845336776002481.003366904063213-3.81644762329776-0.6332647398342058-0.0198483939985114040.67372406903657543.77962042833723
"v14"29014.0-0.118121553475406151.390132701835722-19.2143254902614-0.467752254958522740.0310839038979989250.482259853379876546.94048384657416
"v15"29014.00.00691470855780671350.9226169812462099-4.49894467676621-0.58632961075713310.0511744502221624440.66105960807285454.44117661446309
"v16"29014.0-0.063221676644182491.1115654930397227-14.1298545174931-0.49998214104912390.0582707322354569440.50866439483535675.02188051133651
"v17"29014.0-0.100211829870551951.4647062170670861-25.1627993693248-0.504238107705212-0.074243855381999470.416002310375893977.61186152343735
"v18"29014.0-0.028674682789236420.9435930891860623-9.49874592104677-0.5092200688977337-0.0062150698057138190.50081567275279295.04106918541184
"v19"29014.00.0042452389472377780.8397527209853632-7.21352743017759-0.44865923804137453-0.0012848673714002390.46196473416176635.2283417900513
"v20"29014.00.0039235880746437210.8172200939582945-22.8385483047643-0.21147876012991243-0.059044930508111340.1410674205073225239.4209042482199
"v21"29014.00.0105547264778264560.8505444530861069-22.7976039055519-0.22515757011088944-0.022799699822533610.1929634474941106227.2028391573154
"v22"29014.0-0.00308355774241382640.7408052031898827-10.933143697655-0.54546424574446650.0086198140630755990.52605756972722048.36198519168435
"v23"29014.00.0012985470530803010.6780581861688931-44.8077352037913-0.1643539120577957-0.0126956411954441960.1565041781283425817.7684617982855
"v24"29014.00.00121631642946266770.6025546718887111-2.81489763570598-0.346659076308731330.046386054941499650.43879270374883233.52024099866823
"v25"29014.00.00106563159575804880.5325915965279894-10.2953970749851-0.31793776099231940.0200239973256285360.35517517262236947.51958867870916
"v26"29014.0-0.00095506880952468650.4852314090776167-2.53432972105675-0.3300670688248193-0.0484214917396073450.24842521017221042.95209267905604
"v27"29014.00.0036537533317968430.43729853331388463-8.87866514836831-0.071523748279481760.0042304472248606160.09723079608961996.26770908866261
"v28"29014.00.000293421600286424930.34715171383772586-8.2779240852007-0.053096053500728770.0108239493184201660.0790266728996681122.6200722185803
"amount"29014.088.50709312745573267.159139865894530.05.78790688360910721.421716681301775.9058040394281619656.53
"class"29014.00.016957330943682360.129113726742673880.00.00.00.01.0

It’ll be difficult to work on the principal components (V1 through V28) without knowing what they mean. The only features we can work on are Time and Amount.

Let’s convert the number of seconds elapsed to the correct date and time. We know that the records were ingested in September 2013, so we’ll use that to create the new feature.

creditcard["Time"].apply("DATE_ADD('second', CAST({} AS bigint), TIMESTAMP '2013-09-01 00:00:00')")
📅
time
Timestamp(0)
100%
123
v1
Double
100%
123
v2
Double
100%
123
v3
Double
100%
123
v4
Double
100%
123
v5
Double
100%
123
v6
Double
100%
123
v7
Double
100%
123
v8
Double
100%
123
v9
Double
100%
123
v10
Double
100%
123
v11
Double
100%
123
v12
Double
100%
123
v13
Double
100%
123
v14
Double
100%
123
v15
Double
100%
123
v16
Double
100%
123
v17
Double
100%
123
v18
Double
100%
123
v19
Double
100%
123
v20
Double
100%
123
v21
Double
100%
123
v22
Double
100%
123
v23
Double
100%
123
v24
Double
100%
123
v25
Double
100%
123
v26
Double
100%
123
v27
Double
100%
123
v28
Double
100%
123
amount
Double
100%
123
class
Integer
100%
12013-09-01 02:05:260.00843036489558254.13783683497998-6.240696571947446.67573216313440.768307024571449-3.35305954788994-1.631734672718090.15461244822474-2.79589246446281-6.187890629706475.66439470857116-9.85448482287037-0.306166658250084-10.6911962118171-0.638498192673322-2.04197379107768-1.129055877035850.116452521226364-1.934665738897270.4883782211347150.36451420978479-0.608057133838703-0.5395279418200930.1289399829918131.488481210068680.507962677823850.7358216361196620.5135737406794371.01
22013-09-01 02:07:520.7027099000987532.42643280600508-5.234513295840524.41666124290876-2.17080621591773-2.66755356121463-3.878088454835720.911337122229195-0.166199039175942-5.009248502127514.67572941865677-8.167188051730890.638559282180499-6.763334390623221.29686025605627-3.81175840977789-3.75412806618729-1.049177402279061.554197263458970.4227431291987020.551179689117248-0.00980235731325310.7216982300694150.473245751402033-1.95930377116870.319475540107460.6004849164863590.1293052250965661.01
32013-09-01 02:20:15-0.251470960068234.31352332575143-6.891437716501476.796796680551030.616296505250374-2.96632654187403-2.436652526877140.489327898838014-3.37163857159343-6.810813099382157.62008905836908-10.2852825163315-0.342444334069655-11.5434979157865-1.3349879032835-2.68928420445067-3.204383402957620.0865218956911394-1.314495096653070.6327099637898260.536892074447128-0.546126023729244-0.605239547613772-0.2637434595935931.539915830778680.5235736966808710.8910246883652690.5727406665495841.01
42013-09-01 02:23:34-2.169928975590823.6396539992044-4.508497786177282.73066814884895-2.12269287053048-2.34101682766366-4.235253082906131.70353765763508-1.30527913344307-6.71672002271276.35361231915591-8.601648262764640.449930038237225-7.50616937408145-0.438081781812467-3.69451599830956-6.30475338603049-1.267587127881150.3579870304842530.50077905395070.645103275621378-0.503529448911197-0.00052282181829920.07169577876767420.09200743342472080.3084979315770360.5525909146538410.2989544787093991.01
52013-09-01 02:25:57-1.863755553437323.44264397684973-4.468259731455112.80533625940251-2.1184124774924-2.33228488731467-4.26123719744041.70168184397858-1.43939587542941-6.999906633865226.31620967716699-8.670818004762260.316023991908777-7.41771206475053-0.436537471366146-3.65280196004741-6.29314531907376-1.243248291338520.3648104821938870.360924003720930.66792657242141-0.516242362128804-0.0122178117468390.0706137031034450.05850446695606280.3048828395885840.4180124671852580.2088582781458391.01
62013-09-01 02:26:48-4.617217204155811.69569365346656-3.114372200794914.32819855298178-1.87325699086527-0.989908135538263-4.577264626580380.47221615845190.472016953377646-5.576022636424154.80232276125089-10.8331644693140.104303875698575-9.40542306160986-0.80747786866047-7.55234220350747-9.8025617903912-4.120628834665521.74050729036196-0.03904593433841860.4818296972400670.1460230564459480.117038527549065-0.21756459883966-0.138776043706563-0.424452881068275-1.002041425972270.8907802879706551.11
72013-09-01 03:13:49-3.891191951230867.09891625215749-11.42646709679618.60755678650129-2.06570621026229-2.98528802203595-8.138589416938612.97392807853452-6.27279046836547-13.193415066523211.6197234753825-17.6316063138707-0.355194389515515-18.8220867423816-1.2831290856146-10.0310966568698-15.2270075099256-5.32200898058982-0.5017506534525791.382618999672751.75708522742867-0.18970915318715-0.508629244173448-1.189308168773941.188536499347910.6052418859136261.88152875241480.8752604072223751.01
82013-09-01 03:42:03-5.454361779396738.28742055534983-12.75281127293868.59434189301081-3.10600228114338-3.17994875686414-9.252793937958314.24506220985367-6.32980084623466-13.136698369103911.228470279576-17.1313009454468-0.169401056814124-18.0499976898594-1.36623566099065-9.7235653091894-14.7449024646768-5.24730110631125-0.5746751437958171.305861914834371.84616479291417-0.267171794223081-0.310803969751621-1.201685457998061.352176095024330.6084245963604031.574714783842040.8087252050902331.01
92013-09-01 03:54:33-4.153014498733578.20479650456012-15.031714209748110.330099825448-3.99442609736294-3.25001318447365-10.41569781190794.62080391044331-5.71124797098224-11.797181067577711.2779207278067-16.72833933209150.2413676825577-17.7216383537133-0.387299923381284-10.3220166783853-13.9590853730538-5.030710038318321.197266335046331.412624880194921.976988396098870.256510487175280.485908106513804-1.19882131436036-0.5265673946878450.6348735024834781.627209076239520.7232350685183161.01
102013-09-01 04:52:00-5.268053220893639.06761342731767-15.960728134514410.2966027898053-4.70824109122294-3.39537485177836-11.16105700287975.49996263444947-5.66737554592835-11.627193555657911.0270590938161-16.38805416683270.363921486512734-17.230202160711-0.437487559248993-10.1223919213552-13.6392089915234-4.986457258442291.126784378196311.455878195282.004109945227410.1910583993843870.622927669875126-1.20926355739155-0.3747988626426660.648798036795461.584697346772240.7200558702454921.01
112013-09-01 04:57:18-5.187878108366356.96770866359565-13.5109311390958.61789514115238-11.21442239394580.6722478387604-9.462532810883555.32870420774381-4.89700552817542-11.78681165560419.36907905765884-15.09416314938651.25637700700759-11.85216130378240.274429814172421-10.6882417917386-18.3888105354245-6.898840133018392.38280793552097-0.6237372915741862.086083001074060.7601902408663590.716805785858217-0.646743429628277-1.617042925716240.1723468895032380.626646780979254-0.169726031129409766.361
122013-09-01 05:11:15-12.33960315741224.48826730168204-16.587072937363210.1072738734101-10.42019898274310.130669811857349-15.6003233044816-1.15769598093331-5.3046309683427-12.93892931077068.80568196718575-13.55613013014681.16546376028499-9.809881502070550.369987279150665-9.50521044515487-17.5420303090874-6.792637787231532.06937744931751-0.0855014634055812-2.08960963322321.745314500144051.37681580939778-0.554271418366818-1.610740851961740.1537254445367681.2124772080589-1.86929047595671188.781
132013-09-01 05:33:31-14.72462701192537.87515679273047-21.872317364456611.9061699078901-8.34873369160876-2.26284641969245-15.83344278196060.0778736741759969-6.35683349086288-13.261651708266710.0637897462894-14.39476680167210.654888723464231-14.2483158270781-0.305360761401071-8.16163244506225-12.2809648581754-4.818586393429740.7197876821087910.996468755724792-2.362344927518841.099557295769751.03719942301307-1.03635934178889-0.2547765141543750.6423432010181442.16112922373151-1.401282019638581.01
142013-09-01 05:56:59-17.467710011788710.1148157246654-24.202142232915811.8054692105913-10.1980458075926-2.5799380080012-17.65678799648372.25690247596699-6.24214930949065-12.83065719964179.44266526535108-13.54748589994440.960728558325579-13.0287170264518-0.426674498701321-7.65266166506753-11.4853277896478-4.721369575531020.5505190041495651.00151850195952-2.328024416210570.9408303193501781.29681706144136-1.055103903599670.111792028053620.6796946122034182.09354057112569-1.425491453773611.01
152013-09-01 06:58:151.192395989907681.33897371069007-0.6788761866870093.12367230712220.643244863589275-1.184322994312150.397585526006899-0.2534985382616580.41113497684127-0.8598623018103011.124059382453-3.763873848323980.367975966769142-0.971757538368114-0.01388251323577761.457578527630762.611450334478761.2919552295122-1.56381522671193-0.185454650142832-0.377503243008699-0.889596747960403-0.07420758939641930.03544551641395610.550577974200231-0.0271712789934873-0.02492062122662120.07360539866340453.121
162013-09-01 07:22:03-18.47486790344111.5863805198184-21.40291681313916.03851541556808-14.4511581398683-4.14652350569915-14.85612367317812.4311404723263-4.05335328722588-9.040396248944715.96620250720685-8.463966129525710.0786921317878341-9.092532651812950.0108220973904713-7.1863755078292-13.7974745003699-4.958493909710891.321166559826171.57792440114741.74113559593371-1.25113794883836-0.3962191261164750.09570555557188951.32275094272025-0.2179545648635731.628792531470160.48224827486540299.991
172013-09-01 07:22:36-19.179826414587311.8179219897853-21.91917358078436.0862356342864-14.7088447898427-4.30888760741835-15.357951806583612.8571650181242-3.99986076978115-8.92865566087515.8492930661792-8.261649837224130.153829148314596-8.829359227664090.0088788388257268-7.0709530089254-13.6297209427085-4.95882981584791.272091363532111.5729496149231.74680154191934-1.35314876613352-0.7629650727312180.1170278519037431.29799375563032-0.2248250570485251.621052389669890.484614402726499.991
182013-09-01 07:23:05-19.856322333443312.0958932259299-22.46408274648766.11554110191771-15.1480215209615-4.34672408349599-15.648507472266113.2768048056341-3.97416161954102-8.859194058808845.73081553744459-8.088033512136510.230824969324902-8.578973273334540.000946636693186-6.94774606726094-13.4728970076501-4.940210828868021.230142551938021.582929554921871.75072984045421-1.40963570908141-0.8098092783338820.1213966599058851.35030007077468-0.2242918735255851.597621368352540.47691981802543899.991
192013-09-01 07:28:51-22.561699259129813.2089042844176-24.64381877712266.23253182330679-16.905611362323-4.49743871970001-16.810184191932114.9551067734604-3.87129682308601-8.581265516263395.25698760960799-7.393614992150090.538800829120431-7.57755201800709-0.030725531904052-6.45478323655342-12.8456565242928-4.865917737201691.062441850300671.621444224721261.76598663639159-1.6355166467436-0.9983165034520870.1389722273617251.55935014018173-0.222124516691.50442466002240.44591982610977699.991
202013-09-01 07:57:05-27.8481806719815.5981926625554-28.9237559451046.41844174657532-20.346228155413-4.82820246578157-19.210896417908618.329405525597-3.66873493861418-8.009159386397774.30309581759563-6.008660006685611.1389688287939-5.58044712038062-0.11073711578577-5.49107326163426-11.5885435992809-4.715420171231150.7345734930164041.697856263183661.8021491336386-2.06293427611466-1.269843436159240.1654091884922231.99949911253199-0.2110585224319971.324809492795080.38809017773210799.991

When performing machine learning, we’ll take the data from two days and split it into a training set (first day) and a test set (second day).

creditcard["Time"].describe()
value
name"time"
dtypetimestamp(0)
count29014
min2013-09-01 00:00:00
max2013-09-02 23:59:48

Fraudulent activity probably isn’t uniform across all hours of the day, so we’ll extract the hour from the time and see how that influences the prediction.

import vastorbit.sql.functions as fun

creditcard["hour"] = fun.hour(creditcard["Time"])
creditcard[["Time", "hour"]]
📅
time
Timestamp(0)
100%
123
hour
Bigint
100%
12013-09-01 02:05:262
22013-09-01 02:07:522
32013-09-01 02:20:152
42013-09-01 02:23:342
52013-09-01 02:25:572
62013-09-01 02:26:482
72013-09-01 03:13:493
82013-09-01 03:42:033
92013-09-01 03:54:333
102013-09-01 04:52:004
112013-09-01 04:57:184
122013-09-01 05:11:155
132013-09-01 05:33:315
142013-09-01 05:56:595
152013-09-01 06:58:156
162013-09-01 07:22:037
172013-09-01 07:22:367
182013-09-01 07:23:057
192013-09-01 07:28:517
202013-09-01 07:57:057

We can visualize the frequency of fraudulent transactions throughout the day with a histogram.

creditcard["hour"].hist(method = "avg", of = "Class")

It seems like most fraudulent activity happens at night.

The transaction amount also likely differs between fraudulent and genuine transactions, so we’ll look at that relationship with a bar chart. Notice that fraudulent transactions tend to be larger purchases.

creditcard["Class"].bar(
    method = "avg",
    of = "Amount",
)

Let’s create some new features and move forward from there.

Features Engineering

Since all data (besides Time and Amount) are encoded, we’re somewhat limited in creating features. One way to work with this limitation for time series is with moving windows.

In lieu of customer IDs, we’ll aggregate on the transaction amount over some partitions. Let’s compute some features to analyze the transaction amount and frequencies across different windows: 5 hours preceding, 5 minutes preceding, and 5 seconds preceding. Choosing these windows is pretty subjective, but we can close in on the most relevant windows after some more extensive testing.

creditcard.rolling(
    name = "nb_same_transactions_mn_5h",
    func = "COUNT",
    columns = "Amount",
    window = ("- 5 hours", "0 hour"),
    by = ["Amount"],
    order_by = ["Time"],
)
creditcard.rolling(
    name = "nb_same_transactions_mn_5m",
    func = "COUNT",
    columns = "Amount",
    window = ("- 5 minutes", "0 minute"),
    by = ["Amount"],
    order_by = ["Time"],
)
creditcard.rolling(
    name = "nb_same_transactions_mn_5s",
    func = "COUNT",
    columns = "Amount",
    window = ("- 5 seconds", "0 second"),
    by = ["Amount"],
    order_by = ["Time"],
)
📅
time
Timestamp(0)
100%
123
v1
Double
100%
123
v2
Double
100%
123
v3
Double
100%
123
v4
Double
100%
123
v5
Double
100%
123
v6
Double
100%
123
v7
Double
100%
123
v8
Double
100%
123
v9
Double
100%
123
v10
Double
100%
123
v11
Double
100%
123
v12
Double
100%
123
v13
Double
100%
123
v14
Double
100%
123
v15
Double
100%
123
v16
Double
100%
123
v17
Double
100%
123
v18
Double
100%
123
v19
Double
100%
123
v20
Double
100%
123
v21
Double
100%
123
v22
Double
100%
123
v23
Double
100%
123
v24
Double
100%
123
v25
Double
100%
123
v26
Double
100%
123
v27
Double
100%
123
v28
Double
100%
123
amount
Double
100%
123
class
Integer
100%
123
hour
Bigint
100%
123
nb_same_transactions_mn_5h
Bigint
100%
123
nb_same_transactions_mn_5m
Bigint
100%
123
nb_same_transactions_mn_5s
Bigint
100%
12013-09-01 02:33:501.17437383415169-0.07270202394147541.01576020491131.06300518017119-0.6076081716508590.175083128023047-0.5628530962452950.06592841712260582.27218502890066-0.731408777498810.180255199516083-1.851365632503151.45301002762240.987058783482282-1.37440280916387-0.8015741111162161.27191389585795-0.809173737486902-0.197829290135556-0.217344575153148-0.27284106486129-0.154108514168127-0.01414578538866280.09432175441530270.4177112064989250.519504926440657-0.01422881887384660.00216758993504610.0102111
22013-09-01 02:33:58-0.9698036815693170.769958996014622.16006987107581-0.12500381056439-0.00931678643151310.1641947816825550.323466311285079-0.07613603067780782.36416441509351-0.629056532935246-0.145063085520417-2.837652706822390.1495489581510640.733188078669621-1.84081026215747-0.7361448391571240.966306276262094-0.627522958541049-0.731946291750037-0.140714373673462-0.233070788483237-0.0438031212641579-0.2125758504511830.0471091776375942-0.1350726619623190.299978295927844-0.272313978545899-0.05311061673006440.0102221
32013-09-01 02:34:02-0.5956012934512890.6918930993897052.29307258867392-0.09170001081540690.09537410410164590.1986166992361260.447361948542058-0.03153953957640731.99068418359368-0.9952586197424640.020224467527339-2.252092002107921.245265205237570.618599333584601-1.9848242355799-0.7481690582026120.922381147953346-0.740087208990059-0.747780040646149-0.036726520466188-0.2778275253233640.0526942240813936-0.2414028412449090.0793516537010455-0.06161273338353260.3357788347954320.152496334398776-0.0584638989995610.0102332
42013-09-01 02:34:20-1.273936219460761.197957053948331.94502633073767-1.156205587526051.642765395689641.598266955575831.47646090059973-1.049944768948353.785954966906222.277950635950560.944067345412176-2.325183422627311.85073742570544-1.06747956228605-2.35418037690081-0.703954080022561-0.659756701242662-0.00788843249983850.02779818500816221.29030318956994-0.81218281364009-0.0815514112954326-0.630404446080741-1.727842250772170.3070198909391470.280207952451237-0.204370384168685-1.066931907032120.0102441
52013-09-01 02:34:37-7.367956638329282.831808319167461.52366227152524-1.605167332971020.904577659334205-0.8122135576552943.66992806153215-3.6560397330544210.313973734582211.93301715804292.57245057627018-3.284346278362330.993339261554329-5.05630303704097-1.1056914989802-2.32772998831129-1.23002287365592-1.52023096558822-1.198116611138893.4237101586647-2.59032641806292-0.0408320118500587-0.06851507126570480.8480477336762841.356158466898070.01405506976503440.529443182282133-0.402429079710910.0102551
62013-09-01 02:35:04-5.08476024104213-0.5293743574310050.8833418952421730.2449903770657420.411750426244966-0.318722759426867-0.52822931296115-0.4128291799479423.376368403444751.381556418079281.89623112092331-1.75373442549320.4212180174011830.591739803262256-2.22911749610749-0.2861678730655920.76913817787665-0.359209783197381-0.294389791872219-1.80553251534569-0.4784234801768820.1547280973747161.785309112947360.2104177461805140.7675392630481820.4695579280584571.29777707210128-0.7905986432029320.0102661
72013-09-01 02:35:12-1.979649762017671.188994414531791.94176417806816-0.341000178127457-0.0619011087652189-0.4258299231435120.764880557297243-0.4915263434450883.654751614431891.38457916283083-0.0829862446270208-3.12563752097352-0.0144146915916897-0.0227658132110199-1.91890441062163-0.6617370785481460.43842792806484-0.292756168144771-0.5186191410151770.671987309894378-0.664924923468304-0.21146394907464-0.05550336241364290.3334710012814410.292705106010870.293389874875450.451755573427172-0.05801418634023250.0102771
82013-09-01 02:35:17-2.123736518178351.060444196301341.93684837750265-0.2976862240436880.145607805009528-0.4810183368233820.758801536789849-0.5523695308152293.527472175555071.382766105476980.0968091640385376-2.564033815144331.03262848066368-0.232143280295274-2.01523620796584-0.7025372250932240.374519735580486-0.42312054130842-0.5213242125025570.72876348885787-0.662151752131347-0.14582653765088-0.1230789758833430.3595550903403290.3279923349651410.2860174459212490.3742652957966150.1380839351627530.0102882
92013-09-01 02:35:26-1.865565822355321.068402091352981.48629121283453-0.0544833316771568-0.32016714880878-0.0295060597235864-0.1936226397449850.6067064406485072.1713682735924-0.681658050915958-0.180041752069633-2.05014298263951.224187708124820.980729546534914-1.97891325647769-0.5764713709364821.16640253131928-0.689931118255974-0.789061021823943-0.0257229211969707-0.251512837671866-0.01923452509579260.125574581328080.0599907584029770.1435725575875120.3709378003730730.2324545569498060.3314137552056650.0102991
102013-09-01 02:35:481.200535116486260.0260791106085390.8995969340739320.966336711394623-0.3043544093036880.408092054597179-0.4681118949596190.00226324123606922.07084894469618-0.7746125402275330.114506190324796-1.174605400714563.009026451868190.668576075577221-1.51287228830024-0.7675490005274721.05662931872346-0.876076429833917-0.072956443909761-0.110596803382562-0.279141762988326-0.0967797501068228-0.077075378737238-0.2378053657645050.5157640392680440.537376238227258-0.00484245975281010.00160729763137750.010210101
112013-09-01 02:36:131.15134645917296-0.04687082225510661.160500686956551.13957581233298-0.750849120334911-0.0141649882830707-0.5389501241466660.02993388005189572.21463703444538-0.7386122381907860.512658196503496-1.618453198225081.629141215387860.960414983184585-1.42476172068234-0.9083318826352591.37778025490699-0.945116639504037-0.32276596157338-0.213525210718624-0.248564735862066-0.07374088457828140.02276953728550890.4141278852238350.3816689843404580.499096502126401-0.01218008271939150.00585205480522220.010211111
122013-09-01 02:36:211.15170011149208-0.01193201699875851.17052819068581.13488939474779-0.70780827769076-0.0026906721927648-0.5062299787756620.00253498219159722.14161305183478-0.7527771351233390.594903990087636-1.361959768467692.11141815725630.864435891249312-1.47767576927063-0.9309545142043981.35022083237725-1.00428622519052-0.326022097305229-0.182625478427028-0.243019720693059-0.03424350501302410.01698078947537560.4273322099643370.3979870859977440.497480357883862-0.00902732070651730.00688783050635750.010212121
132013-09-01 02:36:231.15170011149208-0.01193201699875851.17052819068581.13488939474779-0.70780827769076-0.0026906721927648-0.5062299787756620.00253498219159722.14161305183478-0.7527771351233390.594903990087636-1.361959768467692.11141815725630.864435891249312-1.47767576927063-0.9309545142043981.35022083237725-1.00428622519052-0.326022097305229-0.182625478427028-0.243019720693059-0.03424350501302410.01698078947537560.4273322099643370.3979870859977440.497480357883862-0.00902732070651730.00688783050635750.010213132
142013-09-01 02:36:31-0.8600375121526940.4692111837240312.48907161218990.0275373665545480.282903168351467-0.6603682576759060.635615044448659-0.6638869795638522.641991530517810.01195099947778180.285314176226303-2.37456425424571.132383081966240.105562216666883-2.17191432471919-0.7503940161468230.535391628381209-0.481706707907894-0.5056242782418510.113049983185148-0.4118597746836020.0926967025307528-0.3241089837588530.713736944787877-0.3284957885808680.185299862144925-0.111511056341975-0.4413293298338030.010214141
152013-09-01 02:36:321.15170011149208-0.01193201699875851.17052819068581.13488939474779-0.70780827769076-0.0026906721927648-0.5062299787756620.00253498219159722.14161305183478-0.7527771351233390.594903990087636-1.361959768467692.11141815725630.864435891249312-1.47767576927063-0.9309545142043981.35022083237725-1.00428622519052-0.326022097305229-0.182625478427028-0.243019720693059-0.03424350501302410.01698078947537560.4273322099643370.3979870859977440.497480357883862-0.00902732070651730.00688783050635750.010215152
162013-09-01 09:10:161.15157763757596-0.01671986377353711.191347284699671.13625633473046-0.7004358117992670.323663709756618-0.641818637160410.2275972224581380.564591020196726-0.04571396875229450.7173984188014851.381448481493760.604884219379128-0.304351124532671-0.565151628871590.340642663320457-0.6526692077151640.4643182890541770.200694646558147-0.0928805896873416-0.03105914990180370.13280302562462-0.05616502981383050.02967305700559680.452408592815015-0.4073684177983420.07391685953530290.02449421299818120.0109111
172013-09-01 09:46:03-1.75461427105338-0.1947047644565291.06210306797959-2.797917761370290.660730783006917-0.8986561573670350.60467378080792-1.175612902060911.43054565913037-0.4340332118580841.689253433035040.789060739163743-0.708893226205562-0.1007898998122480.553276667797284-0.421604034376388-0.687466909501895-0.0863204177580716-0.174231918937302-0.5090561557686910.5473890914336240.2995760187663780.6000688158313290.0162644666717384-0.0356283161207234-0.2954429738374250.320610766075769-0.3662748306527610.0109211
182013-09-01 09:54:131.54402641302852-0.761242908980963-0.17433402858969-1.39811110656457-0.898719910889933-0.884399729263868-0.51660346246968-0.301736229758136-2.252254830230851.44899397411146-0.189409114291241-0.6694195196664650.690550280560672-0.07308068401582190.487416782621338-1.085471187880450.923324456707021-0.745374368074212-0.480295597518074-0.38200833283334-0.1493206741569890.0567173843627435-0.04483831490983250.07526075432751070.625484743368405-0.05263230810159990.01826758663808920.00142420067640670.0109311
192013-09-01 13:59:001.171620756465060.06926544162827111.315802704838551.32585080522113-0.885902548423458-0.180538340229057-0.5070027722485930.03864817393584940.746356204359898-0.225801589734339-0.6250433378774280.9958214878244040.997946802776625-0.5540582273518890.07113401767142130.0312516215329477-0.303405282321943-0.183133011290416-0.215112492896172-0.0911617468395277-0.05695329652304520.110801755971872-0.00750544287216120.4219916773351130.45105853572827-0.411504744853330.08055774730233980.03878932692768620.01013411
202013-09-01 15:16:281.04000379466034-0.7397229144359031.648515533834180.442334544293136-1.134880963209451.49217849759694-1.383747242231830.687409813435281.59372940530984-0.4871819453019790.8474312374671861.67678091043467-0.203578273713224-0.926371091028804-1.70997667391179-0.8580890973190180.799146026004985-0.999747201078520.191868713071341-0.165448040329761-0.03928881705617370.4381042527552790.0295794174775364-0.1572470173207270.09055785997700921.15118005985370.0357067864014139-0.00088399087960530.01015211

As an aside, we could also create some features that represent different parts of the day, but won’t be useful for our use case since we’re only working with data for two days’ worth of data.

Let’s look at the correlation matrix and see which features influence our prediction.

creditcard.corr()

Our new features aren’t linearly correlated with our response, but some of the components seem to have a large influence on our prediction. We’ll use these when we create our model.

To simplify things, let’s save the dataset into a new table.

vo.drop(
    "creditcard_clean",
    method = "table",
)
creditcard.to_db(
    "creditcard_clean",
    relation_type = "table",
    inplace = True,
)
📅
time
Timestamp(0)
100%
123
v1
Double
100%
123
v2
Double
100%
123
v3
Double
100%
123
v4
Double
100%
123
v5
Double
100%
123
v6
Double
100%
123
v7
Double
100%
123
v8
Double
100%
123
v9
Double
100%
123
v10
Double
100%
123
v11
Double
100%
123
v12
Double
100%
123
v13
Double
100%
123
v14
Double
100%
123
v15
Double
100%
123
v16
Double
100%
123
v17
Double
100%
123
v18
Double
100%
123
v19
Double
100%
123
v20
Double
100%
123
v21
Double
100%
123
v22
Double
100%
123
v23
Double
100%
123
v24
Double
100%
123
v25
Double
100%
123
v26
Double
100%
123
v27
Double
100%
123
v28
Double
100%
123
amount
Double
100%
123
class
Integer
100%
123
hour
Bigint
100%
123
nb_same_transactions_mn_5h
Bigint
100%
123
nb_same_transactions_mn_5m
Bigint
100%
123
nb_same_transactions_mn_5s
Bigint
100%
12013-09-01 00:32:49-0.6939470325398961.318028845732340.815896546390908-0.0901880214976684-0.075838234312895-0.5383541436353210.2134775738179510.536657221951409-0.764248886636331-0.5550850257095511.00766973870140.542556099942768-0.07485965949490370.1081979319760330.2027958462738430.806172170279131-0.1797182989400930.5280009249775350.319768277998190.0034508323493783-0.196070224687173-0.666479337681067-0.0186051553568027-0.0546830086807125-0.1319433874857140.07809142228718720.1159350638219160.02107757729966450.0200111
22013-09-01 06:02:53-1.14969451220909-0.1490491493925353.45172397723128-2.04391487513985-0.4402092348755531.35287450398206-0.5093565981373680.4911191994595433.68890786045674-2.382282283197251.53703949480653-1.061661178379140.7049808620099680.136610477538032-2.81544765802067-1.07004121934990.8590224931493350.018447633923925-1.164538886764-0.3431438168790780.05420882757996921.03688448650413-0.531341726003296-0.2811255658196880.576517389145626-0.620884862256632-0.159750470309145-0.1831000935351970.0206111
32013-09-01 14:40:101.42950150321066-0.990270946322504-0.224429354355113-1.808739677983260.7707875366867333.73098858623111-1.914211610215491.0363846234542-0.3169705711509480.646102741712846-0.100189680769797-0.7510945339116320.437455710928105-0.5041314032412821.491843669989712.02403909933868-0.55493579880519-0.2048743465804640.4771470634539880.2063319818477920.3761364580157090.928722847839222-0.1081743382167851.048921615306410.481213827731516-0.02634159920386550.06290028111219150.02463879670961720.02014111
42013-09-01 16:05:561.35095806068418-0.7017910633102731.51443870664066-0.553158513725929-1.90369380186424-0.718444445490207-1.313377779226230.0043063520743715-0.2341856425132280.580121818921367-0.0033740409599788-0.3737210895712210.604794680217453-0.6608744692873451.259930419950311.593445212139690.0539741871936977-0.779112004471427-0.0199980430229560.08274652030420490.4530466528717991.30932244783782-0.03465370474329890.7618739613928480.283905112974346-0.06224536211794710.06221688508667560.03506394641789920.02016211
52013-09-01 18:14:001.27292822314594-0.8924675939717561.42682976745185-0.738418520419938-1.702089473695320.126512133408805-1.580382888649530.368861387221243-0.3051938370541850.7406200348667711.59791341505794-0.288347323355756-0.693680003873274-0.2016444364404750.9547187741493291.591127859508270.105563476832119-0.542336080988355-0.0336919116192109-0.01125774760352050.5101305136083561.41269834193559-0.01764060443293730.2537873696680010.156714255069728-0.03888077335577240.06667712343225960.01773657944546240.02018311
62013-09-02 10:04:141.99985212662926-1.128660332080380.209439762579962-0.530182942627159-1.227928822744170.694198333719758-1.506686451618020.324264897724773.6051418372434-1.19803850131345-2.520906047551971.043895761498310.311990821681622-1.30525786090393-0.852452409215034-0.705334588453214-0.1697269212047670.6388715876356261.20923599481302-0.2242682515992070.06609544196232330.921168471989934-0.0535830904956647-0.948983955294770.153627838674645-0.752293944323990.16779489935579-0.03046626554084470.02010111
72013-09-02 09:38:192.054988098481530.284267775038234-1.678520130315240.5166213929700520.273809595913958-1.419992937576020.320023704548826-0.4233960569513740.44695275624496-0.506612788262026-0.137780353841110.5859742641428070.936550765673285-0.9116411319380560.8411093142253210.0991781474148040.3989167154639010.294168340288724-0.509854590595338-0.1584638342440330.2104776218901010.844066988298611-0.0223558134928546-0.05123517729917810.271292432932106-0.1011726386111340.0075661457832999-0.03017619127408070.4109111
82013-09-02 20:22:16-0.3386542314106150.759709924109977-0.666459804350598-0.7108570625398311.31498031092885-0.3928405468008060.7268822066704620.0145185178387038-0.0588814821698158-0.4094508261836420.5743036318771350.241003998504569-0.343195886703044-0.852304474216442-0.8325221115652480.658458781704808-0.04344290474049960.4557101766333550.255589010073821-0.0505903209501645-0.307640838742457-0.8226016480648250.2099775693546390.0774270045224069-0.3811629673405470.10444401436013-0.05399542663767790.1069344031322010.64020111
92013-09-01 13:18:47-0.8014722789372881.138290109600862.052182847846920.552310625093470.4830495618043790.6893475070927420.4350075571618540.38386629406628-1.00491982324785-0.1594051409490121.92100335692481.219260720746440.7568406497870610.2939193648361741.02533148746851-0.5333825905870280.0653727460082559-0.6107155600025750.139005988344560.197265760953382-0.149414443863121-0.291131263076908-0.0934242438948921-0.3078005022617070.0271214725620796-0.503499922547880.3660401104283970.1345079520692270.95013111
102013-09-01 16:22:58-1.071839007411841.227563755912851.251878241992790.135979364714769-0.551661634978032-0.3912857961589870.07133067428774660.58450326909491-0.103666727533886-0.190859389124990.7908771321496711.11364204391378-0.1604621836181660.139034400458047-1.40275424389142-0.268175385856950.07528679840419390.06044598127357910.5055289829985210.0854415627277980.05520676867039520.406216065773802-0.1246570216521830.600056077518045-0.1194724730065680.3391281620482510.338412401130050.1879576474381390.95016211
112013-09-01 20:34:541.2516371424818-0.0187289471711530.2971753365964550.575553025217061-0.1391774855546580.198031978860895-0.2306554031154780.07458892090221310.355082947318127-0.05226624953434690.2419345107072291.100614705119690.392397107711587-0.124618554797991-1.085486895914130.0516121718687638-0.4839094523175090.1503551620911680.787033582802688-0.0721998766267873-0.089981422892852-0.0092372054676046-0.203238971898686-0.4249394498029510.6815846412788120.471523838145321-0.0170315318124922-0.00786704086157260.95020211
122013-09-01 23:26:54-2.20378574069451.553474235354660.821292283798549-1.67359018185129-1.3051002757865-0.436330187135068-1.53593501212746-1.6063529686910.345192175733562-1.72170219254449-1.469637193259390.43811972450053-0.8099360988979790.441731531361695-1.190189263417910.996442570608421-0.135552394645336-0.282892311475285-1.536811618514430.407615732883977-1.145389398233770.7288335208172250.130109959220110.455773847640982-0.09023029079179621.197117720016760.06208676769165350.113780307953140.95023211
132013-09-02 13:25:00-0.4407091454956470.9660604327194640.357697650206555-1.052533817155381.35774331316337-1.063258709586811.4000452254861-0.412791510468331-0.0223904387955023-1.65010969745612-0.3191717721891870.4158516847295270.791591932654313-2.30854391959411-1.635757704205970.3045868729540580.650731500990471-0.0636663265485459-1.52587331515157-0.06356325805095790.03773207990000710.455226277370916-0.3392788171395951.098852690021050.06315539044421330.313496707624243-0.0840773480244641-0.03664082172220320.95013111
142013-09-02 13:54:54-1.161576996004571.74431333106717-0.294571431375487-0.5914457216648980.579927011648599-0.8923438117512850.947421415293079-0.00231839745628280.500006575579050.04337078248396051.09436308844110.501501559402544-0.287555561584148-1.94204767823141-1.52228483429470.1173087356662590.8631307498881030.722201019995122-0.08375734660114990.455296943087842-0.06895234106728410.551216258543212-0.292769076102858-0.0019596752524438-0.1014708526728930.5147913805014160.240695025419321-0.07280998103834880.95013211
152013-09-01 10:40:13-1.926115486935680.1847197348036642.192710831608931.09457247655904-0.385502519972489-0.128402466901579-0.6600555138917110.801095418563948-0.104169722462678-0.1976974422961410.669338279863750.391366727460004-0.7184914527040780.2665967122600710.2707326948734610.231781219613771-0.1708659820626751.114088463168990.801274706541643-0.1083974133099350.1705432975163170.7686801475269450.4545396208407510.3117492752655960.551460759425848-0.09825596050979350.218179905628269-0.01560997767386921.23010111
162013-09-01 19:57:27-1.141372388517051.051071153227491.38771417220089-1.48866262507052-0.200034561116065-0.3204153380570580.2557860178210830.471954585320992-0.0677487886236008-0.2983877128163621.349731059683810.65634227071921-0.3602890603143840.2670504693387870.09466021596639280.534181088345345-0.574698976694094-0.272436356876521-0.7832693370703110.0758339635418711-0.04759103374554120.0107687019602749-0.03324169342901450.0393006514817041-0.407865166214520.7110348619849270.0864115922092428-0.0652178342999051.23019111
172013-09-01 20:20:16-0.656676566998721.206945379051641.026225347149230.812685483666262-0.0078038744382095-0.1756455765054170.2277420879745010.542883092118704-1.07972964723133-0.3070295180184781.399116426011850.790202370183494-0.2168310373885520.9318088655772830.526699436607449-0.3537198689308060.09270610258834150.1469956228520670.283569133295125-0.1550298579683020.2907538307725810.7475676697386-0.08498636388152440.237666576601252-0.372742225190831-0.3327875245789750.06373835325787240.0822754322237691.23020211
182013-09-02 13:58:32-8.831845453744045.07310005520069-2.72160102537414-1.49042273888606-1.718842927623751.26802658134452-2.97188541817738-2.170487955531742.500448341796293.016993012209040.1601962585313921.797235481751271.65974762313288-1.61421424483280.04648909879627261.717219866031440.5335142850761251.28374763958823-0.0578756099260872-1.10186847513243.67295371673412-1.59041402058920.823570692490352-0.3053926834919280.9461813834255190.280642532555803-2.645282900440070.601232189929361.23013111
192013-09-02 20:22:21-1.774655447272311.93946582485034-1.45555334601239-1.750001350309130.898426910453413-0.9754644111618410.8745570167129930.671086888829141-0.584006119888973-0.3364099755164360.08728845721552520.301549153460162-0.9606589746323491.43420273232627-0.9115398837252760.0859570321855169-0.5759519432810650.325187842019577-0.170551488512453-0.06834935176746020.2109815448329650.454554995722769-0.4415615898403620.3742787405815940.7983207763127190.1406981240912420.08797356165929540.1222063900452171.23020111
202013-09-01 20:56:04-1.04278552548660.7776978816133381.628501985132090.2519327761451571.041353885760571.185295184347790.4313276990971170.48552528945836-0.551620362615748-0.5911290448468830.259450280872439-0.277735660492825-0.9699838103890140.5120202852556072.01055460338215-1.312276287536160.861445541068075-1.67682780889288-1.72337218250831-0.2458362590837210.3165412167872590.985919521144664-0.245939718252857-0.9764024089346010.341213287043698-0.0639083724718469-0.01320832783890240.05904778802459441.24020111

Data Modeling

Train/Test sets

Since we’re dealing with time series data, we have to maintain time linearity. Our goal is to use the past to predict the future, so a k-fold cross-validation, for example, wouldn’t make much sense here.

We will split the dataset into a train (day 1) and a test (day 2).

train = creditcard.search("Time  < TIMESTAMP '2013-09-02 00:00:00'")
test  = creditcard.search("Time >= TIMESTAMP '2013-09-02 00:00:00'")

Supervision

Supervising would make this pretty easy since it would just be a binary classification problem. We can use different algorithms to optimize the prediction. Our dataset is unbalanced, so the AUC might be a good metric to evaluate the model. The PRC AUC would also be a relevant metric.

LogisticRegression works well with monotonic relationships. Since we have a lot of independent features that correlate with the response, it should be a good first model to use.

from vastorbit.machine_learning.vast import LogisticRegression

predictors = creditcard.get_columns(exclude_columns = ["Class", "Time"])
response = "Class"
model = LogisticRegression(
    max_iter = 3000,
)
model.fit(train, predictors, response, test)
model.classification_report()
value
auc0.9697148890112128
prc_auc0.15988482837422502
accuracy0.9956482696691303
log_loss0.020447207064671364
precision0.9065934065934066
recall0.7819905213270142
f1_score0.8396946564885496
mcc0.8398539006289721
informedness0.7807988768576464
markedness0.9033754982338404
csi0.7236842105263158

Based on the report, our model is very good at detecting non-fraudulent events; the AUC is high and the PRC AUC is very good. We can use this model to filter obvious events and to get some insight on the importance of each feature.

model.features_importance()

Some PCA components seem to be very relevant and will be essential for finding anomalies.

Unsupervised Learning

There are many unsupervised learning techniques, but not all of them will be useful for detecting anomalies. Since there’s no rigid mathematical definition for what an outlier is, finding anomalies becomes somewhat subjective. To solve this problem, we have to evaluate our constraints and needs. Do we need to find anomalies in real-time? Do we have a time constraint?

  • Real-time: We don’t have access to historical data, so we need an easy way to preprocess the data that is wholly independent from historical data, and the model must be simple to deploy at the source of the data stream. For example, we might use simple preprocessing techniques like normalization, standardization or One-Hot Encoding instead of more complex ones like windows, interpolation, or intersection. Isolation forests, KMeans, robust PCA, or global outlier detection using z-score would be ideal, whereas local outlier factor, DBSCAN, or other hard-to-deploy methods cannot be used.

  • Near Real-time: We have access to historical data and our preprocessing method must be fast. The model has to be simple to score with. We can use any preprocessing technique as long as it is fast enough, which of course varies. Since this is still a real-time use case, we should still avoid any hard-to-deploy algorithms like DBSCAN or local outlier factor.

  • No time constraint: We can use any techniques we want.

Due to the complexity of the computations, anomalies are difficult to detect in the context of “Big Data”. We have three efficient methods for that case:

  • Machine Learning: We need to use easily-deployable algorithms to perform real-time fraud detection. Isolation forests and KMeans can be easily deployed and they work well for detecting anomalies.

  • Rules & Thresholds: The z-score can be an efficient solution for detecting global outliers.

  • Decomposition: Robust PCA is another technique for detecting outliers.

Before using these techniques, let’s draw some scatter plots to get a better idea of what kind of anomalies we can expect.

creditcard.scatter(
    ["V12", "V17"],
    by = "Class",
    max_nb_points = 5000000,
)
creditcard.scatter(
    ["V12", "V17", "V10"],
    by = "Class",
)

In this case, the anomalies seem pretty clear global outliers of the distributions. When doing unsupervised learning, we don’t have this information in advance.

For the rest of this example, we’ll investigate labels and how they can help us understand the efficacy of each technique.

k-means Clustering

We begin by examining KMeans clustering, which partitions the data into k clusters.

We can use an elbow curve to find a suitable number of clusters. We can then add more clusters then the amount suggested by the elbow() curve to create clusters mainly composed of anomalies. Clusters with relatively fewer elements can then be investigated by an expert to label the anomalies.

From there, we perform the following procedure:

  • Label historical data by looking at unsupervised learning results.

  • Use supervised learning models to learn on the labeled anomalies. This model will be brought to the source of the data stream.

Once we deploy the unsupervised model and can reliably detect suspicious transactions, we could block them and contact the cardholder about potential fraudulent activity on their card.

from vastorbit.machine_learning.model_selection import elbow

elbow(
    creditcard,
    ["V12", "V17", "V10", "V14", "V16"],
    n_clusters = [1, 2, 10, 20, 30],
)

10 seems to be a suitable number of clusters, so let’s try out 20 clusters and see if the collective outliers cluster together. We can then then evaluate each cluster independently and see which clusters have the most anomalies.

from vastorbit.machine_learning.vast import KMeans

model = KMeans(n_clusters = 13)
model.fit(creditcard, ["V12", "V17", "V10"])

Let’s direct our attention to the smallest clusters.

model.predict(creditcard, name = "cluster")
creditcard.groupby(
    ["cluster"],
    [
        "COUNT(*) AS total",
        "100 * AVG(Class) AS percent_fraud",
        "SUM(Class) / 492 AS total_fraud",
    ],
).sort("total")
123
cluster
Integer
100%
123
total
Bigint
100%
123
percent_fraud
Double
100%
123
total_fraud
Bigint
100%
138996.629213483146070
219296.739130434782610
361050.00
4820097.50
5103580.83798882681564250
61212713.3044846577498030
7519941.45436308926780340
8026170.229270156667940370
9430600.294117647058823540
101134140.20503807850029290
11939910.27562014532698570
12754430.220466654418519180
13263800.0470219435736677140

Notice that clusters with fewer elemenets tend to contain much more fraudulent events than the others. This methodology makes KMeans a good algorithm for catching collective outliers. Combining KMeans with other techniques like Z-score, we can find most of the outliers of the distribution.

Outliers of the distribution

Let’s use the Z-score to detect global outliers of the distribution.

creditcard.outliers(
    ["V12", "V17", "V10"],
    name = "global_outliers",
    threshold = 5.0,
)
creditcard.groupby(
    ["global_outliers"],
    [
        "COUNT(*) AS total",
        "100 * AVG(Class) AS percent_fraud",
        "SUM(Class) AS total_fraud",
    ],
).sort("total")
123
global_outliers
Integer
100%
123
total
Bigint
100%
123
percent_fraud
Double
100%
123
total_fraud
Bigint
100%
1131571.74603174603175226
20286990.9268615631206663266
creditcard.outliers_plot(
    ["V12", "V17",],
    threshold = 5.0,
)

We can see that we can caught more than 71% of the fraudulent activity in less than 1% of the dataset.

Neighbors

Other algorithms could be used to solve the problem with more precision if we could use a more powerful clustering method and didn’t have a time constraint. Based on neighbors, these algorithms are very computationally expensive. An example of this kind of algorithm is the local outlier factor.

from vastorbit.machine_learning.vast import LocalOutlierFactor

model = LocalOutlierFactor()
model.fit(creditcard.sample(x = 0.01), ["V12", "V17", "V10"])
lof_creditcard = model.predict()
lof_creditcard["outliers"] = "(CASE WHEN lof_score > 2 THEN 1 ELSE 0 END)"
lof_creditcard.scatter(["V12", "V17", "V10"], by = "outliers")

We can catch outliers with a neighbors score. Again, the main problem with these sorts of algorithms is that what they have in precision, they lack in speed, which makes them unsuitable for scoring new data. This is why it’s important to focus on scalable techniques like KMeans.

Other Techniques

Other scalable techniques that can solve this problem are robust PCA and isolation forest.

Conclusion

We’ve solved our problem in a pandas-like way, all without ever loading data into memory!