This repository has been archived by the owner on Aug 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: start adding detection of associated features
- Loading branch information
1 parent
bc83f49
commit 4f200c8
Showing
7 changed files
with
167 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<article class="skrubview-wrapper"> | ||
<h2>Column pairwise associations</h2> | ||
<div style="font-size: 1.5rem; padding: var(--skrubview-small);"><strong>🚧 Work In Progress 🚨</strong></div> | ||
{% if summary["top_associations"] %} | ||
<table class="pure-table"> | ||
<thead> | ||
<tr> | ||
<th>Column 1</th> | ||
<th>Column 2</th> | ||
<th><a href="https://en.wikipedia.org/wiki/Cram%C3%A9r%27s_V">Cramér's V</a></th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{% for association in summary["top_associations"] %} | ||
<tr> | ||
<td>{{ association["left_column"] }}</td> | ||
<td>{{ association["right_column"] }}</td> | ||
<td | ||
{% if association["cramer_v"] is gt 0.9 %} | ||
class="skrubview-critical" | ||
{%- endif -%} | ||
> | ||
{{ association["cramer_v"] | format_number }} | ||
</td> | ||
</tr> | ||
{% endfor %} | ||
</tbody> | ||
</table> | ||
{% else %} | ||
No strong associations between any pair of columns were identified by a quick screening of a subsample of the dataframe. | ||
{% endif %} | ||
</article> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import warnings | ||
|
||
import numpy as np | ||
from sklearn.preprocessing import OneHotEncoder, KBinsDiscretizer | ||
|
||
_N_BINS = 10 | ||
_CATEGORICAL_THRESHOLD = 30 | ||
|
||
|
||
def stack_symmetric_associations(associations, column_names): | ||
left_indices, right_indices = np.triu_indices_from(associations, 1) | ||
associations = associations[(left_indices, right_indices)] | ||
order = np.argsort(associations)[::-1] | ||
left_indices, right_indices, associations = ( | ||
left_indices[order], | ||
right_indices[order], | ||
associations[order], | ||
) | ||
return [ | ||
(column_names[left], column_names[right], a) | ||
for (left, right, a) in zip(left_indices, right_indices, associations) | ||
] | ||
|
||
|
||
def chramer_v(df): | ||
df = df.__dataframe_consortium_standard__().persist() | ||
encoded = _onehot_encode(df, _N_BINS) | ||
table = _contingency_table(encoded) | ||
stats = _compute_cramer(table, df.shape()[0]) | ||
return stats | ||
|
||
|
||
def _onehot_encode(df, n_bins): | ||
n_rows, n_cols = df.shape() | ||
output = np.zeros((n_cols, n_bins, n_rows), dtype=bool) | ||
for col_idx, col_name in enumerate(df.column_names): | ||
values = np.asarray(df.col(col_name).to_array()) | ||
if values.dtype.kind in "bOSU" or len(set(values)) <= _CATEGORICAL_THRESHOLD: | ||
_onehot_encode_categories(values, n_bins, output[col_idx]) | ||
else: | ||
_onehot_encode_numbers(values, n_bins, output[col_idx]) | ||
return output | ||
|
||
|
||
def _onehot_encode_categories(values, n_bins, output): | ||
encoded = OneHotEncoder(max_categories=n_bins, sparse_output=False).fit_transform( | ||
values[:, None] | ||
) | ||
effective_n_bins = encoded.shape[1] | ||
output[:effective_n_bins] = encoded.T | ||
|
||
|
||
def _onehot_encode_numbers(values, n_bins, output): | ||
values = values.astype(float) | ||
mask = ~np.isfinite(values) | ||
filled_na = np.array(values) | ||
# TODO pick a better value & non-uniform bins? | ||
filled_na[mask] = 0.0 | ||
encoder = KBinsDiscretizer( | ||
n_bins=n_bins - 1, | ||
strategy="uniform", | ||
subsample=None, | ||
encode="onehot-dense", | ||
) | ||
with warnings.catch_warnings(): | ||
warnings.simplefilter("ignore") | ||
encoded = encoder.fit_transform(filled_na[:, None]) | ||
encoded[mask] = 0 | ||
effective_n_bins = encoded.shape[1] | ||
output[:effective_n_bins] = encoded.T | ||
output[effective_n_bins] = mask | ||
|
||
|
||
def _contingency_table(encoded): | ||
n_cols, n_quantiles, _ = encoded.shape | ||
out = np.empty((n_cols, n_cols, n_quantiles, n_quantiles), dtype="int32") | ||
return np.einsum("ack,bdk", encoded, encoded, out=out) | ||
|
||
|
||
def _compute_cramer(table, n_samples): | ||
marginal_0 = table.sum(axis=-2) | ||
marginal_1 = table.sum(axis=-1) | ||
expected = ( | ||
marginal_0[:, :, None, :] | ||
* marginal_1[:, :, :, None] | ||
/ marginal_0.sum(axis=-1)[:, :, None, None] | ||
) | ||
diff = table - expected | ||
expected[expected == 0] = 1 | ||
chi_stat = ((diff**2) / expected).sum(axis=-1).sum(axis=-1) | ||
min_dim = np.minimum( | ||
(marginal_0 > 0).sum(axis=-1) - 1, (marginal_1 > 0).sum(axis=-1) - 1 | ||
) | ||
stat = np.sqrt(chi_stat / (n_samples * np.maximum(min_dim, 1))) | ||
stat[min_dim == 0] = 0.0 | ||
return stat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters