本文整理匯總了Python中sklearn.preprocessing.label.LabelEncoder.fit方法的典型用法代碼示例。如果您正苦於以下問題:Python LabelEncoder.fit方法的具體用法?Python LabelEncoder.fit怎麽用?Python LabelEncoder.fit使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sklearn.preprocessing.label.LabelEncoder
的用法示例。
在下文中一共展示了LabelEncoder.fit方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_label_encoder
# 需要導入模塊: from sklearn.preprocessing.label import LabelEncoder [as 別名]
# 或者: from sklearn.preprocessing.label.LabelEncoder import fit [as 別名]
def test_label_encoder():
"""Test LabelEncoder's transform and inverse_transform methods"""
le = LabelEncoder()
le.fit([1, 1, 4, 5, -1, 0])
assert_array_equal(le.classes_, [-1, 0, 1, 4, 5])
assert_array_equal(le.transform([0, 1, 4, 4, 5, -1, -1]), [1, 2, 3, 3, 4, 0, 0])
assert_array_equal(le.inverse_transform([1, 2, 3, 3, 4, 0, 0]), [0, 1, 4, 4, 5, -1, -1])
assert_raises(ValueError, le.transform, [0, 6])
示例2: test_label_encoder_empty_array
# 需要導入模塊: from sklearn.preprocessing.label import LabelEncoder [as 別名]
# 或者: from sklearn.preprocessing.label.LabelEncoder import fit [as 別名]
def test_label_encoder_empty_array(values):
le = LabelEncoder()
le.fit(values)
# test empty transform
transformed = le.transform([])
assert_array_equal(np.array([]), transformed)
# test empty inverse transform
inverse_transformed = le.inverse_transform([])
assert_array_equal(np.array([]), inverse_transformed)
示例3: test_label_encoder_negative_ints
# 需要導入模塊: from sklearn.preprocessing.label import LabelEncoder [as 別名]
# 或者: from sklearn.preprocessing.label.LabelEncoder import fit [as 別名]
def test_label_encoder_negative_ints():
le = LabelEncoder()
le.fit([1, 1, 4, 5, -1, 0])
assert_array_equal(le.classes_, [-1, 0, 1, 4, 5])
assert_array_equal(le.transform([0, 1, 4, 4, 5, -1, -1]),
[1, 2, 3, 3, 4, 0, 0])
assert_array_equal(le.inverse_transform([1, 2, 3, 3, 4, 0, 0]),
[0, 1, 4, 4, 5, -1, -1])
assert_raises(ValueError, le.transform, [0, 6])
示例4: test_label_encoder_errors
# 需要導入模塊: from sklearn.preprocessing.label import LabelEncoder [as 別名]
# 或者: from sklearn.preprocessing.label.LabelEncoder import fit [as 別名]
def test_label_encoder_errors():
# Check that invalid arguments yield ValueError
le = LabelEncoder()
assert_raises(ValueError, le.transform, [])
assert_raises(ValueError, le.inverse_transform, [])
# Fail on unseen labels
le = LabelEncoder()
le.fit([1, 2, 3, 1, -1])
assert_raises(ValueError, le.inverse_transform, [-1])
示例5: test_label_encoder_string_labels
# 需要導入模塊: from sklearn.preprocessing.label import LabelEncoder [as 別名]
# 或者: from sklearn.preprocessing.label.LabelEncoder import fit [as 別名]
def test_label_encoder_string_labels():
"""Test LabelEncoder's transform and inverse_transform methods with
non-numeric labels"""
le = LabelEncoder()
le.fit(["paris", "paris", "tokyo", "amsterdam"])
assert_array_equal(le.classes_, ["amsterdam", "paris", "tokyo"])
assert_array_equal(le.transform(["tokyo", "tokyo", "paris"]),
[2, 2, 1])
assert_array_equal(le.inverse_transform([2, 2, 1]),
["tokyo", "tokyo", "paris"])
assert_raises(ValueError, le.transform, ["london"])
示例6: test_label_encoder
# 需要導入模塊: from sklearn.preprocessing.label import LabelEncoder [as 別名]
# 或者: from sklearn.preprocessing.label.LabelEncoder import fit [as 別名]
def test_label_encoder():
# Test LabelEncoder's transform and inverse_transform methods
le = LabelEncoder()
le.fit([1, 1, 4, 5, -1, 0])
assert_array_equal(le.classes_, [-1, 0, 1, 4, 5])
assert_array_equal(le.transform([0, 1, 4, 4, 5, -1, -1]), [1, 2, 3, 3, 4, 0, 0])
assert_array_equal(le.inverse_transform([1, 2, 3, 3, 4, 0, 0]), [0, 1, 4, 4, 5, -1, -1])
assert_raises(ValueError, le.transform, [0, 6])
le.fit(["apple", "orange"])
msg = "bad input shape"
assert_raise_message(ValueError, msg, le.transform, "apple")
示例7: test_label_encoder_errors
# 需要導入模塊: from sklearn.preprocessing.label import LabelEncoder [as 別名]
# 或者: from sklearn.preprocessing.label.LabelEncoder import fit [as 別名]
def test_label_encoder_errors():
# Check that invalid arguments yield ValueError
le = LabelEncoder()
assert_raises(ValueError, le.transform, [])
assert_raises(ValueError, le.inverse_transform, [])
# Fail on unseen labels
le = LabelEncoder()
le.fit([1, 2, 3, -1, 1])
msg = "contains previously unseen labels"
assert_raise_message(ValueError, msg, le.inverse_transform, [-2])
assert_raise_message(ValueError, msg, le.inverse_transform, [-2, -3, -4])
示例8: test_label_encoder
# 需要導入模塊: from sklearn.preprocessing.label import LabelEncoder [as 別名]
# 或者: from sklearn.preprocessing.label.LabelEncoder import fit [as 別名]
def test_label_encoder(values, classes, unknown):
# Test LabelEncoder's transform, fit_transform and
# inverse_transform methods
le = LabelEncoder()
le.fit(values)
assert_array_equal(le.classes_, classes)
assert_array_equal(le.transform(values), [1, 0, 2, 0, 2])
assert_array_equal(le.inverse_transform([1, 0, 2, 0, 2]), values)
le = LabelEncoder()
ret = le.fit_transform(values)
assert_array_equal(ret, [1, 0, 2, 0, 2])
with pytest.raises(ValueError, match="unseen labels"):
le.transform(unknown)
示例9: test_label_encoder_str_bad_shape
# 需要導入模塊: from sklearn.preprocessing.label import LabelEncoder [as 別名]
# 或者: from sklearn.preprocessing.label.LabelEncoder import fit [as 別名]
def test_label_encoder_str_bad_shape(dtype):
le = LabelEncoder()
le.fit(np.array(["apple", "orange"], dtype=dtype))
msg = "bad input shape"
assert_raise_message(ValueError, msg, le.transform, "apple")