VerticaPy

Python API for Vertica Data Science at Scale

Correlation and Dependency

Finding links between variables is a very important task. The main purpose of data science is to find relationships between variables, and to understand how these relationships can help us make better decisions.

Machine learning models are also sensitive to the number of variables and how they relate and affect each other, so finding correlations and dependencies can help us make better use of our machine learning algorithms.

Let's use the Telco Churn dataset to understand how we can find links between different variables in VerticaPy.

In [1]:
import verticapy as vp
vdf = vp.read_csv("data/churn.csv")
display(vdf)
Abc
customerID
Varchar(20)
Abc
gender
Varchar(20)
123
SeniorCitizen
Int
010
Partner
Boolean
010
Dependents
Boolean
123
tenure
Int
010
PhoneService
Boolean
Abc
MultipleLines
Varchar(100)
Abc
InternetService
Varchar(22)
Abc
OnlineSecurity
Varchar(38)
Abc
OnlineBackup
Varchar(38)
Abc
DeviceProtection
Varchar(38)
Abc
TechSupport
Varchar(38)
Abc
StreamingTV
Varchar(38)
Abc
StreamingMovies
Varchar(38)
Abc
Contract
Varchar(28)
010
PaperlessBilling
Boolean
Abc
PaymentMethod
Varchar(50)
123
MonthlyCharges
Numeric(8,3)
123
TotalCharges
Numeric(9,3)
010
Churn
Boolean
10002-ORFBOFemale0
✅
✅
9
✅
NoDSLNoYesNoYesYesNoOne year
✅
Mailed check65.6593.3
❌
20003-MKNFEMale0
❌
❌
9
✅
YesDSLNoNoNoNoNoYesMonth-to-month
❌
Mailed check59.9542.4
❌
30004-TLHLJMale0
❌
❌
4
✅
NoFiber opticNoNoYesNoNoNoMonth-to-month
✅
Electronic check73.9280.85
✅
40011-IGKFFMale1
✅
❌
13
✅
NoFiber opticNoYesYesNoYesYesMonth-to-month
✅
Electronic check98.01237.85
✅
50013-EXCHZFemale1
✅
❌
3
✅
NoFiber opticNoNoNoYesYesNoMonth-to-month
✅
Mailed check83.9267.4
✅
60013-MHZWFFemale0
❌
✅
9
✅
NoDSLNoNoNoYesYesYesMonth-to-month
✅
Credit card (automatic)69.4571.45
❌
70013-SMEOEFemale1
✅
❌
71
✅
NoFiber opticYesYesYesYesYesYesTwo year
✅
Bank transfer (automatic)109.77904.25
❌
80014-BMAQUMale0
✅
❌
63
✅
YesFiber opticYesNoNoYesNoNoTwo year
✅
Credit card (automatic)84.655377.8
❌
90015-UOCOJFemale1
❌
❌
7
✅
NoDSLYesNoNoNoNoNoMonth-to-month
✅
Electronic check48.2340.35
❌
100016-QLJISFemale0
✅
✅
65
✅
YesDSLYesYesYesYesYesYesTwo year
✅
Mailed check90.455957.9
❌
110017-DINOCMale0
❌
❌
54
❌
No phone serviceDSLYesNoNoYesYesNoTwo year
❌
Credit card (automatic)45.22460.55
❌
120017-IUDMWFemale0
✅
✅
72
✅
YesFiber opticYesYesYesYesYesYesTwo year
✅
Credit card (automatic)116.88456.75
❌
130018-NYROUFemale0
✅
❌
5
✅
NoFiber opticNoNoNoNoNoNoMonth-to-month
✅
Electronic check68.95351.5
❌
140019-EFAEPFemale0
❌
❌
72
✅
YesFiber opticYesYesYesNoYesNoTwo year
✅
Bank transfer (automatic)101.37261.25
❌
150019-GFNTWFemale0
❌
❌
56
❌
No phone serviceDSLYesYesYesYesNoNoTwo year
❌
Bank transfer (automatic)45.052560.1
❌
160020-INWCKFemale0
✅
✅
71
✅
YesFiber opticNoYesYesNoNoYesTwo year
✅
Credit card (automatic)95.756849.4
❌
170020-JDNXPFemale0
✅
✅
34
❌
No phone serviceDSLYesNoYesYesYesYesOne year
❌
Mailed check61.251993.2
❌
180021-IKXGCFemale1
❌
❌
1
✅
YesFiber opticNoNoNoNoNoNoMonth-to-month
✅
Electronic check72.172.1
❌
190022-TCJCIMale1
❌
❌
45
✅
NoDSLYesNoYesNoNoYesOne year
❌
Credit card (automatic)62.72791.5
✅
200023-HGHWLMale1
❌
❌
1
❌
No phone serviceDSLNoNoNoNoNoNoMonth-to-month
✅
Electronic check25.125.1
✅
210023-UYUPNFemale1
✅
❌
50
✅
YesNoNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceOne year
❌
Electronic check25.21306.3
❌
220023-XUOPTFemale0
✅
❌
13
✅
YesFiber opticNoYesYesNoYesNoMonth-to-month
❌
Electronic check94.11215.6
✅
230027-KWYKWFemale0
✅
✅
23
✅
YesFiber opticNoNoNoNoYesNoMonth-to-month
✅
Electronic check83.751849.95
❌
240030-FNXPPFemale0
❌
❌
3
✅
NoNoNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceMonth-to-month
❌
Mailed check19.8557.2
❌
250031-PVLZIFemale0
✅
✅
4
✅
NoNoNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceMonth-to-month
❌
Mailed check20.3576.35
✅
260032-PGELSFemale0
✅
✅
1
❌
No phone serviceDSLYesNoNoNoNoNoMonth-to-month
❌
Bank transfer (automatic)30.530.5
✅
270036-IHMOTFemale0
✅
✅
55
✅
NoFiber opticNoYesYesYesYesYesOne year
✅
Bank transfer (automatic)103.75656.75
❌
280040-HALCWMale0
✅
✅
54
✅
NoNoNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceTwo year
❌
Credit card (automatic)20.41090.6
❌
290042-JVWOJMale0
❌
❌
26
✅
NoNoNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceOne year
✅
Bank transfer (automatic)19.6471.85
❌
300042-RLHYPFemale0
✅
✅
69
✅
NoNoNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceTwo year
❌
Bank transfer (automatic)19.71396.9
❌
310048-LUMLSMale0
✅
✅
37
✅
NoFiber opticNoNoNoNoYesYesOne year
❌
Credit card (automatic)91.23247.55
❌
320048-PIHNLFemale0
✅
❌
49
✅
NoNoNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceOne year
❌
Bank transfer (automatic)20.45900.9
❌
330052-DCKONMale0
✅
❌
66
✅
YesFiber opticYesYesYesYesYesYesOne year
❌
Bank transfer (automatic)115.87942.15
❌
340052-YNYOTFemale0
❌
❌
67
✅
NoNoNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceOne year
❌
Electronic check20.551343.4
❌
350056-EPFBGMale0
✅
✅
20
❌
No phone serviceDSLYesNoYesYesNoNoTwo year
✅
Credit card (automatic)39.4825.4
❌
360057-QBUQHFemale0
❌
✅
43
✅
YesNoNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceTwo year
✅
Electronic check25.11070.15
❌
370058-EVZWMFemale0
✅
❌
55
✅
YesFiber opticYesNoNoNoYesNoMonth-to-month
✅
Bank transfer (automatic)89.84959.6
❌
380060-FUALYFemale0
✅
❌
59
✅
YesFiber opticYesYesNoNoYesNoMonth-to-month
✅
Electronic check94.755597.65
❌
390064-SUDOGFemale0
✅
✅
12
✅
NoNoNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceTwo year
❌
Bank transfer (automatic)20.3224.5
❌
400064-YIJGFMale0
✅
✅
27
✅
YesFiber opticNoNoNoNoNoNoMonth-to-month
✅
Bank transfer (automatic)75.751929.0
❌
410067-DKWBLMale1
❌
❌
2
✅
NoDSLYesNoNoNoNoNoMonth-to-month
✅
Electronic check49.2591.1
✅
420068-FIGTFFemale0
❌
❌
27
✅
NoDSLNoYesYesYesYesYesOne year
❌
Mailed check78.22078.95
❌
430071-NDAFPMale0
✅
✅
25
✅
YesNoNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceTwo year
❌
Credit card (automatic)25.5630.6
❌
440074-HDKDGMale0
✅
✅
25
✅
NoDSLYesYesYesNoNoNoOne year
✅
Bank transfer (automatic)61.61611.0
❌
450076-LVEPSMale0
❌
✅
29
❌
No phone serviceDSLYesYesYesYesNoNoMonth-to-month
✅
Mailed check45.01242.45
❌
460078-XZMHTMale0
✅
❌
72
✅
YesDSLNoYesYesYesYesYesTwo year
✅
Bank transfer (automatic)85.156316.2
❌
470080-EMYVYFemale0
❌
❌
14
✅
NoDSLNoYesNoNoNoNoOne year
❌
Credit card (automatic)51.45727.85
❌
480080-OROZOFemale0
❌
❌
35
✅
NoFiber opticNoNoYesYesYesYesOne year
✅
Electronic check99.253532.0
❌
490082-LDZUEMale0
❌
❌
1
✅
NoDSLNoNoNoNoNoNoMonth-to-month
✅
Mailed check44.344.3
❌
500082-OQIQYMale0
❌
❌
29
✅
NoFiber opticNoNoNoYesYesYesMonth-to-month
✅
Electronic check94.22607.6
❌
510083-PIVIKMale0
❌
❌
64
✅
YesDSLYesYesYesYesYesNoOne year
❌
Electronic check81.255567.55
❌
520089-IIQKOFemale0
✅
✅
39
✅
YesFiber opticNoNoNoYesYesYesMonth-to-month
✅
Credit card (automatic)99.953767.4
❌
530093-EXYQLFemale1
❌
❌
40
✅
NoFiber opticNoNoNoNoYesYesMonth-to-month
✅
Electronic check91.553673.6
❌
540093-XWZFYMale0
❌
❌
40
✅
YesFiber opticNoYesYesNoYesYesMonth-to-month
✅
Credit card (automatic)104.54036.85
✅
550094-OIFMOFemale1
❌
❌
11
✅
NoFiber opticNoYesNoNoYesYesMonth-to-month
✅
Electronic check95.01120.3
✅
560096-BXERSFemale0
✅
❌
6
✅
YesDSLNoNoNoNoNoNoMonth-to-month
❌
Electronic check50.35314.55
❌
570096-FCPUFMale0
❌
❌
30
✅
YesDSLYesNoNoNoNoYesMonth-to-month
✅
Mailed check64.51888.45
❌
580098-BOWSOMale0
❌
❌
27
✅
NoNoNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceMonth-to-month
✅
Electronic check19.4529.8
❌
590100-DUVFCMale1
✅
❌
70
✅
YesFiber opticNoYesYesNoYesYesOne year
✅
Electronic check104.87308.95
❌
600103-CSITQFemale0
✅
❌
57
✅
YesFiber opticNoYesYesYesYesYesOne year
✅
Bank transfer (automatic)109.46252.7
❌
610104-PPXDVMale0
✅
❌
58
✅
NoDSLNoNoYesNoNoNoOne year
❌
Credit card (automatic)50.32878.55
❌
620106-GHRQRMale0
❌
❌
16
✅
YesDSLNoYesYesNoNoYesMonth-to-month
✅
Bank transfer (automatic)71.41212.1
❌
630106-UGRDOFemale0
✅
❌
69
✅
YesFiber opticYesYesYesYesYesYesTwo year
✅
Electronic check116.08182.85
❌
640107-WESLMMale0
❌
❌
1
✅
NoNoNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceMonth-to-month
✅
Electronic check19.8519.85
✅
650107-YHINAMale0
❌
✅
1
✅
YesFiber opticNoNoYesNoYesYesMonth-to-month
✅
Electronic check99.7599.75
✅
660111-KLBQGMale1
✅
✅
32
✅
NoFiber opticNoYesNoNoYesYesMonth-to-month
✅
Mailed check93.952861.45
❌
670112-QAWRZMale0
✅
✅
16
✅
YesFiber opticNoNoYesNoYesNoMonth-to-month
✅
Bank transfer (automatic)90.81442.2
❌
680112-QWPNCMale0
✅
❌
49
✅
YesDSLYesNoYesYesYesYesOne year
❌
Electronic check84.354059.35
✅
690114-IGABWFemale0
✅
❌
71
❌
No phone serviceDSLNoYesYesYesYesYesTwo year
❌
Bank transfer (automatic)58.254145.9
❌
700114-PEGZZFemale0
❌
❌
33
✅
YesFiber opticNoYesYesNoYesYesMonth-to-month
❌
Electronic check107.553645.5
❌
710114-RSRRWFemale0
✅
❌
10
✅
NoNoNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceMonth-to-month
✅
Bank transfer (automatic)19.95187.75
❌
720115-TFERTMale0
✅
❌
21
✅
YesFiber opticNoYesYesYesYesYesMonth-to-month
✅
Electronic check111.22317.1
✅
730117-LFRMWMale0
✅
✅
37
❌
No phone serviceDSLYesYesYesNoNoNoMonth-to-month
❌
Bank transfer (automatic)40.21448.8
✅
740118-JPNOYFemale1
❌
❌
26
✅
NoFiber opticYesNoNoNoYesNoMonth-to-month
✅
Credit card (automatic)85.82193.65
❌
750121-SNYRKMale0
❌
❌
50
❌
No phone serviceDSLYesNoNoYesNoNoOne year
✅
Mailed check35.41748.9
❌
760122-OAHPZFemale0
❌
❌
7
✅
YesFiber opticNoNoNoNoNoNoMonth-to-month
✅
Electronic check73.85511.25
✅
770123-CRBRTFemale0
✅
✅
61
✅
YesDSLYesYesYesYesYesYesTwo year
❌
Mailed check88.15526.75
❌
780125-LZQXKMale0
❌
❌
15
✅
NoFiber opticNoNoYesYesYesYesMonth-to-month
✅
Electronic check101.351553.95
✅
790128-MKWSGFemale0
❌
✅
26
❌
No phone serviceDSLYesNoNoYesNoYesMonth-to-month
❌
Mailed check45.81147.0
❌
800129-KPTWJMale0
✅
❌
72
✅
NoFiber opticNoNoYesNoYesYesMonth-to-month
✅
Electronic check94.656747.35
❌
810129-QMPDRMale0
✅
✅
44
✅
NoNoNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceOne year
✅
Bank transfer (automatic)20.5865.05
❌
820130-SXOUNMale0
❌
❌
66
✅
YesFiber opticNoYesNoNoNoYesMonth-to-month
❌
Credit card (automatic)89.45976.9
❌
830133-BMFZOFemale0
❌
❌
2
✅
NoFiber opticYesYesNoYesNoNoMonth-to-month
✅
Electronic check86.25181.65
✅
840134-XWXCEFemale1
❌
❌
44
✅
NoDSLNoNoYesYesYesYesOne year
❌
Bank transfer (automatic)74.853268.05
❌
850135-NMXAPFemale0
❌
❌
12
✅
YesFiber opticNoYesNoNoYesNoMonth-to-month
✅
Bank transfer (automatic)89.751052.4
✅
860136-IFMYDMale1
✅
❌
69
✅
YesFiber opticNoYesYesYesYesYesTwo year
✅
Electronic check109.957634.25
❌
870137-OCGABFemale0
❌
❌
1
✅
YesFiber opticNoYesNoNoNoNoMonth-to-month
✅
Mailed check80.280.2
✅
880137-UDEUOFemale0
❌
❌
3
✅
NoNoNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceMonth-to-month
❌
Mailed check19.8563.75
✅
890139-IVFJGFemale0
✅
❌
2
✅
NoFiber opticYesYesNoNoYesNoMonth-to-month
❌
Electronic check90.35190.5
❌
900141-YEAYSFemale1
❌
❌
27
✅
YesFiber opticNoYesYesNoNoNoMonth-to-month
✅
Bank transfer (automatic)86.452401.05
❌
910142-GVYSNMale0
❌
❌
26
✅
NoNoNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceMonth-to-month
✅
Electronic check20.3511.25
❌
920147-ESWWRFemale1
✅
❌
39
✅
YesFiber opticNoNoNoYesYesYesMonth-to-month
✅
Electronic check101.253949.15
❌
930148-DCDOSMale0
❌
❌
25
✅
YesFiber opticNoNoNoNoYesYesMonth-to-month
✅
Bank transfer (automatic)94.72362.1
✅
940151-ONTOVFemale0
❌
❌
1
✅
NoFiber opticNoNoNoNoNoNoMonth-to-month
❌
Mailed check70.970.9
✅
950156-FVPTAMale0
✅
❌
22
✅
NoDSLYesNoNoYesNoNoMonth-to-month
✅
Electronic check54.21152.7
✅
960164-APGRBFemale0
❌
❌
72
✅
YesFiber opticYesYesYesYesYesYesTwo year
✅
Electronic check114.98496.7
❌
970164-XAIRPFemale0
❌
❌
24
✅
NoNoNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceTwo year
✅
Bank transfer (automatic)19.55470.2
❌
980168-XZKBBFemale0
✅
❌
19
✅
YesFiber opticNoNoNoNoYesNoMonth-to-month
✅
Electronic check86.851564.4
❌
990174-QRVVYMale0
✅
✅
71
✅
YesNoNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceNo internet serviceTwo year
❌
Credit card (automatic)25.351847.55
❌
1000177-PXBATMale1
✅
❌
33
✅
YesFiber opticYesNoYesYesYesYesMonth-to-month
✅
Bank transfer (automatic)109.93694.7
❌
Rows: 1-100 | Columns: 21

The Pearson correlation coefficient is a very common correlation function. In this case, it helped us to find linear links between the variables. Having a strong Pearson relationship means that the two input variables are linearly correlated.

In [2]:
vdf.corr(method = "pearson")
Out[2]:
"SeniorCitizen"
"Partner"
"Dependents"
"tenure"
"PhoneService"
"PaperlessBilling"
"MonthlyCharges"
"TotalCharges"
"Churn"
"SeniorCitizen"1.00.0164786575974139-0.2111850884939580.01656687768180930.008576401079279440.1565295593111730.2201733385762740.1024106053953320.150889328176473
"Partner"0.01647865759741391.00.4526762829294640.3796974611682930.017705663223972-0.0148766222878910.0968479362564710.319072363238569-0.150447544959177
"Dependents"-0.2111850884939580.4526762829294641.00.159712331216918-0.00176167854468371-0.111377229193644-0.1138902300219370.0646532494217718-0.164221401579725
"tenure"0.01656687768180930.3796974611682930.1597123312169181.00.008448206732743750.006152481598271120.247899856286150.825880460933193-0.352228670113078
"PhoneService"0.008576401079279440.017705663223972-0.001761678544683710.008448206732743751.00.01650480573256970.247397963214140.1130082609547410.0119419800290031
"PaperlessBilling"0.156529559311173-0.014876622287891-0.1113772291936440.006152481598271120.01650480573256971.00.3521499677959870.1578297828659070.191825331666468
"MonthlyCharges"0.2201733385762740.096847936256471-0.1138902300219370.247899856286150.247397963214140.3521499677959871.00.6510648032262150.193356422237847
"TotalCharges"0.1024106053953320.3190723632385690.06465324942177180.8258804609331930.1130082609547410.1578297828659070.6510648032262151.0-0.199484083567563
"Churn"0.150889328176473-0.150447544959177-0.164221401579725-0.3522286701130780.01194198002900310.1918253316664680.193356422237847-0.1994840835675631.0
Rows: 1-9 | Columns: 10

