本文整理汇总了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
示例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
示例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)
示例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
示例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())
示例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}
示例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)
示例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)
示例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())
示例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
示例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
示例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])
示例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]]))
示例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]]))
示例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)