• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python extmath.weighted_mode函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python fixes.bincount函数代码示例发布时间:2022-05-27
下一篇:
Python extmath.squared_norm函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap