當前位置: 首頁>>代碼示例>>Python>>正文


Python csr.csr_matrix方法代碼示例

本文整理匯總了Python中scipy.sparse.csr.csr_matrix方法的典型用法代碼示例。如果您正苦於以下問題:Python csr.csr_matrix方法的具體用法?Python csr.csr_matrix怎麽用?Python csr.csr_matrix使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在scipy.sparse.csr的用法示例。


在下文中一共展示了csr.csr_matrix方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: convert_input

# 需要導入模塊: from scipy.sparse import csr [as 別名]
# 或者: from scipy.sparse.csr import csr_matrix [as 別名]
def convert_input(X, columns=None, deep=False):
    """
    Unite data into a DataFrame.
    Objects that do not contain column names take the names from the argument.
    Optionally perform deep copy of the data.
    """
    if not isinstance(X, pd.DataFrame):
        if isinstance(X, pd.Series):
            X = pd.DataFrame(X, copy=deep)
        else:
            if columns is not None and np.size(X,1) != len(columns):
                raise ValueError('The count of the column names does not correspond to the count of the columns')
            if isinstance(X, list):
                X = pd.DataFrame(X, columns=columns, copy=deep)  # lists are always copied, but for consistency, we still pass the argument
            elif isinstance(X, (np.generic, np.ndarray)):
                X = pd.DataFrame(X, columns=columns, copy=deep)
            elif isinstance(X, csr_matrix):
                X = pd.DataFrame(X.todense(), columns=columns, copy=deep)
            else:
                raise ValueError('Unexpected input type: %s' % (str(type(X))))
    elif deep:
        X = X.copy(deep=True)

    return X 
開發者ID:scikit-learn-contrib,項目名稱:category_encoders,代碼行數:26,代碼來源:utils.py

示例2: test_binarizer_remove_first

# 需要導入模塊: from scipy.sparse import csr [as 別名]
# 或者: from scipy.sparse.csr import csr_matrix [as 別名]
def test_binarizer_remove_first(self):
        """...Test binarizer fit when remove_first=True
        """
        n_cuts = 3
        one_hot_encoder = OneHotEncoder(sparse=True)
        expected_binarization = one_hot_encoder.fit_transform(
            self.default_expected_intervals)

        binarizer = FeaturesBinarizer(method='quantile', n_cuts=n_cuts,
                                      detect_column_type="auto",
                                      remove_first=True)

        binarizer.fit(self.features)
        binarized_array = binarizer.transform(self.features)
        self.assertEqual(binarized_array.__class__, csr.csr_matrix)

        expected_binarization_without_first = \
            np.delete(expected_binarization.toarray(), [0, 4, 8, 10], 1)

        np.testing.assert_array_equal(expected_binarization_without_first,
                                      binarized_array.toarray())

        return 
開發者ID:X-DataInitiative,項目名稱:tick,代碼行數:25,代碼來源:features_binarizer_test.py

示例3: read_mtx

# 需要導入模塊: from scipy.sparse import csr [as 別名]
# 或者: from scipy.sparse.csr import csr_matrix [as 別名]
def read_mtx(filename: PathLike, dtype: str = "float32") -> AnnData:
    """\
    Read `.mtx` file.

    Parameters
    ----------
    filename
        The filename.
    dtype
        Numpy data type.
    """
    from scipy.io import mmread

    # could be rewritten accounting for dtype to be more performant
    X = mmread(fspath(filename)).astype(dtype)
    from scipy.sparse import csr_matrix

    X = csr_matrix(X)
    return AnnData(X, dtype=dtype) 
開發者ID:theislab,項目名稱:anndata,代碼行數:21,代碼來源:read.py

示例4: predict

# 需要導入模塊: from scipy.sparse import csr [as 別名]
# 或者: from scipy.sparse.csr import csr_matrix [as 別名]
def predict(self, X):
        """
        Predicts the classes for the samples. Takes the top k classes with smallest distance.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape = [n_samples, n_features]
            Prediction vector, where n_samples in the number of samples and
            n_features is the number of features.
        """
        predictions = csr_matrix((X.shape[0], self.y.shape[1]), dtype=np.int)
        
        topNIndices, _ = self._get_closest_centroids(X)
        
        for entry, label_list in enumerate(topNIndices):
            for label in label_list:
                predictions[entry, label] = 1
        return predictions 
開發者ID:quadflor,項目名稱:Quadflor,代碼行數:20,代碼來源:rocchioclassifier.py

示例5: train

# 需要導入模塊: from scipy.sparse import csr [as 別名]
# 或者: from scipy.sparse.csr import csr_matrix [as 別名]
def train(self, X, mean=None):
        """
        .. todo::

            WRITEME
        """
        warnings.warn('You should probably be using SparseMatPCA, '
                      'unless your design matrix fits in memory.')

        n, d = X.shape
        # Can't subtract a sparse vector from a sparse matrix, apparently,
        # so here I repeat the vector to construct a matrix.
        mean = X.mean(axis=0)
        mean_matrix = csr_matrix(mean.repeat(n).reshape((d, n))).T
        X = X - mean_matrix

        super(SparsePCA, self).train(X, mean=numpy.asarray(mean).squeeze()) 
開發者ID:zchengquan,項目名稱:TextDetector,代碼行數:19,代碼來源:pca.py

示例6: fit

# 需要導入模塊: from scipy.sparse import csr [as 別名]
# 或者: from scipy.sparse.csr import csr_matrix [as 別名]
def fit(self, hyperparameter_config, X, train_indices, dataset_info):
        hyperparameter_config = ConfigWrapper(self.get_name(), hyperparameter_config)

        normalizer_name = hyperparameter_config['normalization_strategy']

        if normalizer_name == 'none':
            return {'normalizer': None}

        if isinstance(X, csr_matrix):
            normalizer = self.normalization_strategies[normalizer_name](with_mean=False)
        else:
            normalizer = self.normalization_strategies[normalizer_name]()
        
        transformer = ColumnTransformer(transformers=[("normalize", normalizer, [i for i, c in enumerate(dataset_info.categorical_features) if not c])],
                                        remainder='passthrough')

        transformer.fit(X[train_indices])

        X = transformer.transform(X)
        
        dataset_info.categorical_features = sorted(dataset_info.categorical_features)

        return {'X': X, 'normalizer': transformer, 'dataset_info': dataset_info} 
開發者ID:automl,項目名稱:Auto-PyTorch,代碼行數:25,代碼來源:normalization_strategy_selector.py

示例7: test_awesome_cossim_top_one_zeros

# 需要導入模塊: from scipy.sparse import csr [as 別名]
# 或者: from scipy.sparse.csr import csr_matrix [as 別名]
def test_awesome_cossim_top_one_zeros():
    # test with one row matrix with all zeros
    # helper_awesome_cossim_top_sparse uses a local function awesome_cossim_top
    nr_vocab = 1000
    density = 0.1
    for _ in range(3):
        a_sparse = csr_matrix(np.zeros((1, nr_vocab)))
        b_sparse = rand(800, nr_vocab, density=density, format='csr')
        helper_awesome_cossim_topn_sparse(a_sparse, b_sparse) 
開發者ID:ing-bank,項目名稱:sparse_dot_topn,代碼行數:11,代碼來源:test_awesome_cossim_topn.py

示例8: test_awesome_cossim_top_all_zeros

# 需要導入模塊: from scipy.sparse import csr [as 別名]
# 或者: from scipy.sparse.csr import csr_matrix [as 別名]
def test_awesome_cossim_top_all_zeros():
    # test with all zeros matrix
    # helper_awesome_cossim_top_sparse uses a local function awesome_cossim_top
    nr_vocab = 1000
    density = 0.1
    for _ in range(3):
        a_sparse = csr_matrix(np.zeros((2, nr_vocab)))
        b_sparse = rand(800, nr_vocab, density=density, format='csr')
        helper_awesome_cossim_topn_sparse(a_sparse, b_sparse) 
開發者ID:ing-bank,項目名稱:sparse_dot_topn,代碼行數:11,代碼來源:test_awesome_cossim_topn.py

示例9: test_binarizer_fit

# 需要導入模塊: from scipy.sparse import csr [as 別名]
# 或者: from scipy.sparse.csr import csr_matrix [as 別名]
def test_binarizer_fit(self):
        """...Test binarizer fit
        """
        n_cuts = 3
        enc = OneHotEncoder(sparse=True)
        expected_binarization = enc.fit_transform(
            self.default_expected_intervals)

        binarizer = FeaturesBinarizer(method='quantile', n_cuts=n_cuts,
                                      detect_column_type="auto",
                                      remove_first=False)
        # for pandas dataframe
        binarizer.fit(self.df_features)
        binarized_df = binarizer.transform(self.df_features)
        self.assertEqual(binarized_df.__class__, csr.csr_matrix)

        np.testing.assert_array_equal(expected_binarization.toarray(),
                                      binarized_df.toarray())
        # for numpy array
        binarizer.fit(self.features)
        binarized_array = binarizer.transform(self.features)
        self.assertEqual(binarized_array.__class__, csr.csr_matrix)

        np.testing.assert_array_equal(expected_binarization.toarray(),
                                      binarized_array.toarray())

        # test fit_transform
        binarized_array = binarizer.fit_transform(self.features)
        self.assertEqual(binarized_array.__class__, csr.csr_matrix)

        np.testing.assert_array_equal(expected_binarization.toarray(),
                                      binarized_array.toarray()) 
開發者ID:X-DataInitiative,項目名稱:tick,代碼行數:34,代碼來源:features_binarizer_test.py

示例10: vectorize_dic

# 需要導入模塊: from scipy.sparse import csr [as 別名]
# 或者: from scipy.sparse.csr import csr_matrix [as 別名]
def vectorize_dic(dic, ix=None, p=None):
    if(ix == None):
        d = count(0)
        ix = defaultdict(lambda:next(d))
        
    n = len(list(dic.values())[0])
    g = len(list(dic.keys()))
    nz = n*g
    
    col_ix = np.empty(nz, dtype=int)
    
    i = 0
    for k, lis in dic.items():
        col_ix[i::g] = [ix[str(k)+str(el)] for el in lis]
        i += 1
    
    row_ix = np.repeat(np.arange(n), g)
    data = np.ones(nz);print('data.shape ', data.shape)

    if(p == None):
        p = len(ix)
        
    ixx = np.where(col_ix < p)
    
    return csr.csr_matrix(
        (data[ixx], (row_ix[ixx], col_ix[ixx])), shape=(n, p)), ix 
開發者ID:wyl6,項目名稱:Recommender-Systems-Samples,代碼行數:28,代碼來源:util.py

示例11: load_sparse_csr

# 需要導入模塊: from scipy.sparse import csr [as 別名]
# 或者: from scipy.sparse.csr import csr_matrix [as 別名]
def load_sparse_csr(d, key="X"):
    from scipy.sparse.csr import csr_matrix

    key_csr = f"{key}_csr"
    d[key] = csr_matrix(
        (d[f"{key_csr}_data"], d[f"{key_csr}_indices"], d[f"{key_csr}_indptr"]),
        shape=d[f"{key_csr}_shape"],
    )
    del_sparse_matrix_keys(d, key_csr)
    return d 
開發者ID:theislab,項目名稱:anndata,代碼行數:12,代碼來源:read.py

示例12: test_simple

# 需要導入模塊: from scipy.sparse import csr [as 別名]
# 或者: from scipy.sparse.csr import csr_matrix [as 別名]
def test_simple(self):
        y_true = csr.csr_matrix([[1, 0], [1, 0], [1, 0], [1, 1]])
        y_pred = csr.csr_matrix([[1, 0], [0, 1], [1, 1], [0, 1]])

        np.testing.assert_array_equal(f1_per_sample(y_true, y_pred), [1.,0., 2/3, 2/3]) 
開發者ID:quadflor,項目名稱:Quadflor,代碼行數:7,代碼來源:test_f1_per_sample.py

示例13: test_inner_kneighbors

# 需要導入模塊: from scipy.sparse import csr [as 別名]
# 或者: from scipy.sparse.csr import csr_matrix [as 別名]
def test_inner_kneighbors(self):
        X = csr.csr_matrix([[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3]])
        y = csr.csr_matrix([[0.4, 0.4, 0.4], [2.4, 2.4, 2.4], [3.1, 3.1, 3.1], [1.1, 1.1, 1.1]])
        nearest_neighbors = NearestNeighbors()
        nearest_neighbors.fit(X)
        neighbors = BatchKNeighbors(nearest_neighbors)

        kneighbors = neighbors._batch_kneighbors(y, n_neighbors=1, batchsize=1)
        np.testing.assert_array_equal(kneighbors, np.matrix([[0], [2], [3], [1]]))
        kneighbors = neighbors._batch_kneighbors(y, n_neighbors=1, batchsize=3)
        np.testing.assert_array_equal(kneighbors, np.matrix([[0], [2], [3], [1]]))
        kneighbors = neighbors._batch_kneighbors(y, n_neighbors=1, batchsize=10)
        np.testing.assert_array_equal(kneighbors, np.matrix([[0], [2], [3], [1]])) 
開發者ID:quadflor,項目名稱:Quadflor,代碼行數:15,代碼來源:test_kneighbors.py

示例14: test_inner_kneighbors_more_neighbors

# 需要導入模塊: from scipy.sparse import csr [as 別名]
# 或者: from scipy.sparse.csr import csr_matrix [as 別名]
def test_inner_kneighbors_more_neighbors(self):
        X = csr.csr_matrix([[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3]])
        y = csr.csr_matrix([[0.4, 0.4, 0.4], [2.4, 2.4, 2.4], [3.1, 3.1, 3.1], [1.1, 1.1, 1.1]])
        nearest_neighbors = NearestNeighbors()
        nearest_neighbors.fit(X)
        neighbors = BatchKNeighbors(nearest_neighbors)

        kneighbors = neighbors._batch_kneighbors(y, n_neighbors=2, batchsize=1)
        np.testing.assert_array_equal(kneighbors, np.matrix([[0, 1], [2,3], [3, 2], [1,2]]))

        kneighbors = neighbors._batch_kneighbors(y, n_neighbors=2, batchsize=3)
        np.testing.assert_array_equal(kneighbors, np.matrix([[0, 1], [2,3], [3, 2], [1,2]])) 
開發者ID:quadflor,項目名稱:Quadflor,代碼行數:14,代碼來源:test_kneighbors.py

示例15: test_BRKnna_no_labels_take_closest

# 需要導入模塊: from scipy.sparse import csr [as 別名]
# 或者: from scipy.sparse.csr import csr_matrix [as 別名]
def test_BRKnna_no_labels_take_closest(self):
        data = csr.csr_matrix([[0, 1], [1, 1], [1, 1.1], [0, 1]])
        train_ids = [['lid0', 'lid1'], ['lid2', 'lid3'], ['lid2', 'lid3'], ['lid0', 'lid5']]
        mlb = MultiLabelBinarizer(sparse_output=True)
        y = mlb.fit_transform(train_ids)
        knn = BRKNeighborsClassifier(n_neighbors=2, threshold=0.6, mode='a')
        knn.fit(data, y)

        pred = knn.predict(csr.csr_matrix([[0, 1]])).todense()
        print(pred)
        np.testing.assert_array_equal([[1, 0, 0, 0, 0]], pred) 
開發者ID:quadflor,項目名稱:Quadflor,代碼行數:13,代碼來源:test_BRKNN.py


注:本文中的scipy.sparse.csr.csr_matrix方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。