Skip to content

Commit f3b4e8b

Browse files
committed
first commit
0 parents  commit f3b4e8b

6 files changed

Lines changed: 138 additions & 0 deletions

File tree

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Rahul
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

OneHotEncode/OneHotEncode.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
def OneHotEncode(pandas_dataframe,category_columns=[],check_numerical=False,max_var=None):
2+
# Parameter explanation ----
3+
# pandas_dataframe -> The Pandas Dataframe object that contains the column you want to one-hot encode
4+
# category_columns -> List of column names in pandas_dataframe that you want to one-hot encode
5+
# override_alert (Default=False) -> A naive way of checking if the column contains numerical
6+
# data or is unsuitable for one-hot encoding
7+
# Set it to True to turn on the detection
8+
9+
import numpy as numpylib
10+
11+
# The dataframe is copied to a new variable
12+
df=pandas_dataframe.copy()
13+
14+
# List of list of names of all new columns made for a single column
15+
all_new_cols=[]
16+
17+
# List of dictionary of names of all new columns made for a single column
18+
new_col_dict=[]
19+
20+
# List of arrays containg the dropped columns that were originally input
21+
dropped_cols=[]
22+
23+
numerical_const=20
24+
25+
for col in category_columns:
26+
27+
# Total number of rows in each column
28+
total_rows=len(df[category_columns[0]].values)
29+
30+
category_elements=[]
31+
main_row=df[str(col)].values
32+
for category_element in main_row:
33+
category_elements.append(category_element)
34+
category_elements=list(set(category_elements))
35+
36+
if check_numerical:
37+
if max_var!=None:
38+
if len(category_elements) > max_var:
39+
print(col+' not suitable for One-Hot Encoding')
40+
continue
41+
else:
42+
if len(category_elements)>numerical_const:
43+
print(col+' has more variables than permitted')
44+
continue
45+
46+
if max_var != None:
47+
if len(category_elements) > max_var:
48+
print(col+' has more variables than allowed')
49+
continue
50+
51+
category_element_dict={}
52+
for i in range(len(category_elements)):
53+
category_element_dict[category_elements[i]]=i
54+
55+
category_element_reverse_dict={}
56+
for i in range(len(category_elements)):
57+
category_element_reverse_dict[col+str(i)]=category_elements[i]
58+
59+
dict_new_columns={}
60+
61+
category_element_str = [(col+str(x)) for x in range(len(category_elements))]
62+
for string in category_element_str:
63+
zero_row=numpylib.zeros((total_rows,), dtype=numpylib.int)
64+
dict_new_columns[string]=zero_row
65+
66+
colnames=[]
67+
for i in range(total_rows):
68+
colnames.append(category_element_str[category_element_dict[main_row[i]]])
69+
for i in range(total_rows):
70+
dict_new_columns[colnames[i]][i]=1
71+
for element in category_element_str:
72+
df[element]=dict_new_columns[element]
73+
74+
# Original columns are dropped from the dataframe
75+
df=df.drop(col,1)
76+
77+
all_new_cols.append(category_element_str)
78+
new_col_dict.append(category_element_reverse_dict)
79+
dropped_cols.append(main_row)
80+
81+
return df,dropped_cols,all_new_cols,new_col_dict

OneHotEncode/__init__.py

Whitespace-only changes.

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# One-Hot Encode
2+
A python script to deploy One-Hot encoding in Pandas Dataframes
3+
4+
Requirements -> Pandas, Numpy
5+
6+
#### Input -> (pandas_dataframe,cols,check_numerical=False,max_var=20)
7+
pandas_dataframe -> The Pandas Dataframe object that contains the column you want to one-hot encode
8+
cols -> List of column names in pandas_dataframe that you want to one-hot encode
9+
check_numerical (Default=False) -> A naive way of checking if the column contains numerical
10+
data or is unsuitable for one-hot encoding
11+
Set it to True to turn on the detection
12+
max_var (Default=20) -> Max number of diferent variables allowed in a category
13+
14+
#### Returns df,dropped_cols,all_new_cols,new_col_dict
15+
df -> Pandas dataframe that returns the one-hot encoded data with the original columns dropped
16+
dropped_cols -> List of arrays containg the dropped columns that were originally input
17+
all_new_cols -> List of list of names of all new columns made for a past single column
18+
new_col_dict -> List of dictionary of names of all new columns made for a past single column
19+
20+
21+
More info would be soon provided through a Medium page.

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[metadata]
2+
description-file = README.md

setup.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from distutils.core import setup
2+
setup(
3+
name = 'OneHotEncode',
4+
packages = ['OneHotEncode'], # this must be the same as the name above
5+
version = '0.1',
6+
description = 'A python script to deploy One-Hot encoding in Pandas Dataframes',
7+
author = 'Rahul Singh',
8+
author_email = 'singhrahuldps@gmail.com',
9+
url = 'https://github.com/singhrahuldps/OneHotEncode.git', # use the URL to the github repo
10+
download_url = 'https://github.com/singhrahuldps/OneHotEncode/archive/0.1.tar.gz', # I'll explain this in a second
11+
keywords = ['pandas', 'database', 'one-hot','one','hot','features','categorical','numerical','encode','encoding'], # arbitrary keywords
12+
classifiers = [],
13+
)

0 commit comments

Comments
 (0)