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

Python utils.atleast2d_or_csr函数代码示例

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

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



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

示例1: test_np_matrix

def test_np_matrix():
    """
    Confirm that input validation code does not return np.matrix
    """
    X = np.arange(12).reshape(3, 4)

    assert_false(isinstance(as_float_array(X), np.matrix))
    assert_false(isinstance(as_float_array(np.matrix(X)), np.matrix))
    assert_false(isinstance(as_float_array(sp.csc_matrix(X)), np.matrix))

    assert_false(isinstance(atleast2d_or_csr(X), np.matrix))
    assert_false(isinstance(atleast2d_or_csr(np.matrix(X)), np.matrix))
    assert_false(isinstance(atleast2d_or_csr(sp.csc_matrix(X)), np.matrix))

    assert_false(isinstance(atleast2d_or_csc(X), np.matrix))
    assert_false(isinstance(atleast2d_or_csc(np.matrix(X)), np.matrix))
    assert_false(isinstance(atleast2d_or_csc(sp.csr_matrix(X)), np.matrix))

    assert_false(isinstance(safe_asarray(X), np.matrix))
    assert_false(isinstance(safe_asarray(np.matrix(X)), np.matrix))
    assert_false(isinstance(safe_asarray(sp.lil_matrix(X)), np.matrix))

    assert_true(atleast2d_or_csr(X, copy=False) is X)
    assert_false(atleast2d_or_csr(X, copy=True) is X)
    assert_true(atleast2d_or_csc(X, copy=False) is X)
    assert_false(atleast2d_or_csc(X, copy=True) is X)
开发者ID:GbalsaC,项目名称:bitnamiP,代码行数:26,代码来源:test_validation.py


示例2: predict

    def predict(self, X):
        """Predict using the multi-layer perceptron model

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)

        Returns
        -------
        array, shape (n_samples)
            Predicted target values per element in X.
        """
        X = atleast2d_or_csr(X)
        scores = self.decision_function(X)

        if len(scores.shape) == 1 or self.multi_label is True:
            scores = logistic_sigmoid(scores)
            results = (scores > 0.5).astype(np.int)

            if self.multi_label:
                return self._lbin.inverse_transform(results)

        else:
            scores = _softmax(scores)
            results = scores.argmax(axis=1)

        return self.classes_[results]
开发者ID:ddofer,项目名称:NeuralNetworks,代码行数:27,代码来源:multilayer_perceptron.py


示例3: predict

    def predict(self, X, n_neighbors=1):
        """Perform classification on an array of test vectors X.

        The predicted class C for each sample in X is returned.

        Parameters
        ----------
        X : array-like, shape = [n_samples, n_features]

        Returns
        -------
        C : array, shape = [n_samples]

        Notes
        -----
        The default prediction is using KNeighborsClassifier, if the
        instance reducition algorithm is to be performed with another
        classifier, it should be explicited overwritten and explained
        in the documentation.
        """
        X = atleast2d_or_csr(X)
        if not hasattr(self, "X_") or self.X_ is None:
            raise AttributeError("Model has not been trained yet.")

        if not hasattr(self, "y_") or self.y_ is None:
            raise AttributeError("Model has not been trained yet.")

        if self.classifier == None:
            self.classifier = KNeighborsClassifier(n_neighbors=n_neighbors)

        self.classifier.fit(self.X_, self.y_)
        return self.classifier.predict(X)
开发者ID:dvro,项目名称:ml,代码行数:32,代码来源:baseNew.py


示例4: _joint_log_likelihood

 def _joint_log_likelihood(self, X):
     """Calculate the posterior log probability of the samples X"""
     X = atleast2d_or_csr(X)
     neg_prob = np.log(1 - np.exp(self.feature_log_prob_))
     jll = safe_sparse_dot(X, (self.feature_log_prob_ - neg_prob).T)
     jll += self.class_log_prior_ + neg_prob.sum(axis=1)
     return jll
开发者ID:katyasosa,项目名称:TSA,代码行数:7,代码来源:bayesian_naive_bayes.py


示例5: scatter

def scatter(data, labels=None, title=None, name=None):
    """2d PCA scatter plot with optional class info

    Return the pca model to be able to introspect the components or transform
    new data with the same model.
    """
    data = atleast2d_or_csr(data)

    if data.shape[1] == 2:
        # No need for a PCA:
        data_2d = data
    else:
        pca = RandomizedPCA(n_components=2)
        data_2d = pca.fit_transform(data)

    for i, c, m in zip(np.unique(labels), cycle(COLORS), cycle(MARKERS)):
        plt.scatter(data_2d[labels == i, 0], data_2d[labels == i, 1],
                    c=c, marker=m, label=i, alpha=0.5)

    plt.legend(loc='best')
    if title is None:
        title = "2D PCA scatter plot"
        if name is not None:
            title += " for " + name
    plt.xlabel('First Principal Component')
    plt.ylabel('Second Principal Component')
    plt.title(title)

    return pca
开发者ID:chrinide,项目名称:oglearn,代码行数:29,代码来源:visualization.py


示例6: fit

    def fit(self, X, y):
        if sparse.issparse(y):
            y = np.asarray(y.todense())

        self._enc = LabelEncoder()
        y = self._enc.fit_transform(y)

        if len(self.classes_) != 2:
            raise ValueError("The number of classes must be 2, "
                             "use sklearn.multiclass for more classes.")

        # The LabelEncoder maps the binary labels to 0 and 1 but the
        # training algorithm requires the labels to be -1 and +1.
        y[y==0] = -1

        X = atleast2d_or_csr(X, dtype=np.float64, order="C")

        if X.shape[0] != y.shape[0]:
            raise ValueError("X and y have incompatible shapes.\n"
                             "X has %s samples, but y has %s." %
                             (X.shape[0], y.shape[0]))

        self.weight_vector = WeightVector(X)

        if self.loop_type == constants.LOOP_BALANCED_STOCHASTIC:
            pegasos.train_stochastic_balanced(self, X, y)
        elif self.loop_type == constants.LOOP_STOCHASTIC:
            pegasos.train_stochastic(self, X, y)
        else:
            raise ValueError('%s: unknown loop type' % self.loop_type)

        return self
开发者ID:awesome,项目名称:pegasos,代码行数:32,代码来源:base.py


示例7: decision_function

    def decision_function(self, X):
        """Predict confidence scores for samples

        The confidence score for a sample is the signed distance of that
        sample to the hyperplane.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape = (n_samples, n_features)
            Samples.

        Returns
        -------
        array, shape=(n_samples,) if n_classes == 2 else (n_samples, n_classes)
            Confidence scores per (sample, class) combination. In the binary
            case, confidence score for self.classes_[1] where >0 means this
            class would be predicted.
        """
        # handle regression (least-squared loss)
        if not self.is_classif:
            return LinearModel.decision_function(self, X)

        X = atleast2d_or_csr(X)
        n_features = self.coef_.shape[1]
        if X.shape[1] != n_features:
            raise ValueError("X has %d features per sample; expecting %d"
                             % (X.shape[1], n_features))

        scores = safe_sparse_dot(X, self.coef_.T,
                                 dense_output=True) + self.intercept_
        return scores.ravel() if scores.shape[1] == 1 else scores
开发者ID:AlexandreAbraham,项目名称:nilearn,代码行数:31,代码来源:space_net.py


示例8: test_atleast2d_or_sparse

def test_atleast2d_or_sparse():
    for typ in [sp.csr_matrix, sp.dok_matrix, sp.lil_matrix, sp.coo_matrix]:
        X = typ(np.arange(9, dtype=float).reshape(3, 3))

        Y = atleast2d_or_csr(X, copy=True)
        assert_true(isinstance(Y, sp.csr_matrix))
        Y.data[:] = 1
        assert_array_equal(X.toarray().ravel(), np.arange(9))

        Y = atleast2d_or_csc(X, copy=False)
        Y.data[:] = 4
        assert_true(np.all(X.data == 4)
                    if isinstance(X, sp.csc_matrix)
                    else np.all(X.toarray().ravel() == np.arange(9)))

        Y = atleast2d_or_csr(X, dtype=np.float32)
        assert_true(Y.dtype == np.float32)
开发者ID:adammendoza,项目名称:scikit-learn,代码行数:17,代码来源:test_validation.py


示例9: _to_csr

    def _to_csr(self, X):
        """
        check & convert X to csr format
        """
        X = atleast2d_or_csr(X)
        if not sp.issparse(X):
            X = sp.csr_matrix(X)

        return X
开发者ID:hijbul,项目名称:topicModels,代码行数:9,代码来源:lda.py


示例10: decision_function

    def decision_function(self, X):

        X = atleast2d_or_csr(X)

        # compute hidden layer activations
        self.hidden_activations_ = self._get_hidden_activations(X)

        output = safe_sparse_dot(self.hidden_activations_, self.coef_output_)

        return output
开发者ID:ddofer,项目名称:NeuralNetworks,代码行数:10,代码来源:elm.py


示例11: predict

    def predict(self, X):
        X = atleast2d_or_csr(X)
        scores = self.decision_function(X)

        # if len(scores.shape) == 1:
        #scores = logistic_sigmoid(scores)
        #results = (scores > 0.5).astype(np.int)

        # else:
            #scores = _softmax(scores)
            #results = scores.argmax(axis=1)
            # self.classes_[results]
        return self._lbin.inverse_transform(scores)
开发者ID:ddofer,项目名称:NeuralNetworks,代码行数:13,代码来源:elm.py


示例12: predict

    def predict(self, X):
        """Predict using the multi-layer perceptron model

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape = [n_samples, n_features]

        Returns
        -------
        array, shape = [n_samples]
           Predicted target values per element in X.
        """
        X = atleast2d_or_csr(X)
        return super(DBNRegressor, self).decision_function(X)
开发者ID:ddofer,项目名称:NeuralNetworks,代码行数:14,代码来源:dbn.py


示例13: transform

    def transform(self, features):

        features = atleast2d_or_csr(features)

        if self.mean_ is not None:
            features = features - self.mean_

        features = np.dot(features, self.U_reduce);

        ##features = safe_sparse_dot(features, self.components.T)
        ## features = np.dot(np.transpose(self.U[:, :self.k_components]), features)

        #print 'features dimensions : ', features.shape

        return features
开发者ID:Oleksandra28,项目名称:FaceRecognizer,代码行数:15,代码来源:SPCA.py


示例14: fit_transform

    def fit_transform(self, X, y=None):
        """Apply dimensionality reduction on X.

        Parameters
        ----------
        X : array-like, shape (n_samples, n_features)
            New data, where n_samples in the number of samples
            and n_features is the number of features.

        Returns
        -------
        X_new : array-like, shape (n_samples, n_components)

        """
        X = self._fit(atleast2d_or_csr(X))
        X = safe_sparse_dot(X, self.components_.T)
        return X
开发者ID:xiaofeng007,项目名称:FaST-LMM,代码行数:17,代码来源:pca.py


示例15: fit

    def fit(self, X, y=None):
        """Generate a random hidden layer.
        Parameters
        ----------
        X : {array-like, sparse matrix} of shape [n_samples, n_features]
            Training set: only the shape is used to generate random component
            values for hidden units
        y : is not used: placeholder to allow for usage in a Pipeline.
        Returns
        -------
        self
        """
        X = atleast2d_or_csr(X)

        self._generate_components(X)

        return self
开发者ID:chrinide,项目名称:PyFV,代码行数:17,代码来源:random_layer.py


