Label Encoding with LabelBinarizer in Scikit-Learn

Python

Transforms the column 'target' that contains three distinct classes to three columns with a binary value indicating whether which class the row in question belongs to.

 1|  from sklearn.preprocessing import LabelBinarizer
 2|  
 3|  # Step 1: Initialise and fit label binarizer
 4|  binarizer = LabelBinarizer(neg_label=0, pos_label=1)
 5|  binarizer.fit(df['target'])
 6|  print(binarizer.classes_)
 7|  
 8|  # Step 2: Transforms target column and assigns to array
 9|  binarized_target = binarizer.transform(df['target'])
10|  
11|  # Step 3: Convert array to DataFrame with column names and join back to
12|  # original DataFrame
13|  column_names = ['class_1', 'class_2', 'class_3']
14|  binarized_target = pd.DataFrame(binarized_target, columns=column_names)
15|  df = pd.concat([df,binarized_target],axis=1)
16|  
17|  """
18|  
19|  BEFORE TRANSFORMATION:
20|  Target
21|  3
22|  2
23|  2
24|  
25|  AFTER TRANSFORMATION:
26|  class_1	class_2		class_3
27|  0		0		1
28|  0		1		0
29|  0		1		0
30|  """
Did you find this snippet useful?

Sign up for free to to add this to your code library