We can see that 'tenure' is well-correlated to the 'TotalCharges', which makes sense.

In [3]:
vdf.scatter(["tenure", "TotalCharges"])
vdf.corr(["tenure", "TotalCharges"], method = "pearson")
Out[3]:
0.825880460933193

Note, however, that having a low Pearson relationship imply that the variables aren't correlated. For example, let's compute the Pearson correlation coefficient between 'tenure' and 'TotalCharges' to the power of 20.

In [4]:
vdf["TotalCharges^20"] = vdf["TotalCharges"] ** 20
vdf.scatter(["tenure", "TotalCharges^20"])
vdf.corr(["tenure", "TotalCharges^20"], method = "pearson")
Out[4]:
0.224994408804537

We know that the 'tenure' and 'TotalCharges' are strongly linearly correlated. However we can notice that the correlation between the 'tenure' and 'TotalCharges' to the power of 20 is not very high. Indeed, the Pearson correlation coefficient is not robust for monotonic relationships, but rank-based correlations are. Knowing this, we'll calculate the Spearman's rank correlation coefficient instead.

In [5]:
vdf.corr(method = "spearman")
Out[5]:
"SeniorCitizen"
"Partner"
"Dependents"
"tenure"
"PhoneService"
"PaperlessBilling"
"MonthlyCharges"
"TotalCharges"
"Churn"
"TotalCharges^20"
"SeniorCitizen"1.00.0164786575974139-0.2111850884939580.01907678987011520.008576401079279440.1565295593111730.2210925291021620.1057953423037250.1508893281764730.105795342303725
"Partner"0.01647865759741391.00.4526762829294640.3846657102841190.017705663223972-0.0148766222878910.1084109458959810.343930553215626-0.1504475449591770.343930553215626
"Dependents"-0.2111850884939580.4526762829294641.00.164485741353804-0.00176167854468371-0.111377229193644-0.1070827255867110.0866797760484616-0.1642214015797250.0866797760484616
"tenure"0.01907678987011520.3846657102841190.1644857413538041.00.008150819869071840.007928762394763210.2763422452237080.883103368818293-0.3696207787634350.883103368818293
"PhoneService"0.008576401079279440.017705663223972-0.001761678544683710.008150819869071841.00.01650480573256970.2388264102300160.08380485478560370.01194198002900310.0838048547856037
"PaperlessBilling"0.156529559311173-0.014876622287891-0.1113772291936440.007928762394763210.01650480573256971.00.3461588793813230.1516697127990970.1918253316664680.151669712799097
"MonthlyCharges"0.2210925291021620.108410945895981-0.1070827255867110.2763422452237080.2388264102300160.3461588793813231.00.6339584053012060.1848392857837580.633958405301206
"TotalCharges"0.1057953423037250.3439305532156260.08667977604846160.8831033688182930.08380485478560370.1516697127990970.6339584053012061.0-0.2332110185851041.0
"Churn"0.150889328176473-0.150447544959177-0.164221401579725-0.3696207787634350.01194198002900310.1918253316664680.184839285783758-0.2332110185851041.0-0.233211018585104
"TotalCharges^20"0.1057953423037250.3439305532156260.08667977604846160.8831033688182930.08380485478560370.1516697127990970.6339584053012061.0-0.2332110185851041.0
Rows: 1-10 | Columns: 11

