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


Python utils.safe_indexing方法代码示例

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


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

示例1: test_safe_indexing_pandas

# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import safe_indexing [as 别名]
def test_safe_indexing_pandas():
    try:
        import pandas as pd
    except ImportError:
        raise SkipTest("Pandas not found")
    X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    X_df = pd.DataFrame(X)
    inds = np.array([1, 2])
    X_df_indexed = safe_indexing(X_df, inds)
    X_indexed = safe_indexing(X_df, inds)
    assert_array_equal(np.array(X_df_indexed), X_indexed)
    # fun with read-only data in dataframes
    # this happens in joblib memmapping
    X.setflags(write=False)
    X_df_readonly = pd.DataFrame(X)
    inds_readonly = inds.copy()
    inds_readonly.setflags(write=False)

    for this_df, this_inds in product([X_df, X_df_readonly],
                                      [inds, inds_readonly]):
        with warnings.catch_warnings(record=True):
            X_df_indexed = safe_indexing(this_df, this_inds)

        assert_array_equal(np.array(X_df_indexed), X_indexed) 
开发者ID:PacktPublishing,项目名称:Mastering-Elasticsearch-7.0,代码行数:26,代码来源:test_utils.py

示例2: test_safe_indexing

# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import safe_indexing [as 别名]
def test_safe_indexing():
    X = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    inds = np.array([1, 2])
    X_inds = safe_indexing(X, inds)
    X_arrays = safe_indexing(np.array(X), inds)
    assert_array_equal(np.array(X_inds), X_arrays)
    assert_array_equal(np.array(X_inds), np.array(X)[inds]) 
开发者ID:PacktPublishing,项目名称:Mastering-Elasticsearch-7.0,代码行数:9,代码来源:test_utils.py

示例3: test_safe_indexing_mock_pandas

# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import safe_indexing [as 别名]
def test_safe_indexing_mock_pandas():
    X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    X_df = MockDataFrame(X)
    inds = np.array([1, 2])
    X_df_indexed = safe_indexing(X_df, inds)
    X_indexed = safe_indexing(X_df, inds)
    assert_array_equal(np.array(X_df_indexed), X_indexed) 
开发者ID:PacktPublishing,项目名称:Mastering-Elasticsearch-7.0,代码行数:9,代码来源:test_utils.py

示例4: _indexing_other

# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import safe_indexing [as 别名]
def _indexing_other(data, i):
    # sklearn's safe_indexing doesn't work with tuples since 0.22
    if isinstance(i, (int, np.integer, slice, tuple)):
        return data[i]
    return safe_indexing(data, i) 
开发者ID:skorch-dev,项目名称:skorch,代码行数:7,代码来源:utils.py

示例5: _shuffle

# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import safe_indexing [as 别名]
def _shuffle(y, groups, random_state):
    """Return a shuffled copy of y eventually shuffle among same groups."""
    if groups is None:
        indices = random_state.permutation(len(y))
    else:
        indices = np.arange(len(groups))
        for group in np.unique(groups):
            this_mask = groups == group
            indices[this_mask] = random_state.permutation(indices[this_mask])
    return safe_indexing(y, indices) 
开发者ID:poldracklab,项目名称:mriqc,代码行数:12,代码来源:_validation.py

示例6: _reorder

# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import safe_indexing [as 别名]
def _reorder(X, y, random_state, shuffle):
    # reorder if needed
    order = np.arange(X.shape[0])
    if shuffle:
        order = random_state.permutation(order)
    return safe_indexing(X, order), y[order] 
开发者ID:tgsmith61591,项目名称:skoot,代码行数:8,代码来源:base.py

示例7: _safe_indexing

# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import safe_indexing [as 别名]
def _safe_indexing(
    X,  # type: Union[OneDimArrayLikeType, TwoDimArrayLikeType]
    indices,  # type: OneDimArrayLikeType
):
    # type: (...) -> Union[OneDimArrayLikeType, TwoDimArrayLikeType]
    if X is None:
        return X

    return sklearn_safe_indexing(X, indices) 
开发者ID:optuna,项目名称:optuna,代码行数:11,代码来源:sklearn.py

示例8: get_from_ids

# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import safe_indexing [as 别名]
def get_from_ids(self, instance_ids):
        if self.streaming:
            raise StreamingUnsupported('get_from_ids is not supported for '
                                       'streaming features.')
        indices = [self.instance_ids.get_index(id_)
                   for id_ in instance_ids.ids]
        values = safe_indexing(self.values, indices)
        return Features(values, self.info, instance_ids) 
开发者ID:ANSSI-FR,项目名称:SecuML,代码行数:10,代码来源:features.py

示例9: get_from_indices

# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import safe_indexing [as 别名]
def get_from_indices(self, instance_ids, indices):
        if self.streaming:
            raise StreamingUnsupported('get_from_ids is not supported for '
                                       'streaming features.')
        if len(indices) > 0:
            values = safe_indexing(self.values, indices)
        else:
            values = np.empty((0, self.values.shape[1]))
        return Features(values, self.info, instance_ids) 
开发者ID:ANSSI-FR,项目名称:SecuML,代码行数:11,代码来源:features.py

示例10: my_davies_bouldin_score

# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import safe_indexing [as 别名]
def my_davies_bouldin_score(X, labels):
    """Computes the Davies-Bouldin score.
    The score is defined as the ratio of within-cluster distances to
    between-cluster distances.
    Read more in the :ref:`User Guide <davies-bouldin_index>`.
    Parameters
    ----------
    X : array-like, shape (``n_samples``, ``n_features``)
        List of ``n_features``-dimensional data points. Each row corresponds
        to a single data point.
    labels : array-like, shape (``n_samples``,)
        Predicted labels for each sample.
    Returns
    -------
    score: float
        The resulting Davies-Bouldin score.
    References
    ----------
    .. [1] Davies, David L.; Bouldin, Donald W. (1979).
       `"A Cluster Separation Measure"
       <https://ieeexplore.ieee.org/document/4766909>`__.
       IEEE Transactions on Pattern Analysis and Machine Intelligence.
       PAMI-1 (2): 224-227
    """
    X, labels = check_X_y(X, labels)
    le = LabelEncoder()
    labels = le.fit_transform(labels)
    n_samples, _ = X.shape
    n_labels = len(le.classes_)
    check_number_of_labels(n_labels, n_samples)

    intra_dists = np.zeros(n_labels)
    centroids = np.zeros((n_labels, len(X[0])), dtype=np.float)
    for k in range(n_labels):
        cluster_k = safe_indexing(X, labels == k)
        centroid = cluster_k.mean(axis=0)
        centroids[k] = centroid
        intra_dists[k] = np.average(pairwise_distances(
            cluster_k, [centroid]))

    # centroid_distances will contain zeros in the diagonal
    centroid_distances = pairwise_distances(centroids)

    if np.allclose(intra_dists, 0) or np.allclose(centroid_distances, 0):
        return 0.0

    # Compute score avoiding division by zero by adding an epsilon
    # this leads to high values in the diagonal's result
    score = (intra_dists[:, None] + intra_dists) / (centroid_distances + 1e-15)

    # Simply put the diagonal to zero
    score[np.eye(centroid_distances.shape[0]) == 1] = 0

    # Here is the original code
    # score = (intra_dists[:, None] + intra_dists) / (centroid_distances)
    # score[score == np.inf] = np.nan
    return np.mean(np.nanmax(score, axis=1)) 
开发者ID:h2oai,项目名称:driverlessai-recipes,代码行数:59,代码来源:KMeansClustering.py


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