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


Python LabelEncoder.transform方法代碼示例

本文整理匯總了Python中sklearn.preprocessing.label.LabelEncoder.transform方法的典型用法代碼示例。如果您正苦於以下問題:Python LabelEncoder.transform方法的具體用法?Python LabelEncoder.transform怎麽用?Python LabelEncoder.transform使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在sklearn.preprocessing.label.LabelEncoder的用法示例。


在下文中一共展示了LabelEncoder.transform方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_label_encoder

# 需要導入模塊: from sklearn.preprocessing.label import LabelEncoder [as 別名]
# 或者: from sklearn.preprocessing.label.LabelEncoder import transform [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

示例2: test_label_encoder

# 需要導入模塊: from sklearn.preprocessing.label import LabelEncoder [as 別名]
# 或者: from sklearn.preprocessing.label.LabelEncoder import transform [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

示例3: test_label_encoder_empty_array

# 需要導入模塊: from sklearn.preprocessing.label import LabelEncoder [as 別名]
# 或者: from sklearn.preprocessing.label.LabelEncoder import transform [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

示例4: test_label_encoder_negative_ints

# 需要導入模塊: from sklearn.preprocessing.label import LabelEncoder [as 別名]
# 或者: from sklearn.preprocessing.label.LabelEncoder import transform [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

示例5: load_dataset

# 需要導入模塊: from sklearn.preprocessing.label import LabelEncoder [as 別名]
# 或者: from sklearn.preprocessing.label.LabelEncoder import transform [as 別名]
    def load_dataset(self):
        with open(self.file_name) as f:
            dataset = arff.load(f)

            if self.label_attribute is None:
                self.label_attribute = dataset["attributes"][-1][0]

            data = list(numpy.asarray(dataset["data"]).transpose())
            labels = None

            row = 0
            for attribute_name, attribute_type in dataset["attributes"]:
                if attribute_name == self.label_attribute:
                    # Labels found!
                    labels = data.pop(row)
                    continue
                # Nominal attribute
                if isinstance(attribute_type, list):
                    # Convert None in '?' for next check and to make label_binarize work
                    for j in range(len(data[row])):
                        if data[row][j] is None:
                            data[row][j] = "?"
                    if numpy.all(data[row] == "?"):
                        # If no data is present, just remove the row
                        data.pop(row)
                        continue
                    if self.binarize:
                        data[row] = numpy.asarray(label_binarize(data[row], attribute_type), dtype=numpy.float64)
                    else:
                        encoder = LabelEncoder()
                        encoder.classes_ = attribute_type
                        if "?" not in encoder.classes_:
                            encoder.classes_.insert(0, "?")
                        data[row] = encoder.transform(data[row]).reshape((len(data[row]), 1)).astype(numpy.float64)
                else:
                    # Numeric attributes: check for nan values
                    data[row] = data[row].astype(numpy.float64)
                    nans = numpy.isnan(data[row])
                    if numpy.all(nans):
                        # If everything is nan, remove the feature
                        data.pop(row)
                        continue
                    if numpy.any(nans):
                        mean = data[row][numpy.invert(nans)].sum() / numpy.invert(nans).sum()
                        data[row][nans] = mean
                    # Reshape to do hstack later
                    data[row] = data[row].reshape((len(data[row]), 1))
                # Go to next row only if we have NOT removed the current one
                row += 1

            instances = numpy.hstack(tuple(data))
            useless_indices = numpy.where(instances.var(axis=0) == 0)
            instances = numpy.delete(instances, useless_indices, axis=1)

            return instances, labels
開發者ID:etamponi,項目名稱:eole,代碼行數:57,代碼來源:dataset_utils.py

示例6: test_label_encoder_string_labels

# 需要導入模塊: from sklearn.preprocessing.label import LabelEncoder [as 別名]
# 或者: from sklearn.preprocessing.label.LabelEncoder import transform [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

示例7: test_label_encoder

# 需要導入模塊: from sklearn.preprocessing.label import LabelEncoder [as 別名]
# 或者: from sklearn.preprocessing.label.LabelEncoder import transform [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


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