The Spearman's rank correlation coefficient determines the monotonic relationships between the variables.

In [6]:
vdf.corr(["tenure", "TotalCharges^20"], method = "spearman")
Out[6]:
0.883103368818293

We can notice that Spearman's rank correlation coefficient stays the same if one of the variables can be expressed using a monotonic function on the other. The same applies to Kendall rank correlation coefficient.

In [7]:
vdf.corr(method = "kendall")

Out[7]:
"SeniorCitizen"
"Partner"
"Dependents"
"tenure"
"PhoneService"
"PaperlessBilling"
"MonthlyCharges"
"TotalCharges"
"Churn"
"TotalCharges^20"
"SeniorCitizen"10.0164786575974139-0.2111850884939580.01531540635432760.008576401079279440.1565295593111730.1806359492160480.08735711660856450.1508893281764730.0873571166085645
"Partner"0.016478657597413910.4526762829294640.3177135036469560.017705663223972-0.0148766222878910.08862603533694540.279358144937778-0.1504475449591770.279358144937778
"Dependents"-0.2111850884939580.45267628292946410.135506499930892-0.00176167854468371-0.111377229193644-0.08743943756427740.0674036111339054-0.1642214015797250.0674036111339054
"tenure"0.01531540635432760.3177135036469560.13550649993089210.007001803125611430.00635145682196070.1905648681169640.731699318287362-0.3029650182111260.731699318287362
"PhoneService"0.008576401079279440.017705663223972-0.001761678544683710.0070018031256114310.01650480573256970.1958625381059680.06907613441435850.01194198002900310.0690761344143585
"PaperlessBilling"0.156529559311173-0.014876622287891-0.1113772291936440.00635145682196070.016504805732569710.282715626971380.125281001839270.1918253316664680.12528100183927
"MonthlyCharges"0.1806359492160480.0886260353369454-0.08743943756427740.1905648681169640.1958625381059680.2827156269713810.4584691989252340.1509552851582450.458469198925234
"TotalCharges"0.08735711660856450.2793581449377780.06740361113390540.7316993182873620.06907613441435850.125281001839270.4584691989252341-0.1891016992393130.996878225678041
"Churn"0.150889328176473-0.150447544959177-0.164221401579725-0.3029650182111260.01194198002900310.1918253316664680.150955285158245-0.1891016992393131-0.189101699239313
"TotalCharges^20"0.08735711660856450.2793581449377780.06740361113390540.7316993182873620.06907613441435850.125281001839270.4584691989252340.996878225678041-0.1891016992393131
Rows: 1-10 | Columns: 11

