Keras: LabelEncoder

From OnnoWiki
Jump to navigation Jump to search

sklearn.preprocessing.LabelEncoder

class sklearn.preprocessing.LabelEncoder[source]

Encode labels with value between 0 and n_classes-1. Read more in the User Guide.

Attributes:

classes_ : array of shape (n_class,)

Holds the label for each class. See also

sklearn.preprocessing.OrdinalEncoder

encode categorical features using a one-hot or ordinal encoding scheme.

Examples

LabelEncoder can be used to normalize labels.

from sklearn import preprocessing
le = preprocessing.LabelEncoder()
le.fit([1, 2, 2, 6])
LabelEncoder()
le.classes_
array([1, 2, 6])
le.transform([1, 1, 2, 6]) 
array([0, 0, 1, 2]...)
le.inverse_transform([0, 0, 1, 2])
array([1, 1, 2, 6])

It can also be used to transform non-numerical labels (as long as they are hashable and comparable) to numerical labels.

le = preprocessing.LabelEncoder()
le.fit(["paris", "paris", "tokyo", "amsterdam"])
LabelEncoder()
list(le.classes_)
['amsterdam', 'paris', 'tokyo']
le.transform(["tokyo", "tokyo", "paris"]) 
array([2, 2, 1]...)
list(le.inverse_transform([2, 2, 1]))
['tokyo', 'tokyo', 'paris']

Methods

fit(self, y) 	Fit label encoder
fit_transform(self, y) 	Fit label encoder and return encoded labels
get_params(self[, deep]) 	Get parameters for this estimator.
inverse_transform(self, y) 	Transform labels back to original encoding.
set_params(self, \*\*params) 	Set the parameters of this estimator.
transform(self, y) 	Transform labels to normalized encoding.


Pranala Menarik