本文整理汇总了Python中scipy.sparse.find方法的典型用法代码示例。如果您正苦于以下问题:Python sparse.find方法的具体用法?Python sparse.find怎么用?Python sparse.find使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.sparse
的用法示例。
在下文中一共展示了sparse.find方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: jacobian
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import find [as 别名]
def jacobian(self, params, into=None):
params = flattest(params)
n = len(params)
ii = np.arange(n)
(rs,cs,zs) = ([],[],[])
for ((mn,mx), f) in self.pieces_with_default:
if len(ii) == 0: break
k = np.where((params >= mn) & (params <= mx))[0]
if len(k) == 0: continue
kk = ii[k]
j = f.jacobian(params[k])
if j.shape[0] == 1 and j.shape[1] > 1: j = repmat(j, j.shape[1], 1)
(rj,cj,vj) = sps.find(j)
rs.append(kk[rj])
cs.append(kk[cj])
zs.append(vj)
ii = np.delete(ii, k)
params = np.delete(params, k)
(rs,cs,zs) = [np.concatenate(us) if len(us) > 0 else [] for us in (rs,cs,zs)]
dz = sps.csr_matrix((zs, (rs,cs)), shape=(n,n))
return safe_into(into, dz)
示例2: try_until
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import find [as 别名]
def try_until(*args, **kw):
'''
try_until(f1, f2, f3...) attempts to return f1(); if this raises an Exception during its
evaluation, however, it attempts to return f2(); etc. If none of the functions succeed, then
an exception is raised.
The following optional arguments may be given:
* check (default: None) may specify a function of one argument that must return True when the
passed value is an acceptable return value; for example, an option of
`check=lambda x: x is not None` would indicate that a function that returns None should not
be considered to have succeeded.
'''
if 'check' in kw: check = kw.pop('check')
else: check = None
if len(kw) > 0: raise ValueError('unrecognized options given to try_until')
for f in args:
if not hasattr(f, '__call__'):
raise ValueError('function given to try_until is not callable')
try:
rval = f()
if check is None or check(rval): return rval
except Exception: raise
raise ValueError('try_until failed to find a successful function return')
示例3: fit
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import find [as 别名]
def fit(self, X, Y):
n_samples = X.shape[0]
F = self.firing_threshold
decay = self.decay
coef_ = np.zeros(shape=(X.shape[1]), dtype=np.float64)
fired_ = np.zeros(shape=(X.shape[1]), dtype=np.bool_)
_, I, V = sp.find(Y)
coef_[I] += np.divide(V[I], X.shape[0])
markers = deque(I)
while markers:
i = markers.popleft()
if coef_[i] >= F and not fired[i]:
#fire
for j in self.hierarchy.neighbors(i):
if self.use_weights:
coef_[j] += coef[i] * decay * hierarchy[i][j]['weight']
else:
coef_[j] += coef[i] * decay
if coef_[j] >= F:
coef_[j] = F
markers.append(n)
self.coef_ = coef_
return self
示例4: read_directed_graph
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import find [as 别名]
def read_directed_graph(X, weighted):
rows = X[:, 0]
cols = X[:, 1]
data = X[:, 2]
# assume id starts from 0
n = int(np.amax(X[:, 0:2])) + 1
# the weights of redundant edges will be summed (by default in csr_matrix)
A = csr_matrix((data, (rows, cols)), shape=(n, n))
if not weighted:
# no redundant edges are allowed for unweighted graphs
I, J, K = find(A)
A = csr_matrix((np.ones(len(K), (I, J), shape=A.shape)))
return A
示例5: read_undirected_graph
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import find [as 别名]
def read_undirected_graph(X, weighted):
rows = X[:, 0]
cols = X[:, 1]
data = X[:, 2]
# assume id starts from 0
n = int(np.amax(X[:, 0:2])) + 1
# the weights of redundant edges will be summed (by default in csr_matrix)
_A = csr_matrix((data, (rows, cols)), shape=(n, n))
# this is under the assumption that src_id <= dst_id for all edges (see line 80 in this code)
A = _A + _A.T
if not weighted:
# no redundant edges are allowed for unweighted graphs
I, J, K = find(A)
A = csr_matrix((np.ones(len(K)), (I, J)), shape=A.shape)
return A
示例6: _update_chunk
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import find [as 别名]
def _update_chunk(blocks, x, params):
n_f, lambda_, axis = params
r_chunk = Array._merge_blocks(blocks)
if axis == 1:
r_chunk = r_chunk.transpose()
n = r_chunk.shape[0]
y = np.zeros((n, n_f), dtype=np.float32)
n_c = np.array(
[len(sparse.find(r_chunk[i])[0]) for i in
range(0, r_chunk.shape[0])])
for element in range(0, n):
indices = sparse.find(r_chunk[element])[1]
x_xt = x[indices].T.dot(x[indices])
a_i = x_xt + lambda_ * n_c[element] * np.eye(n_f)
v_i = x[indices].T.dot(r_chunk[element, indices].toarray().T)
# TODO: decide if atol should be changed when default is changed
y[element] = sparse.linalg.cg(a_i, v_i, atol='legacy')[0].reshape(-1)
return y
示例7: convert_to_adjacency_matrix
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import find [as 别名]
def convert_to_adjacency_matrix(matrix):
"""
Converts transition matrix into adjacency matrix
:param matrix: The matrix to be converted
:returns: adjacency matrix
"""
for i in range(matrix.shape[0]):
if isspmatrix(matrix):
col = find(matrix[:,i])[2]
else:
col = matrix[:,i].T.tolist()[0]
coeff = max( Fraction(c).limit_denominator().denominator for c in col )
matrix[:,i] *= coeff
return matrix
示例8: build_input_label_data
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import find [as 别名]
def build_input_label_data(labels, class_order):
from sklearn.preprocessing import MultiLabelBinarizer
from itertools import chain
bml = MultiLabelBinarizer(classes=class_order, sparse_output=True)
indexes = sp.find(bml.fit_transform(labels))
y = []
for i in range(len(labels)):
y.append([])
for i,j in zip(indexes[0], indexes[1]):
y[i].append(j)
return y
# padding operation
# =========================================================
示例9: tmpdir
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import find [as 别名]
def tmpdir(prefix='npythy_tempdir_', delete=True):
'''
tmpdir() creates a temporary directory and yields its path. At python exit, the directory and
all of its contents are recursively deleted (so long as the the normal python exit process is
allowed to call the atexit handlers).
tmpdir(prefix) uses the given prefix in the tempfile.mkdtemp() call.
The option delete may be set to False to specify that the tempdir should not be deleted on exit.
'''
path = tempfile.mkdtemp(prefix=prefix)
if not os.path.isdir(path): raise ValueError('Could not find or create temp directory')
if delete: atexit.register(shutil.rmtree, path)
return path
示例10: neighbors
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import find [as 别名]
def neighbors(fringe, A, row=True):
# find all 1-hop neighbors of nodes in fringe from A
res = set()
for node in fringe:
if row:
_, nei, _ = ssp.find(A[node, :])
else:
nei, _, _ = ssp.find(A[:, node])
nei = set(nei)
res = res.union(nei)
return res
示例11: _get_neighbors
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import find [as 别名]
def _get_neighbors(adj, nodes):
"""Takes a set of nodes and a graph adjacency matrix and returns a set of neighbors."""
sp_nodes = _sp_row_vec_from_idx_list(list(nodes), adj.shape[1])
sp_neighbors = sp_nodes.dot(adj)
neighbors = set(sp.find(sp_neighbors)[1]) # convert to set of indices
return neighbors
示例12: score_alignment_matrix
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import find [as 别名]
def score_alignment_matrix(alignment_matrix, topk = None, topk_score_weighted = False, true_alignments = None):
n_nodes = alignment_matrix.shape[0]
correct_nodes = []
if topk is None:
row_sums = alignment_matrix.sum(axis=1)
row_sums[row_sums == 0] = 10e-6 #shouldn't affect much since dividing 0 by anything is 0
alignment_matrix = alignment_matrix / row_sums[:, np.newaxis] #normalize
alignment_score = score(alignment_matrix, true_alignments = true_alignments)
else:
alignment_score = 0
if not sp.issparse(alignment_matrix):
sorted_indices = np.argsort(alignment_matrix)
for node_index in range(n_nodes):
target_alignment = node_index #default: assume identity mapping, and the node should be aligned to itself
if true_alignments is not None: #if we have true alignments (which we require), use those for each node
target_alignment = int(true_alignments[node_index])
if sp.issparse(alignment_matrix):
row, possible_alignments, possible_values = sp.find(alignment_matrix[node_index])
node_sorted_indices = possible_alignments[possible_values.argsort()]
else:
node_sorted_indices = sorted_indices[node_index]
if target_alignment in node_sorted_indices[-topk:]:
if topk_score_weighted:
alignment_score += 1.0 / (n_nodes - np.argwhere(sorted_indices[node_index] == target_alignment)[0])
else:
alignment_score += 1
correct_nodes.append(node_index)
alignment_score /= float(n_nodes)
return alignment_score, set(correct_nodes)
示例13: _pack_data
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import find [as 别名]
def _pack_data(self, data_dict):
data = GPNNData(
data_dict["graph"],
train_idx=self.train_idx,
num_edgetype=self.num_edgetype,
num_cluster=self._num_cluster,
decomp_method=self._decomp_method,
seed=self.seed)
data.get_graph_partition()
data.get_prop_index(data.cluster_graphs, data.cut_graph)
self._param["num_node_cut"] = data.num_node_cut
self._param["cluster_size"] = data.cluster_size
logger.info("cluster_size = {}".format(self._param["cluster_size"]))
data.train_idx = np.array([data._pos_map[xx] for xx in self.train_idx])
data.val_idx = np.array([data._pos_map[xx] for xx in self.val_idx])
data.test_idx = np.array([data._pos_map[xx] for xx in self.test_idx])
row_idx, col_idx, values = sparse.find(sparse.csr_matrix(self._node_feat))
# construc label
feat_idx = np.array([data._pos_map[xx] for xx in xrange(self._num_nodes)])
data.node_gt_label = np.zeros(self._num_nodes, dtype=np.int32)
data.node_gt_label[feat_idx] = data_dict["all_label"]
row_idx = np.array([data._pos_map[xx] for xx in row_idx])
data.node_feat_shape = np.array(
[self._num_nodes, self._feat_dim], dtype=np.int64)
data.node_feat_indices = np.stack(
[row_idx.astype(np.int64),
col_idx.astype(np.int64)], axis=1)
data.node_feat_values = values.astype(np.float32)
return data
示例14: balance_dataset
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import find [as 别名]
def balance_dataset(x, y):
number_of_elements = y.shape[0]
nnz = set(find(y)[0])
zero = set(range(number_of_elements)).difference(nnz)
max_samples = min(len(zero), len(nnz))
nnz_indices = random.sample(nnz, max_samples)
zero_indeces = random.sample(zero, max_samples)
indeces = nnz_indices + zero_indeces
return x[indeces, :], y[indeces, :]
示例15: pca
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import find [as 别名]
def pca(self, nterms=6):
"""Returns a new `.DesignMatrix` with a smaller number of regressors.
This method will use Principal Components Analysis (PCA) to reduce
the number of columns in the matrix.
Parameters
----------
nterms : int
Number of columns in the new matrix.
Returns
-------
`.DesignMatrix`
A new design matrix with PCA applied.
"""
# nterms cannot be langer than the number of columns in the matrix
if nterms > self.shape[1]:
nterms = self.shape[1]
# We use `fbpca.pca` instead of `np.linalg.svd` because it is faster.
# Note that fbpca is randomized, and has n_iter=2 as default,
# we find this to be too few, and that n_iter=10 is still fast but
# produces more stable results.
from fbpca import pca # local import because not used elsewhere
new_values, _, _ = pca(self.values, nterms, n_iter=10)
return DesignMatrix(new_values, name=self.name)