Notice that the Kendall rank correlation coefficient will also detect the monotonic relationship.

In [8]:
vdf.corr(["tenure", "TotalCharges^20"], method = "kendall")
Out[8]:
0.731699318287362

However, the Kendall rank correlation coefficient is very computationally expensive, so we'll generally use Pearson and Spearman when dealing with correlations between numerical variables.

Binary features are considered numerical, but this isn't technically accurate. Since binary variables can only take two values, calculating correlations between a binary and numerical variable can lead to misleading results. To account for this, we'll want to use the 'Biserial Point' method to calculate the Point-Biserial correlation coefficient. This powerful method will help us understand the link between a binary variable and a numerical variable.

In [9]:
vdf.corr(method = "biserial")

Out[9]:
"SeniorCitizen"
"Partner"
"Dependents"
"tenure"
"PhoneService"
"PaperlessBilling"
"MonthlyCharges"
"TotalCharges"
"Churn"
"TotalCharges^20"
"SeniorCitizen"10.0164774876952011-0.2111700954096680.01656570151641210.00857579219772810.1565184464959010.2201577073712140.1024033233812150.1508786157893820.00959483426594477
"Partner"0.016477487695201110.4526441451790470.3796705045489580.017704406210501-0.0148755661221670.096841060537730.319049675261633-0.1504368639364830.0945722425175577
"Dependents"-0.2111700954096680.45264414517904710.159700992440771-0.0017615534743243-0.111369321967821-0.1138821443856310.0646486521842783-0.1642097426821510.0224428704447468
"tenure"0.01656570151641210.3796705045489580.1597009924407711nannannannannannan
"PhoneService"0.00857579219772810.017704406210501-0.0017615534743243nan10.01650363397397010.2473803991969890.1130002253831440.01194113220819130.0467146740346801
"PaperlessBilling"0.156518446495901-0.014875566122167-0.111369321967821nan0.016503633973970110.3521249669108040.1578185602127220.1918117130280160.0579931118233683
"MonthlyCharges"0.2201577073712140.09684106053773-0.113882144385631nan0.2473803991969890.3521249669108041nan0.193342694899628nan
"TotalCharges"0.1024033233812150.3190496752616330.0646486521842783nan0.1130002253831440.157818560212722nan1-0.199469899041392nan
"Churn"0.150878615789382-0.150436863936483-0.164209742682151nan0.01194113220819130.1918117130280160.193342694899628-0.1994698990413921-0.0630928693846116
"TotalCharges^20"0.009594834265944770.09457224251755770.0224428704447468nan0.04671467403468010.0579931118233683nannan-0.06309286938461161
Rows: 1-10 | Columns: 11

Lastly, we'll look at the relationship between categorical columns. In this case, the 'Cramer's V' method is very efficient. Since there is no position in the Euclidean space for those variables, the 'Cramer's V' coefficients cannot be negative (which is a sign of an opposite relationship) and they will range in the interval [0,1].

In [10]:
vdf.corr(method = "cramer")

Out[10]:
"customerID"
"gender"
"SeniorCitizen"
"Partner"
"Dependents"
"PhoneService"
"MultipleLines"
"InternetService"
"OnlineSecurity"
"OnlineBackup"
"DeviceProtection"
"TechSupport"
"StreamingTV"
"StreamingMovies"
"Contract"
"PaperlessBilling"
"PaymentMethod"
"Churn"
"customerID"10.70713877598729410.85339882393390250.70751379622725240.76179376747887020.90834248687472450.84231854239613930.82411825890526640.82939685231701670.8240529912351410.82411001511071510.82895496415456050.8227563761615460.82273248571388720.83796522089846730.71903321956045490.86792914392832050.7810906445878953
"gender"0.707138775987294110.0018737116382607360.0018080796617890210.0105166119227340770.0064876721362081990.0094516665704529760.0113198160238208880.01702486739969520.0137983573777455770.00602924423843103140.0097222412174553550.0088443280426680360.0106407648648404640.0081525573496048910.0117536912212221930.018645841077588510.008612095078997812
"SeniorCitizen"0.85339882393390250.00187371163826073610.0164786575974139140.211185088493957650.0085764010792794360.14664289815476040.265324012474907060.211070265514393870.182773716002051830.183071178714984240.224050498070303140.185585792098413220.188961931310887260.14386836649843890.156529559311172770.195984515894879090.1508893281764731
"Partner"0.70751379622725240.0018080796617890210.01647865759741391410.45267628292946380.0177056632239720320.143897685113320320.00090735212082897140.15200981443032940.153336852217446860.166568261906738070.127641560829126250.137342655614560470.12957389839577670.296039475937436850.014876622287891040.162762278937051970.15044754495917656
"Dependents"0.76179376747887020.0105166119227340770.211185088493957650.452676282929463810.00176167854468371050.0250940167139795870.18026724788255380.194579243768499970.16276014139235460.157570107102989830.18237744041561060.147082439573214430.14133537396088380.244212236084413360.111377229193644260.150744110492830120.16422140157972528
"PhoneService"0.90834248687472450.0064876721362081990.0085764010792794360.0177056632239720320.001761678544683710510.90834248687472450.37568770981934810.10045433406594660.094261323516926450.09334037086154530.101685020269790140.10759057322946650.102628431390149850.0039761173477146810.016504805732569660.0102972217665900770.01194198002900308
"MultipleLines"0.84231854239613930.0094516665704529760.14664289815476040.143897685113320320.0250940167139795870.908342486874724510.354425640546093770.205498245028874230.222742501607039570.224040380424223360.206116785560806930.240923906648774420.241537641895443880.080407662269149670.166340632339777420.16692980803824730.04010925283378872
"InternetService"0.82411825890526640.0113198160238208880.265324012474907060.00090735212082897140.18026724788255380.37568770981934810.3544256405460937710.59592611193974580.57479189518557480.57469792597539130.59402636479197710.58694755724528350.58561372463621160.206920914028970670.37842736542051740.31312300237781060.32245455521230887
"OnlineSecurity"0.82939685231701670.01702486739969520.211070265514393870.15200981443032940.194579243768499970.10045433406594660.205498245028874230.595926111939745810.58857831342263510.5871827042571950.60635781883424430.5755352689642840.57605175249524430.301348732505670350.341799755425112760.304958746013804350.3474004326740551
"OnlineBackup"0.8240529912351410.0137983573777455770.182773716002051830.153336852217446860.16276014139235460.094261323516926450.222742501607039570.57479189518557480.588578313422635110.58942138800276720.59028630752378310.58401318355688880.58276763979447420.25857178752847320.32104582079805370.2825166407061620.2923155121954445
"DeviceProtection"0.82411001511071510.00602924423843103140.183071178714984240.166568261906738070.157570107102989830.09334037086154530.224040380424223360.57469792597539130.5871827042571950.589421388002767210.59837892460785950.60707079693652650.60995272913035530.296976493135118040.32163124576650220.287021160417336050.281579732968073
"TechSupport"0.82895496415456050.0097222412174553550.224050498070303140.127641560829126250.18237744041561060.101685020269790140.206116785560806930.59402636479197710.60635781883424430.59028630752378310.598378924607859510.58593004408495060.58600362005535870.33102091764887370.32952430875439180.30688837112191490.3429161982469257
"StreamingTV"0.8227563761615460.0088443280426680360.185585792098413220.137342655614560470.147082439573214430.10759057322946650.240923906648774420.58694755724528350.5755352689642840.58401318355688880.60707079693652650.585930044084950610.65175307251136840.23590136543928770.33606068627428470.27367105778774930.2305023384466809
"StreamingMovies"0.82273248571388720.0106407648648404640.188961931310887260.12957389839577670.14133537396088380.102628431390149850.241537641895443880.58561372463621160.57605175249524430.58276763979447420.60995272913035530.58600362005535870.651753072511368410.2386398563537620.33211092493359170.274658739334631030.230950809069268
"Contract"0.83796522089846730.0081525573496048910.14386836649843890.296039475937436850.244212236084413360.0039761173477146810.080407662269149670.206920914028970670.301348732505670350.25857178752847320.296976493135118040.33102091764887370.23590136543928770.23863985635376210.177645219971311640.26665474765414910.4101156965761409
"PaperlessBilling"0.71903321956045490.0117536912212221930.156529559311172770.014876622287891040.111377229193644260.016504805732569660.166340632339777420.37842736542051740.341799755425112760.32104582079805370.32163124576650220.32952430875439180.33606068627428470.33211092493359170.1776452199713116410.248718572671984660.1918253316664679
"PaymentMethod"0.86792914392832050.018645841077588510.195984515894879090.162762278937051970.150744110492830120.0102972217665900770.16692980803824730.31312300237781060.304958746013804350.2825166407061620.287021160417336050.30688837112191490.27367105778774930.274658739334631030.26665474765414910.2487185726719846610.3033586255540706
"Churn"0.78109064458789530.0086120950789978120.15088932817647310.150447544959176560.164221401579725280.011941980029003080.040109252833788720.322454555212308870.34740043267405510.29231551219544450.2815797329680730.34291619824692570.23050233844668090.2309508090692680.41011569657614090.19182533166646790.30335862555407061
Rows: 1-18 | Columns: 19

