This repository has been archived by the owner on Jan 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from georgianpartners/issue_13
Fixes #13
- Loading branch information
Showing
4 changed files
with
144 additions
and
3 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,71 @@ | ||
import numpy as np | ||
import pandas as pd | ||
from sklearn.base import TransformerMixin, BaseEstimator | ||
from sklearn.utils import check_array | ||
from sklearn.utils.validation import check_is_fitted | ||
|
||
|
||
class DummyEncoder(BaseEstimator, TransformerMixin): | ||
"""Dummy encodes delimmited data within column of dataframe""" | ||
|
||
def __init__(self, delimeter=",", other_cutoff=0.1, other_name="other"): | ||
self.delimeter = delimeter | ||
self.other_cutoff = other_cutoff | ||
self.other_name = other_name | ||
|
||
def fit(self, X, y=None): | ||
"""Determines dummy categories | ||
Args: | ||
X (:obj:`numpy.ndarray`): Fit data | ||
Returns: | ||
self | ||
""" | ||
X = X.iloc[:, 0] | ||
X = X.str.get_dummies(sep=self.delimeter) | ||
self.other = (X.fillna(0).sum(axis=0) / X.count()) < self.other_cutoff | ||
|
||
self.categories = [c for c in list(X) if not self.other[c]] | ||
self.other = [c for c in list(X) if self.other[c]] | ||
if len(self.other) > 0: | ||
self.categories += [self.other_name] | ||
|
||
return self | ||
|
||
def transform(self, X, y=None): | ||
"""Performs Dummy Encoding on data | ||
Args: | ||
X (:obj:`numpy.ndarray`): X data | ||
Returns: | ||
:obj:`numpy.ndarray`: Transformed data | ||
""" | ||
|
||
check_is_fitted(self, ["categories"]) | ||
|
||
kwargs = { | ||
k: X.applymap(separate(k, self.delimeter, self.other, self.other_name)) | ||
.iloc[:, 0] | ||
.tolist() | ||
for k in self.categories | ||
} | ||
df = pd.DataFrame(kwargs) | ||
|
||
return df | ||
|
||
|
||
def separate(cat, delim, other, other_name): | ||
def sep(X): | ||
if cat == other_name: | ||
if set(other) & set(X.split(delim)): | ||
return 1 | ||
return 0 | ||
if cat in X.split(delim): | ||
return 1 | ||
return 0 | ||
|
||
return sep |
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