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


Python extmath.weighted_mode函数代码示例

本文整理汇总了Python中sklearn.utils.extmath.weighted_mode函数的典型用法代码示例。如果您正苦于以下问题:Python weighted_mode函数的具体用法?Python weighted_mode怎么用?Python weighted_mode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


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

示例1: predict

    def predict(self, X):
        """Predict the class labels for the provided data

        Parameters
        ----------
        X: array
            A 2-D array representing the test points.

        Returns
        -------
        labels: array
            List of class labels (one for each data sample).
        """
        X = np.atleast_2d(X)

        neigh_dist, neigh_ind = self.kneighbors(X)
        pred_labels = self._y[neigh_ind]

        weights = _get_weights(neigh_dist, self.weights)

        if weights is None:
            mode, _ = smart_mode(pred_labels, axis=1)
        else:
            mode, _ = weighted_mode(pred_labels, weights, axis=1)

        return mode.flatten().astype(np.int)
开发者ID:dpmcsuss,项目名称:dtmrigraph,代码行数:26,代码来源:fastaprnn.py

示例2: test_uniform_weights

def test_uniform_weights():
    # with uniform weights, results should be identical to stats.mode
    x = np.random.randint(10, size=(10, 5))
    weights = np.ones(x.shape)

    for axis in (None, 0, 1):
        mode, score = stats.mode(x, axis)
        mode2, score2 = weighted_mode(x, weights, axis)

        assert np.all(mode == mode2)
        assert np.all(score == score2)
开发者ID:Scott-Alex,项目名称:scikit-learn,代码行数:11,代码来源:test_weighted_mode.py

示例3: test_uniform_weights

def test_uniform_weights():
    # with uniform weights, results should be identical to stats.mode
    rng = np.random.RandomState(0)
    x = rng.randint(10, size=(10, 5))
    weights = np.ones(x.shape)

    for axis in (None, 0, 1):
        mode, score = stats.mode(x, axis)
        mode2, score2 = weighted_mode(x, weights, axis)

        assert_array_equal(mode, mode2)
        assert_array_equal(score, score2)
开发者ID:BasilBeirouti,项目名称:scikit-learn,代码行数:12,代码来源:test_extmath.py

示例4: test_random_weights

def test_random_weights():
    # set this up so that each row should have a weighted mode of 6,
    # with a score that is easily reproduced
    mode_result = 6

    x = np.random.randint(mode_result, size=(100, 10))
    w = np.random.random(x.shape)

    x[:, :5] = mode_result
    w[:, :5] += 1

    mode, score = weighted_mode(x, w, axis=1)

    assert np.all(mode == mode_result)
    assert np.all(score.ravel() == w[:, :5].sum(1))
开发者ID:Scott-Alex,项目名称:scikit-learn,代码行数:15,代码来源:test_weighted_mode.py

示例5: test_random_weights

def test_random_weights():
    # set this up so that each row should have a weighted mode of 6,
    # with a score that is easily reproduced
    mode_result = 6

    rng = np.random.RandomState(0)
    x = rng.randint(mode_result, size=(100, 10))
    w = rng.random_sample(x.shape)

    x[:, :5] = mode_result
    w[:, :5] += 1

    mode, score = weighted_mode(x, w, axis=1)

    np.testing.assert_array_equal(mode, mode_result)
    np.testing.assert_array_almost_equal(score.ravel(), w[:, :5].sum(1))
开发者ID:93sam,项目名称:scikit-learn,代码行数:16,代码来源:test_extmath.py

示例6: predict

    def predict(self, X, idx=None):

        neigh_dist, neigh_ind = self.kneighbors(X,idx)
        pred_labels = self._y[neigh_ind]

        weights = _get_weights(neigh_dist, self.weights)

        if weights is None:
            mode, _ = stats.mode(pred_labels, axis=1)
        else:
            # Randomly permute the neighbors to tie-break randomly if necessary
            perm = np.random.permutation(n_neighbors)
            ind = ind[perm]
            mode, _ = weighted_mode(pred_labels,weights,axis)
            
        return mode.flatten().astype(np.int)
开发者ID:dpmcsuss,项目名称:stfpSim,代码行数:16,代码来源:fastaprnn.py

示例7: predict

    def predict(self, X):
        """Predict the class labels for the provided data
        Parameters
        ----------
        X : array-like, shape (n_query, n_features), \
                or (n_query, n_indexed) if metric == 'precomputed'
            Test samples.
        Returns
        -------
        y : array of shape [n_samples]
            Class labels for each data sample.
        """
        X = check_array(X, accept_sparse="csr")
        n_samples = X.shape[0]

        neigh_dist, neigh_ind = self.radius_neighbors(X)
        inliers = [i for i, nind in enumerate(neigh_ind) if len(nind) != 0]
        outliers = [i for i, nind in enumerate(neigh_ind) if len(nind) == 0]

        classes_ = self.classes_
        _y = self._y
        if not self.outputs_2d_:
            _y = self._y.reshape((-1, 1))
            classes_ = [self.classes_]
        n_outputs = len(classes_)

        if self.outlier_function is None and outliers:
            raise ValueError(
                "No neighbors found for test samples %r, "
                "you can try using larger radius, "
                "give a function for outliers, "
                "or consider removing them from your dataset." % outliers
            )

        if type(neigh_ind) is int:
            neigh_ind = [neigh_ind]

        weights = self.weight_function(neigh_dist=neigh_dist, neigh_ind=neigh_ind, target_space=self.target_space)

        y_pred = np.empty((n_samples, n_outputs), dtype=classes_[0].dtype)
        for k, classes_k in enumerate(classes_):
            pred_labels = np.array([_y[ind, k] for ind in neigh_ind], dtype=object)
            if weights is None:
                mode = np.array([stats.mode(pl)[0] for pl in pred_labels[inliers]], dtype=np.int)
            else:
                mode = np.array(
                    [weighted_mode(pl, w)[0] for (pl, w) in zip(pred_labels[inliers], weights)], dtype=np.int
                )

            mode = mode.ravel()

            y_pred[inliers, k] = classes_k.take(mode)

        if outliers:
            for outlier in outliers:
                y_pred[outlier, 0] = self.outlier_function.predict(X[outlier])

        if not self.outputs_2d_:
            y_pred = y_pred.ravel()

        return y_pred
开发者ID:emma-d-cotter,项目名称:ARTEMIS,代码行数:61,代码来源:classification.py


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