Sometimes, we just need to look at the correlation between a response and other variables. The parameter 'focus' will isolate and show us the specified correlation vector.

In [11]:
vdf.corr(method = "cramer", focus = "Churn")
Out[11]:
"Churn"
"Churn"1
"customerID"0.7810906445878953
"Contract"0.4101156965761409
"OnlineSecurity"0.3474004326740551
"TechSupport"0.3429161982469257
"InternetService"0.32245455521230887
"PaymentMethod"0.3033586255540706
"OnlineBackup"0.2923155121954445
"DeviceProtection"0.281579732968073
"StreamingMovies"0.230950809069268
"StreamingTV"0.2305023384466809
"PaperlessBilling"0.1918253316664679
"Dependents"0.16422140157972528
"SeniorCitizen"0.1508893281764731
"Partner"0.15044754495917656
"MultipleLines"0.04010925283378872
"PhoneService"0.01194198002900308
"gender"0.008612095078997812
Rows: 1-18 | Columns: 2

Sometimes a correlation coefficient can lead to incorrect assumptions, so we should always look at the coefficient p-value.

In [12]:
vdf.corr_pvalue("Churn", "customerID", method = "cramer",)
Out[12]:
(0.7810906445878953, 1.3659871749110484e-36)

We can see that churning correlates to the type of contract (monthly, yearly, etc.) which makes sense: you would expect that different types of contracts differ in flexibility for the customer, and particularly restrictive contracts may make churning more likely.

The type of internet service also seems to correlate with churning. Let's split the different categories to binaries to understand which services can influence the global churning rate.

In [13]:
vdf["InternetService"].one_hot_encode()
vdf.corr(method = "spearman", 
         focus = "Churn", 
         columns = ["InternetService_DSL", 
                    "InternetService_Fiber_optic"])
Out[13]:
"Churn"
"InternetService_Fiber_optic"0.308
"InternetService_DSL"-0.124
Rows: 1-2 | Columns: 2

We can see that the Fiber Optic option in particular seems to be directly linked to a customer's likelihood to churn. Let's compute some aggregations to find a causal relationship.

In [14]:
vdf["contract"].one_hot_encode()
vdf.groupby(["InternetService_Fiber_optic"], 
            ["AVG(tenure) AS tenure", 
             "AVG(totalcharges) AS totalcharges",
             'AVG("contract_month-to-month") AS "contract_month-to-month"',
             'AVG("monthlycharges") AS "monthlycharges"'])
Out[14]:
123
InternetService_Fiber_optic
Integer
123
tenure
Float
123
totalcharges
Float
123
contract_month-to-month
Float
123
monthlycharges
Float
1031.94223460856351558.065485264230.44261464403344343.7882442361287
2132.91795865633073205.304570413440.6873385012919991.5001291989664
Rows: 1-2 | Columns: 5

It seems that users with the Fiber Optic option tend more to churn not because of the option itself, but probably because of the type of contracts and the monthly charges the users are paying to get it. Be careful when dealing with identifying correlations! Remember: correlation doesn't imply causation!

Another important type of correlation is the autocorrelation. Let's use the Amazon dataset to understand it.

