8000 Adding example for new_labels argument · scikit-learn/scikit-learn@ab788f7 · GitHub
[go: up one dir, main page]

Skip to content

Commit ab788f7

Browse files
committed
Adding example for new_labels argument
1 parent bb8d9a6 commit ab788f7

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

doc/modules/preprocessing.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,20 @@ hashable and comparable) to numerical labels::
417417
>>> list(le.inverse_transform([2, 2, 1]))
418418
['tokyo', 'tokyo', 'paris']
419419

420+
By default, ``LabelEncoder`` will throw a ``ValueError`` in the event that
421+
labels are passed in ``transform`` that were not seen in ``fit``. This
422+
behavior can be handled with the ``new_labels`` parameter, which supports
423+
``"raise"``, ``"nan"``, ``"update"``, and ``"label"`` strategies for
424+
handling new labels. For example, the ``"label"`` strategy will assign
425+
the unseen values a label of ``-1``.
426+
427+
>>> le = preprocessing.LabelEncoder(new_labels="label")
428+
>>> le.fit(["paris", "paris", "tokyo", "amsterdam"])
429+
LabelEncoder(new_label_class=-1, new_labels='label')
430+
>>> list(le.classes_)
431+
['amsterdam', 'paris', 'tokyo']
432+
>>> le.transform(["tokyo", "tokyo", "paris", "rome"])
433+
array([ 2, 2, 1, -1])
420434

421435
Imputation of missing values
422436
============================

0 commit comments

Comments
 (0)
0