-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
nominal.py
507 lines (460 loc) · 19.7 KB
/
nominal.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
import math
from collections import Counter
import numpy as np
import pandas as pd
import seaborn as sns
import scipy.stats as ss
import scipy.cluster.hierarchy as sch
import matplotlib.pyplot as plt
from ._private import (
convert, remove_incomplete_samples, replace_nan_with_value
)
__all__ = [
'associations',
'cluster_correlations',
'conditional_entropy',
'correlation_ratio',
'cramers_v',
'identify_nominal_columns',
'numerical_encoding',
'theils_u'
]
_REPLACE = 'replace'
_DROP = 'drop'
_DROP_SAMPLES = 'drop_samples'
_DROP_FEATURES = 'drop_features'
_SKIP = 'skip'
_DEFAULT_REPLACE_VALUE = 0.0
def conditional_entropy(x,
y,
nan_strategy=_REPLACE,
nan_replace_value=_DEFAULT_REPLACE_VALUE):
"""
Calculates the conditional entropy of x given y: S(x|y)
Wikipedia: https://en.wikipedia.org/wiki/Conditional_entropy
**Returns:** float
Parameters
----------
x : list / NumPy ndarray / Pandas Series
A sequence of measurements
y : list / NumPy ndarray / Pandas Series
A sequence of measurements
nan_strategy : string, default = 'replace'
How to handle missing values: can be either 'drop' to remove samples
with missing values, or 'replace' to replace all missing values with
the nan_replace_value. Missing values are None and np.nan.
nan_replace_value : any, default = 0.0
The value used to replace missing values with. Only applicable when
nan_strategy is set to 'replace'.
"""
if nan_strategy == _REPLACE:
x, y = replace_nan_with_value(x, y, nan_replace_value)
elif nan_strategy == _DROP:
x, y = remove_incomplete_samples(x, y)
y_counter = Counter(y)
xy_counter = Counter(list(zip(x, y)))
total_occurrences = sum(y_counter.values())
entropy = 0.0
for xy in xy_counter.keys():
p_xy = xy_counter[xy] / total_occurrences
p_y = y_counter[xy[1]] / total_occurrences
entropy += p_xy * math.log(p_y / p_xy)
return entropy
def cramers_v(x,
y,
nan_strategy=_REPLACE,
nan_replace_value=_DEFAULT_REPLACE_VALUE):
"""
Calculates Cramer's V statistic for categorical-categorical association.
Uses correction from Bergsma and Wicher, Journal of the Korean Statistical
Society 42 (2013): 323-328.
This is a symmetric coefficient: V(x,y) = V(y,x)
Original function taken from: https://stackoverflow.com/a/46498792/5863503
Wikipedia: https://en.wikipedia.org/wiki/Cram%C3%A9r%27s_V
**Returns:** float in the range of [0,1]
Parameters
----------
x : list / NumPy ndarray / Pandas Series
A sequence of categorical measurements
y : list / NumPy ndarray / Pandas Series
A sequence of categorical measurements
nan_strategy : string, default = 'replace'
How to handle missing values: can be either 'drop' to remove samples
with missing values, or 'replace' to replace all missing values with
the nan_replace_value. Missing values are None and np.nan.
nan_replace_value : any, default = 0.0
The value used to replace missing values with. Only applicable when
nan_strategy is set to 'replace'.
"""
if nan_strategy == _REPLACE:
x, y = replace_nan_with_value(x, y, nan_replace_value)
elif nan_strategy == _DROP:
x, y = remove_incomplete_samples(x, y)
confusion_matrix = pd.crosstab(x, y)
chi2 = ss.chi2_contingency(confusion_matrix)[0]
n = confusion_matrix.sum().sum()
phi2 = chi2 / n
r, k = confusion_matrix.shape
phi2corr = max(0, phi2 - ((k - 1) * (r - 1)) / (n - 1))
rcorr = r - ((r - 1)**2) / (n - 1)
kcorr = k - ((k - 1)**2) / (n - 1)
return np.sqrt(phi2corr / min((kcorr - 1), (rcorr - 1)))
def theils_u(x,
y,
nan_strategy=_REPLACE,
nan_replace_value=_DEFAULT_REPLACE_VALUE):
"""
Calculates Theil's U statistic (Uncertainty coefficient) for categorical-
categorical association. This is the uncertainty of x given y: value is
on the range of [0,1] - where 0 means y provides no information about
x, and 1 means y provides full information about x.
This is an asymmetric coefficient: U(x,y) != U(y,x)
Wikipedia: https://en.wikipedia.org/wiki/Uncertainty_coefficient
**Returns:** float in the range of [0,1]
Parameters
----------
x : list / NumPy ndarray / Pandas Series
A sequence of categorical measurements
y : list / NumPy ndarray / Pandas Series
A sequence of categorical measurements
nan_strategy : string, default = 'replace'
How to handle missing values: can be either 'drop' to remove samples
with missing values, or 'replace' to replace all missing values with
the nan_replace_value. Missing values are None and np.nan.
nan_replace_value : any, default = 0.0
The value used to replace missing values with. Only applicable when
nan_strategy is set to 'replace'.
"""
if nan_strategy == _REPLACE:
x, y = replace_nan_with_value(x, y, nan_replace_value)
elif nan_strategy == _DROP:
x, y = remove_incomplete_samples(x, y)
s_xy = conditional_entropy(x, y)
x_counter = Counter(x)
total_occurrences = sum(x_counter.values())
p_x = list(map(lambda n: n / total_occurrences, x_counter.values()))
s_x = ss.entropy(p_x)
if s_x == 0:
return 1
else:
return (s_x - s_xy) / s_x
def correlation_ratio(categories,
measurements,
nan_strategy=_REPLACE,
nan_replace_value=_DEFAULT_REPLACE_VALUE):
"""
Calculates the Correlation Ratio (sometimes marked by the greek letter Eta)
for categorical-continuous association.
Answers the question - given a continuous value of a measurement, is it
possible to know which category is it associated with?
Value is in the range [0,1], where 0 means a category cannot be determined
by a continuous measurement, and 1 means a category can be determined with
absolute certainty.
Wikipedia: https://en.wikipedia.org/wiki/Correlation_ratio
**Returns:** float in the range of [0,1]
Parameters
----------
categories : list / NumPy ndarray / Pandas Series
A sequence of categorical measurements
measurements : list / NumPy ndarray / Pandas Series
A sequence of continuous measurements
nan_strategy : string, default = 'replace'
How to handle missing values: can be either 'drop' to remove samples
with missing values, or 'replace' to replace all missing values with
the nan_replace_value. Missing values are None and np.nan.
nan_replace_value : any, default = 0.0
The value used to replace missing values with. Only applicable when
nan_strategy is set to 'replace'.
"""
if nan_strategy == _REPLACE:
categories, measurements = replace_nan_with_value(
categories, measurements, nan_replace_value)
elif nan_strategy == _DROP:
categories, measurements = remove_incomplete_samples(
categories, measurements)
categories = convert(categories, 'array')
measurements = convert(measurements, 'array')
fcat, _ = pd.factorize(categories)
cat_num = np.max(fcat) + 1
y_avg_array = np.zeros(cat_num)
n_array = np.zeros(cat_num)
for i in range(0, cat_num):
cat_measures = measurements[np.argwhere(fcat == i).flatten()]
n_array[i] = len(cat_measures)
y_avg_array[i] = np.average(cat_measures)
y_total_avg = np.sum(np.multiply(y_avg_array, n_array)) / np.sum(n_array)
numerator = np.sum(
np.multiply(n_array, np.power(np.subtract(y_avg_array, y_total_avg),
2)))
denominator = np.sum(np.power(np.subtract(measurements, y_total_avg), 2))
if numerator == 0:
eta = 0.0
else:
eta = np.sqrt(numerator / denominator)
return eta
def identify_nominal_columns(dataset, include=['object', 'category']):
"""Given a dataset, identify categorical columns.
Parameters:
-----------
dataset : a pandas dataframe
include : which column types to filter by; default: ['object', 'category'])
Returns:
--------
categorical_columns : a list of categorical columns
Example:
--------
>> df = pd.DataFrame({'col1': ['a', 'b', 'c', 'a'], 'col2': [3, 4, 2, 1]})
>> identify_nominal_columns(df)
['col1']
"""
dataset = convert(dataset, 'dataframe')
nominal_columns = list(dataset.select_dtypes(include=include).columns)
return nominal_columns
def associations(dataset,
nominal_columns='auto',
mark_columns=False,
theil_u=False,
plot=True,
return_results=False,
clustering=False,
nan_strategy=_REPLACE,
nan_replace_value=_DEFAULT_REPLACE_VALUE,
ax=None,
**kwargs):
"""
Calculate the correlation/strength-of-association of features in data-set
with both categorical (eda_tools) and continuous features using:
* Pearson's R for continuous-continuous cases
* Correlation Ratio for categorical-continuous cases
* Cramer's V or Theil's U for categorical-categorical cases
**Returns:** a DataFrame of the correlation/strength-of-association between
all features
**Example:** see `associations_example` under `dython.examples`
Parameters
----------
dataset : NumPy ndarray / Pandas DataFrame
The data-set for which the features' correlation is computed
nominal_columns : string / list / NumPy ndarray
Names of columns of the data-set which hold categorical values. Can
also be the string 'all' to state that all columns are categorical,
'auto' (default) to try to identify nominal columns, or None to state
none are categorical
mark_columns : Boolean, default = False
if True, output's columns' names will have a suffix of '(nom)' or
'(con)' based on there type (eda_tools or continuous), as provided
by nominal_columns
theil_u : Boolean, default = False
In the case of categorical-categorical feaures, use Theil's U instead
of Cramer's V
plot : Boolean, default = True
If True, plot a heat-map of the correlation matrix
return_results : Boolean, default = False
If True, the function will return a Pandas DataFrame of the computed
associations
clustering : Boolean, default = False
If True, hierarchical clustering is applied in order to sort
features into meaningful groups
nan_strategy : string, default = 'replace'
How to handle missing values: can be either 'drop_samples' to remove
samples with missing values, 'drop_features' to remove features
(columns) with missing values, or 'replace' to replace all missing
values with the nan_replace_value. Missing values are None and np.nan.
nan_replace_value : any, default = 0.0
The value used to replace missing values with. Only applicable when
nan_strategy is set to 'replace'
ax : matplotlib ax, default = None
Matplotlib Axis on which the heat-map will be plotted
kwargs : any key-value pairs
Arguments to be passed to used function and methods
"""
dataset = convert(dataset, 'dataframe')
if nan_strategy == _REPLACE:
dataset.fillna(nan_replace_value, inplace=True)
elif nan_strategy == _DROP_SAMPLES:
dataset.dropna(axis=0, inplace=True)
elif nan_strategy == _DROP_FEATURES:
dataset.dropna(axis=1, inplace=True)
columns = dataset.columns
if nominal_columns is None:
nominal_columns = list()
elif nominal_columns == 'all':
nominal_columns = columns
elif nominal_columns == 'auto':
nominal_columns = identify_nominal_columns(dataset)
corr = pd.DataFrame(index=columns, columns=columns)
for i in range(0, len(columns)):
for j in range(i, len(columns)):
if i == j:
corr[columns[i]][columns[j]] = 1.0
else:
if columns[i] in nominal_columns:
if columns[j] in nominal_columns:
if theil_u:
corr[columns[j]][columns[i]] = theils_u(
dataset[columns[i]],
dataset[columns[j]],
nan_strategy=_SKIP)
corr[columns[i]][columns[j]] = theils_u(
dataset[columns[j]],
dataset[columns[i]],
nan_strategy=_SKIP)
else:
cell = cramers_v(dataset[columns[i]],
dataset[columns[j]],
nan_strategy=_SKIP)
corr[columns[i]][columns[j]] = cell
corr[columns[j]][columns[i]] = cell
else:
cell = correlation_ratio(dataset[columns[i]],
dataset[columns[j]],
nan_strategy=_SKIP)
corr[columns[i]][columns[j]] = cell
corr[columns[j]][columns[i]] = cell
else:
if columns[j] in nominal_columns:
cell = correlation_ratio(dataset[columns[j]],
dataset[columns[i]],
nan_strategy=_SKIP)
corr[columns[i]][columns[j]] = cell
corr[columns[j]][columns[i]] = cell
else:
cell, _ = ss.pearsonr(dataset[columns[i]],
dataset[columns[j]])
corr[columns[i]][columns[j]] = cell
corr[columns[j]][columns[i]] = cell
corr.fillna(value=np.nan, inplace=True)
if mark_columns:
marked_columns = [
'{} (nom)'.format(col)
if col in nominal_columns else '{} (con)'.format(col)
for col in columns
]
corr.columns = marked_columns
corr.index = marked_columns
if clustering:
corr, _ = cluster_correlations(corr)
if plot:
if ax is None:
plt.figure(figsize=kwargs.get('figsize', None))
sns.heatmap(
corr,
cmap=kwargs.get('cmap', None),
annot=kwargs.get('annot', True),
fmt=kwargs.get('fmt', '.2f'),
ax=ax
)
if ax is None:
plt.show()
if return_results:
return corr
def numerical_encoding(dataset,
nominal_columns='auto',
drop_single_label=False,
drop_fact_dict=True,
nan_strategy=_REPLACE,
nan_replace_value=_DEFAULT_REPLACE_VALUE):
"""
Encoding a data-set with mixed data (numerical and categorical) to a
numerical-only data-set using the following logic:
* categorical with only a single value will be marked as zero (or dropped,
if requested)
* categorical with two values will be replaced with the result of Pandas
`factorize`
* categorical with more than two values will be replaced with the result
of Pandas `get_dummies`
* numerical columns will not be modified
**Returns:** DataFrame or (DataFrame, dict). If `drop_fact_dict` is True,
returns the encoded DataFrame.
else, returns a tuple of the encoded DataFrame and dictionary, where each
key is a two-value column, and the value is the original labels, as
supplied by Pandas `factorize`. Will be empty if no two-value columns are
present in the data-set
Parameters
----------
dataset : NumPy ndarray / Pandas DataFrame
The data-set to encode
nominal_columns : sequence / string. default = 'all'
A sequence of the nominal (categorical) columns in the dataset. If
string, must be 'all' to state that all columns are nominal. If None,
nothing happens. If 'auto', categorical columns will be identified
based on dtype.
drop_single_label : Boolean, default = False
If True, nominal columns with a only a single value will be dropped.
drop_fact_dict : Boolean, default = True
If True, the return value will be the encoded DataFrame alone. If
False, it will be a tuple of the DataFrame and the dictionary of the
binary factorization (originating from pd.factorize)
nan_strategy : string, default = 'replace'
How to handle missing values: can be either 'drop_samples' to remove
samples with missing values, 'drop_features' to remove features
(columns) with missing values, or 'replace' to replace all missing
values with the nan_replace_value. Missing values are None and np.nan.
nan_replace_value : any, default = 0.0
The value used to replace missing values with. Only applicable when nan
_strategy is set to 'replace'
"""
dataset = convert(dataset, 'dataframe')
if nan_strategy == _REPLACE:
dataset.fillna(nan_replace_value, inplace=True)
elif nan_strategy == _DROP_SAMPLES:
dataset.dropna(axis=0, inplace=True)
elif nan_strategy == _DROP_FEATURES:
dataset.dropna(axis=1, inplace=True)
if nominal_columns is None:
return dataset
elif nominal_columns == 'all':
nominal_columns = dataset.columns
elif nominal_columns == 'auto':
nominal_columns = identify_nominal_columns(dataset)
converted_dataset = pd.DataFrame()
binary_columns_dict = dict()
for col in dataset.columns:
if col not in nominal_columns:
converted_dataset.loc[:, col] = dataset[col]
else:
unique_values = pd.unique(dataset[col])
if len(unique_values) == 1 and not drop_single_label:
converted_dataset.loc[:, col] = 0
elif len(unique_values) == 2:
converted_dataset.loc[:, col], binary_columns_dict[
col] = pd.factorize(dataset[col])
else:
dummies = pd.get_dummies(dataset[col], prefix=col)
converted_dataset = pd.concat([converted_dataset, dummies],
axis=1)
if drop_fact_dict:
return converted_dataset
else:
return converted_dataset, binary_columns_dict
def cluster_correlations(corr_mat, indices=None):
'''
Apply agglomerative clustering in order to sort
a correlation matrix.
Based on https://github.com/TheLoneNut/CorrelationMatrixClustering/blob/master/CorrelationMatrixClustering.ipynb
Parameters
----------
- corr_mat : a square correlation matrix (pandas DataFrame)
- indices : cluster labels [None]; if not provided we'll do
an aglomerative clustering to get cluster labels.
Returns
-------
- corr : a sorted correlation matrix
- indices : cluster indexes based on the original dataset
Example
-------
>> correlations = associations(
customers,
return_results=True,
plot=False
)
>> correlations, _ = cluster_correlations(correlations)
'''
if indices is None:
X = corr_mat.values
d = sch.distance.pdist(X)
L = sch.linkage(d, method='complete')
indices = sch.fcluster(L, 0.5*d.max(), 'distance')
columns = [corr_mat.columns.tolist()[i]
for i in list((np.argsort(indices)))]
corr_mat = corr_mat.reindex(columns=columns).reindex(index=columns)
return corr_mat, indices