In [15]:
from verticapy.datasets import load_amazon
vdf = load_amazon()
display(vdf)
📅
date
Date
Abc
state
Varchar(32)
123
number
Int
11998-01-01ACRE0
21998-01-01ALAGOAS0
31998-01-01AMAPÁ0
41998-01-01AMAZONAS0
51998-01-01BAHIA0
61998-01-01CEARÁ0
71998-01-01DISTRITO FEDERAL0
81998-01-01ESPÍRITO SANTO0
91998-01-01GOIÁS0
101998-01-01MARANHÃO0
111998-01-01MATO GROSSO0
121998-01-01MATO GROSSO DO SUL0
131998-01-01MINAS GERAIS0
141998-01-01PARANÁ0
151998-01-01PARAÍBA0
161998-01-01PARÁ0
171998-01-01PERNAMBUCO0
181998-01-01PIAUÍ0
191998-01-01RIO DE JANEIRO0
201998-01-01RIO GRANDE DO NORTE0
211998-01-01RIO GRANDE DO SUL0
221998-01-01RONDÔNIA0
231998-01-01RORAIMA0
241998-01-01SANTA CATARINA0
251998-01-01SERGIPE0
261998-01-01SÃO PAULO0
271998-01-01TOCANTINS0
281998-02-01ACRE0
291998-02-01ALAGOAS0
301998-02-01AMAPÁ0
311998-02-01AMAZONAS0
321998-02-01BAHIA0
331998-02-01CEARÁ0
341998-02-01DISTRITO FEDERAL0
351998-02-01ESPÍRITO SANTO0
361998-02-01GOIÁS0
371998-02-01MARANHÃO0
381998-02-01MATO GROSSO0
391998-02-01MATO GROSSO DO SUL0
401998-02-01MINAS GERAIS0
411998-02-01PARANÁ0
421998-02-01PARAÍBA0
431998-02-01PARÁ0
441998-02-01PERNAMBUCO0
451998-02-01PIAUÍ0
461998-02-01RIO DE JANEIRO0
471998-02-01RIO GRANDE DO NORTE0
481998-02-01RIO GRANDE DO SUL0
491998-02-01RONDÔNIA0
501998-02-01RORAIMA0
511998-02-01SANTA CATARINA0
521998-02-01SERGIPE0
531998-02-01SÃO PAULO0
541998-02-01TOCANTINS0
551998-03-01ACRE0
561998-03-01ALAGOAS0
571998-03-01AMAPÁ0
581998-03-01AMAZONAS0
591998-03-01BAHIA0
601998-03-01CEARÁ0
611998-03-01DISTRITO FEDERAL0
621998-03-01ESPÍRITO SANTO0
631998-03-01GOIÁS0
641998-03-01MARANHÃO0
651998-03-01MATO GROSSO0
661998-03-01MATO GROSSO DO SUL0
671998-03-01MINAS GERAIS0
681998-03-01PARANÁ0
691998-03-01PARAÍBA0
701998-03-01PARÁ0
711998-03-01PERNAMBUCO0
721998-03-01PIAUÍ0
731998-03-01RIO DE JANEIRO0
741998-03-01RIO GRANDE DO NORTE0
751998-03-01RIO GRANDE DO SUL0
761998-03-01RONDÔNIA0
771998-03-01RORAIMA0
781998-03-01SANTA CATARINA0
791998-03-01SERGIPE0
801998-03-01SÃO PAULO0
811998-03-01TOCANTINS0
821998-04-01ACRE0
831998-04-01ALAGOAS0
841998-04-01AMAPÁ0
851998-04-01AMAZONAS0
861998-04-01BAHIA0
871998-04-01CEARÁ0
881998-04-01DISTRITO FEDERAL0
891998-04-01ESPÍRITO SANTO0
901998-04-01GOIÁS0
911998-04-01MARANHÃO0
921998-04-01MATO GROSSO0
931998-04-01MATO GROSSO DO SUL0
941998-04-01MINAS GERAIS0
951998-04-01PARANÁ0
961998-04-01PARAÍBA0
971998-04-01PARÁ0
981998-04-01PERNAMBUCO0
991998-04-01PIAUÍ0
1001998-04-01RIO DE JANEIRO0
Rows: 1-100 | Columns: 3

Our goal is to predict the number of forest fires in Brazil. To do this, we can draw an autocorrelation plot and a partial autocorrelation plot.

In [16]:
vdf.acf(column = "number",
        ts = "date",
        by = ["state"],
        p = 48,
        method = "pearson")
vdf.pacf(column = "number",
         ts = "date",
         by = ["state"],
         p = 48)

Out[16]:
value
confidence
01.00.024396841824873755
10.6726675295418580.033675228780048694
2-0.1887274038013820.034301750180211296
30.0222066882658490.03431296790442547
4-0.08197985013054340.03443207317154983
5-0.006636068540111950.03443550440495167
6-0.002730444441742870.03443830352079605
70.0145098509949780.03444461651417873
80.09848418613494230.03461467835735325
90.1440127337513830.034972633390635596
100.2345873172932720.03590106333622817
110.4244496636885960.03878033225579347
120.4621368903907860.04193874171442886
13-0.1709292926144580.04235541732162943
14-0.03098356452061310.0423722220603323
15-0.02016614662339940.042381237262525503
160.008803807426182450.042385619747699776
17-0.01441708367117880.04239183813723341
180.01058649109867540.04239670917644579
19-3.04350920105641e-050.04240000328911371
200.01267004572515920.04240555842646121
210.0619417632587560.042462844676922726
220.1211704694842580.04267213673556238
230.249246740314840.04353632913341579
240.2499597452568120.044388749935414466
25-0.1517760159495380.0447011914060506
26-0.04327561936263910.044729696703441166
270.04257249723889610.04475738662368059
28-0.0511616342279430.04479581330579359
290.02231391305136360.0448059438601625
30-0.01492133594110280.044812402245112425
31-0.002919322967544870.04481600427562471
32-0.001059896132460840.04481950838871394
330.02963822239912140.04483472136181398
340.008994385592363980.044839292596148154
350.2114332344637270.04543546774174034
360.1718208913450560.045826240829950876
37-0.07013118317836960.045894011277228176
38-0.00700029254940540.04589822691705164
390.03280601793205830.04591584235878527
40-0.04268156036855850.045943175691876184
410.001491088863557150.04594678657275398
42-0.009476950039686670.045951540276646934
43-0.02080178808820590.04596076565630418
44-0.02166572709488190.04597047036327498
45-0.03130073667391440.045986828122287306
46-0.05408128844380450.04602852459945253
47-0.02308926729866620.04603905984310686
48-0.06616045485411570.046099627206536
Rows: 1-49 | Columns: 3

We can see the seasonality forest fires.

It's mathematically impossible to build the perfect correlation function, but we still have several powerful functions at our disposal for finding relationships in all kinds of datasets.