当前位置: 首页>>代码示例>>Python>>正文


Python SimpleImputer.fit_transform方法代码示例

本文整理汇总了Python中sklearn.impute.SimpleImputer.fit_transform方法的典型用法代码示例。如果您正苦于以下问题:Python SimpleImputer.fit_transform方法的具体用法?Python SimpleImputer.fit_transform怎么用?Python SimpleImputer.fit_transform使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sklearn.impute.SimpleImputer的用法示例。


在下文中一共展示了SimpleImputer.fit_transform方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_imputation_error_invalid_strategy

# 需要导入模块: from sklearn.impute import SimpleImputer [as 别名]
# 或者: from sklearn.impute.SimpleImputer import fit_transform [as 别名]
def test_imputation_error_invalid_strategy(strategy):
    X = np.ones((3, 5))
    X[0, 0] = np.nan

    with pytest.raises(ValueError, match=str(strategy)):
        imputer = SimpleImputer(strategy=strategy)
        imputer.fit_transform(X)
开发者ID:psorianom,项目名称:scikit-learn,代码行数:9,代码来源:test_impute.py

示例2: test_imputation_deletion_warning

# 需要导入模块: from sklearn.impute import SimpleImputer [as 别名]
# 或者: from sklearn.impute.SimpleImputer import fit_transform [as 别名]
def test_imputation_deletion_warning(strategy):
    X = np.ones((3, 5))
    X[:, 0] = np.nan

    with pytest.warns(UserWarning, match="Deleting"):
        imputer = SimpleImputer(strategy=strategy, verbose=True)
        imputer.fit_transform(X)
开发者ID:psorianom,项目名称:scikit-learn,代码行数:9,代码来源:test_impute.py

示例3: test_imputation_mean_median_error_invalid_type

# 需要导入模块: from sklearn.impute import SimpleImputer [as 别名]
# 或者: from sklearn.impute.SimpleImputer import fit_transform [as 别名]
def test_imputation_mean_median_error_invalid_type(strategy, dtype):
    X = np.array([["a", "b", 3],
                  [4, "e", 6],
                  ["g", "h", 9]], dtype=dtype)

    with pytest.raises(ValueError, match="non-numeric data"):
        imputer = SimpleImputer(strategy=strategy)
        imputer.fit_transform(X)
开发者ID:psorianom,项目名称:scikit-learn,代码行数:10,代码来源:test_impute.py

示例4: test_imputation_constant_error_invalid_type

# 需要导入模块: from sklearn.impute import SimpleImputer [as 别名]
# 或者: from sklearn.impute.SimpleImputer import fit_transform [as 别名]
def test_imputation_constant_error_invalid_type(X_data, missing_value):
    # Verify that exceptions are raised on invalid fill_value type
    X = np.full((3, 5), X_data, dtype=float)
    X[0, 0] = missing_value

    with pytest.raises(ValueError, match="imputing numerical"):
        imputer = SimpleImputer(missing_values=missing_value,
                                strategy="constant",
                                fill_value="x")
        imputer.fit_transform(X)
开发者ID:psorianom,项目名称:scikit-learn,代码行数:12,代码来源:test_impute.py

示例5: test_imputation_shape

# 需要导入模块: from sklearn.impute import SimpleImputer [as 别名]
# 或者: from sklearn.impute.SimpleImputer import fit_transform [as 别名]
def test_imputation_shape():
    # Verify the shapes of the imputed matrix for different strategies.
    X = np.random.randn(10, 2)
    X[::2] = np.nan

    for strategy in ['mean', 'median', 'most_frequent']:
        imputer = SimpleImputer(strategy=strategy)
        X_imputed = imputer.fit_transform(X)
        assert_equal(X_imputed.shape, (10, 2))
        X_imputed = imputer.fit_transform(sparse.csr_matrix(X))
        assert_equal(X_imputed.shape, (10, 2))
开发者ID:LoveYakamoz,项目名称:scikit-learn,代码行数:13,代码来源:test_impute.py

示例6: test_imputation_shape

# 需要导入模块: from sklearn.impute import SimpleImputer [as 别名]
# 或者: from sklearn.impute.SimpleImputer import fit_transform [as 别名]
def test_imputation_shape():
    # Verify the shapes of the imputed matrix for different strategies.
    X = np.random.randn(10, 2)
    X[::2] = np.nan

    for strategy in ['mean', 'median', 'most_frequent', "constant"]:
        imputer = SimpleImputer(strategy=strategy)
        X_imputed = imputer.fit_transform(sparse.csr_matrix(X))
        assert X_imputed.shape == (10, 2)
        X_imputed = imputer.fit_transform(X)
        assert X_imputed.shape == (10, 2)

        iterative_imputer = IterativeImputer(initial_strategy=strategy)
        X_imputed = iterative_imputer.fit_transform(X)
        assert X_imputed.shape == (10, 2)
开发者ID:psorianom,项目名称:scikit-learn,代码行数:17,代码来源:test_impute.py

示例7: test_imputation_add_indicator

# 需要导入模块: from sklearn.impute import SimpleImputer [as 别名]
# 或者: from sklearn.impute.SimpleImputer import fit_transform [as 别名]
def test_imputation_add_indicator(marker):
    X = np.array([
        [marker, 1,      5,       marker, 1],
        [2,      marker, 1,       marker, 2],
        [6,      3,      marker,  marker, 3],
        [1,      2,      9,       marker, 4]
    ])
    X_true = np.array([
        [3., 1., 5., 1., 1., 0., 0., 1.],
        [2., 2., 1., 2., 0., 1., 0., 1.],
        [6., 3., 5., 3., 0., 0., 1., 1.],
        [1., 2., 9., 4., 0., 0., 0., 1.]
    ])

    imputer = SimpleImputer(missing_values=marker, add_indicator=True)
    X_trans = imputer.fit_transform(X)

    assert_allclose(X_trans, X_true)
    assert_array_equal(imputer.indicator_.features_, np.array([0, 1, 2, 3]))
开发者ID:allefpablo,项目名称:scikit-learn,代码行数:21,代码来源:test_impute.py

示例8: __call__

# 需要导入模块: from sklearn.impute import SimpleImputer [as 别名]
# 或者: from sklearn.impute.SimpleImputer import fit_transform [as 别名]
 def __call__(self, data):
     from Orange.data.sql.table import SqlTable
     if isinstance(data, SqlTable):
         return Impute()(data)
     imputer = SimpleImputer(strategy=self.strategy)
     X = imputer.fit_transform(data.X)
     # Create new variables with appropriate `compute_value`, but
     # drop the ones which do not have valid `imputer.statistics_`
     # (i.e. all NaN columns). `sklearn.preprocessing.Imputer` already
     # drops them from the transformed X.
     features = [impute.Average()(data, var, value)
                 for var, value in zip(data.domain.attributes,
                                       imputer.statistics_)
                 if not np.isnan(value)]
     assert X.shape[1] == len(features)
     domain = Orange.data.Domain(features, data.domain.class_vars,
                                 data.domain.metas)
     new_data = data.transform(domain)
     new_data.X = X
     return new_data
开发者ID:lanzagar,项目名称:orange3,代码行数:22,代码来源:preprocess.py

示例9: test_simple_imputation_add_indicator_sparse_matrix

# 需要导入模块: from sklearn.impute import SimpleImputer [as 别名]
# 或者: from sklearn.impute.SimpleImputer import fit_transform [as 别名]
def test_simple_imputation_add_indicator_sparse_matrix(arr_type):
    X_sparse = arr_type([
        [np.nan, 1, 5],
        [2, np.nan, 1],
        [6, 3, np.nan],
        [1, 2, 9]
    ])
    X_true = np.array([
        [3., 1., 5., 1., 0., 0.],
        [2., 2., 1., 0., 1., 0.],
        [6., 3., 5., 0., 0., 1.],
        [1., 2., 9., 0., 0., 0.],
    ])

    imputer = SimpleImputer(missing_values=np.nan, add_indicator=True)
    X_trans = imputer.fit_transform(X_sparse)

    assert sparse.issparse(X_trans)
    assert X_trans.shape == X_true.shape
    assert_allclose(X_trans.toarray(), X_true)
开发者ID:daniel-perry,项目名称:scikit-learn,代码行数:22,代码来源:test_impute.py

示例10: test_imputation_constant_object

# 需要导入模块: from sklearn.impute import SimpleImputer [as 别名]
# 或者: from sklearn.impute.SimpleImputer import fit_transform [as 别名]
def test_imputation_constant_object(marker):
    # Test imputation using the constant strategy on objects
    X = np.array([
        [marker, "a", "b", marker],
        ["c", marker, "d", marker],
        ["e", "f", marker, marker],
        ["g", "h", "i", marker]
    ], dtype=object)

    X_true = np.array([
        ["missing", "a", "b", "missing"],
        ["c", "missing", "d", "missing"],
        ["e", "f", "missing", "missing"],
        ["g", "h", "i", "missing"]
    ], dtype=object)

    imputer = SimpleImputer(missing_values=marker, strategy="constant",
                            fill_value="missing")
    X_trans = imputer.fit_transform(X)

    assert_array_equal(X_trans, X_true)
开发者ID:psorianom,项目名称:scikit-learn,代码行数:23,代码来源:test_impute.py

示例11: test_imputation_constant_integer

# 需要导入模块: from sklearn.impute import SimpleImputer [as 别名]
# 或者: from sklearn.impute.SimpleImputer import fit_transform [as 别名]
def test_imputation_constant_integer():
    # Test imputation using the constant strategy on integers
    X = np.array([
        [-1, 2, 3, -1],
        [4, -1, 5, -1],
        [6, 7, -1, -1],
        [8, 9, 0, -1]
    ])

    X_true = np.array([
        [0, 2, 3, 0],
        [4, 0, 5, 0],
        [6, 7, 0, 0],
        [8, 9, 0, 0]
    ])

    imputer = SimpleImputer(missing_values=-1, strategy="constant",
                            fill_value=0)
    X_trans = imputer.fit_transform(X)

    assert_array_equal(X_trans, X_true)
开发者ID:psorianom,项目名称:scikit-learn,代码行数:23,代码来源:test_impute.py

示例12: test_imputation_constant_pandas

# 需要导入模块: from sklearn.impute import SimpleImputer [as 别名]
# 或者: from sklearn.impute.SimpleImputer import fit_transform [as 别名]
def test_imputation_constant_pandas(dtype):
    # Test imputation using the constant strategy on pandas df
    pd = pytest.importorskip("pandas")

    f = io.StringIO("Cat1,Cat2,Cat3,Cat4\n"
                    ",i,x,\n"
                    "a,,y,\n"
                    "a,j,,\n"
                    "b,j,x,")

    df = pd.read_csv(f, dtype=dtype)

    X_true = np.array([
        ["missing_value", "i", "x", "missing_value"],
        ["a", "missing_value", "y", "missing_value"],
        ["a", "j", "missing_value", "missing_value"],
        ["b", "j", "x", "missing_value"]
    ], dtype=object)

    imputer = SimpleImputer(strategy="constant")
    X_trans = imputer.fit_transform(df)

    assert_array_equal(X_trans, X_true)
开发者ID:psorianom,项目名称:scikit-learn,代码行数:25,代码来源:test_impute.py

示例13: test_imputation_constant_float

# 需要导入模块: from sklearn.impute import SimpleImputer [as 别名]
# 或者: from sklearn.impute.SimpleImputer import fit_transform [as 别名]
def test_imputation_constant_float(array_constructor):
    # Test imputation using the constant strategy on floats
    X = np.array([
        [np.nan, 1.1, 0, np.nan],
        [1.2, np.nan, 1.3, np.nan],
        [0, 0, np.nan, np.nan],
        [1.4, 1.5, 0, np.nan]
    ])

    X_true = np.array([
        [-1, 1.1, 0, -1],
        [1.2, -1, 1.3, -1],
        [0, 0, -1, -1],
        [1.4, 1.5, 0, -1]
    ])

    X = array_constructor(X)

    X_true = array_constructor(X_true)

    imputer = SimpleImputer(strategy="constant", fill_value=-1)
    X_trans = imputer.fit_transform(X)

    assert_allclose_dense_sparse(X_trans, X_true)
开发者ID:psorianom,项目名称:scikit-learn,代码行数:26,代码来源:test_impute.py


注:本文中的sklearn.impute.SimpleImputer.fit_transform方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。