示例16: transform

    def transform(self, X, y=None):
        """Generate the random hidden layer's activations given X as input.
        Parameters
        ----------
        X : {array-like, sparse matrix}, shape [n_samples, n_features]
            Data to transform
        y : is not used: placeholder to allow for usage in a Pipeline.
        Returns
        -------
        X_new : numpy array of shape [n_samples, n_components]
        """
        X = atleast2d_or_csr(X)

        if (self.components_ is None):
            raise ValueError('No components initialized')

        return self._compute_hidden_activations(X)
开发者ID:chrinide,项目名称:PyFV,代码行数:17,代码来源:random_layer.py


示例17: score

    def score(self, X):
        """Computer lower bound on data, very naive implementation

        Parameters
        ----------
        data : array-like, shape (N, n_features)
            The data that needs to be fitted to and transformed

        Returns
        -------
        lower bound : int
            The lower bound on the log likelihood 
        """
        v = atleast2d_or_csr(X)
        rng = check_random_state(self.random_state)

        gradients, lowerbound = self._computeGradients(v.T, rng)
        return lowerbound/X.shape[0]
开发者ID:adaixiaoshuai,项目名称:Variational-Autoencoder,代码行数:18,代码来源:VariationalAutoencoder_withSK.py


示例18: transform

    def transform(self, X):
        """Transform the data X according to the fitted NMF model

        Parameters
        ----------

        X: {array-like, sparse matrix}, shape = [n_samples, n_features]
            Data matrix to be transformed by the model

        Returns
        -------
        data: array, [n_samples, n_components]
            Transformed data
        """
        X = atleast2d_or_csr(X)
        H = np.zeros((X.shape[0], self.n_components))
        for j in xrange(0, X.shape[0]):
            H[j, :], _ = nnls(self.components_.T, X[j, :])
        return H
开发者ID:steve-poulson,项目名称:inquisition,代码行数:19,代码来源:nmf2.py


示例19: inverse_transform

    def inverse_transform(self, X, dict_type=dict, inverse_onehot=True):
        """Transform array or sparse matrix X back to feature mappings.

        X must have been produced by this DictVectorizer's transform or
        fit_transform method; it may only have passed through transformers
        that preserve the number of features and their order.

        In the case of one-hot/one-of-K coding, the constructed feature
        names and values are returned rather than the original ones.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape = [n_samples, n_features]
            Sample matrix.
        dict_type : callable, optional
            Constructor for feature mappings. Must conform to the
            collections.Mapping API.

        Returns
        -------
        D : list of dict_type objects, length = n_samples
            Feature mappings for the samples in X.
        """
        X = atleast2d_or_csr(X)     # COO matrix is not subscriptable
        n_samples = X.shape[0]

        names = self.feature_names_
        dicts = [dict_type() for _ in xrange(n_samples)]

        if sp.issparse(X):
            for i, j in zip(*X.nonzero()):
                if reverse_onehot and names[j] in self._onehot_dict and X[i, j]:
                    dicts[i][self._onehot_dict[names[j]][0]] = self._onehot_dict[names[j]][1]
                else:
                    dicts[i][names[j]] = X[i, j]
        else:
            for i, d in enumerate(dicts):
                for j, v in enumerate(X[i, :]):
                    if v != 0:
                        d[names[j]] = X[i, j]

        return dicts
开发者ID:doxav,项目名称:skll,代码行数:42,代码来源:DictVectorizer.py


示例20: decision_function

    def decision_function(self, X):
        """Fit the model to the data X and target y.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)

        Returns
        -------
        array, shape (n_samples)
        Predicted target values per element in X.
        """
        X = atleast2d_or_csr(X)

        a_hidden = self.activation_func(safe_sparse_dot(X, self.coef_hidden_) + self.intercept_hidden_)
        output = safe_sparse_dot(a_hidden, self.coef_output_) + self.intercept_output_
        if output.shape[1] == 1:
            output = output.ravel()

        return output
开发者ID:uci-cbcl,项目名称:DeepCADD,代码行数:20,代码来源:multilayer_perceptron.py



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Python utils.check_X_y函数代码示例发布时间:2022-05-27
下一篇:
Python utils.as_float_array函数代码示例发布时间: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