Support dummy encoding similar to Pandas get_dummies()
- Dominant language
- C++
- Stars
- 1.9k
- Forks
- 164
- Avg merge
- 7h 31m
- Merged PRs (30d)
- 1
Description
The current method for `split_into_nhot` only supports one column, and doesn't return the original name of the column, only the possible values. Users may want to do something similar to what get_dummies() does in Pandas.
I created this simple function that does the trick, it's not that efficient but could be an idea on how to implement the solution:
```python
def ohe_columns(columns,df):
df_work = df.copy()
for column in columns:
df_ohe = dt.str.split_into_nhot(df_work[column])
df_ohe.names = [f'{column}_{col}' for col in df_ohe.names]
df_work.cbind(df_ohe)
return df_work
```
Example:
```python
df = dt.Frame([["cat","dog","rat","cat","dog"],["brown","black","black","brown","black"]],names=["animal","color"])
ohe_columns(["animal","color"],df)
```
Result:
| animal | color | animal_cat | animal_dog | animal_rat | color_brown | color_black |
|:------:|:-----:|:----------:|:----------:|:----------:|:-----------:|:-----------:|
| ▪▪▪▪ | ▪▪▪▪ | ▪ | ▪ | ▪ | ▪ | ▪ |
| cat | brown | 1 | 0 | 0 | 1 | 0 |
| dog | black | 0 | 1 | 0 | 0 | 1 |
| rat | black | 0 | 0 | 1 | 0 | 1 |
| cat | brown | 1 | 0 | 0 | 1 | 0 |
| dog | black | 0 | 1 | 0 | 0 | 1 |
I can help in the Python development of this function but not in the C++ part.
Contributor guide
Assessment
This issue has not been assessed yet.