當前位置: 首頁>>代碼示例>>Python>>正文


Python LabelEncoder.fit方法代碼示例

本文整理匯總了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])
開發者ID:huyng,項目名稱:scikit-learn,代碼行數:10,代碼來源:test_label.py

示例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)
開發者ID:manhhomienbienthuy,項目名稱:scikit-learn,代碼行數:11,代碼來源:test_label.py

示例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])
開發者ID:manhhomienbienthuy,項目名稱:scikit-learn,代碼行數:11,代碼來源:test_label.py

示例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])
開發者ID:tguillemot,項目名稱:scikit-learn,代碼行數:12,代碼來源:test_label.py

示例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"])
開發者ID:andywangpku,項目名稱:scikit-learn,代碼行數:13,代碼來源:test_label.py

示例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")
開發者ID:tguillemot,項目名稱:scikit-learn,代碼行數:14,代碼來源:test_label.py

示例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])
開發者ID:NelleV,項目名稱:scikit-learn,代碼行數:14,代碼來源:test_label.py

示例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)
開發者ID:manhhomienbienthuy,項目名稱:scikit-learn,代碼行數:16,代碼來源:test_label.py

示例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")
開發者ID:manhhomienbienthuy,項目名稱:scikit-learn,代碼行數:7,代碼來源:test_label.py


注:本文中的sklearn.preprocessing.label.LabelEncoder.fit方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。