本文整理汇总了Python中numpy.compress方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.compress方法的具体用法?Python numpy.compress怎么用?Python numpy.compress使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.compress方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: dens_elec_vec
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import compress [as 别名]
def dens_elec_vec(sv, crds, dm):
""" Compute the electronic density using vectorized oprators """
from pyscf.nao.m_rsphar_vec import rsphar_vec as rsphar_vec_python
assert crds.ndim==2
assert crds.shape[-1]==3
nc = crds.shape[0]
lmax = sv.ao_log.jmx
for ia,[ra,sp] in enumerate(zip(sv.atom2coord,sv.atom2sp)):
lngs = cdist(crds, sv.atom2coord[ia:ia+1,:])
bmask = lngs<sv.ao_log.sp2rcut[sp]
crds_selec = compress(bmask[:,0], crds, axis=0)
t1 = timer()
rsh1 = rsphar_vec_python(crds_selec, lmax)
t2 = timer(); print(t2-t1); t1 = timer()
return 0
示例2: test_numpy_compress
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import compress [as 别名]
def test_numpy_compress(self):
cond = [True, False, True, False, False]
s = Series([1, -1, 5, 8, 7],
index=list('abcde'), name='foo')
expected = Series(s.values.compress(cond),
index=list('ac'), name='foo')
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
tm.assert_series_equal(np.compress(cond, s), expected)
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
msg = "the 'axis' parameter is not supported"
with pytest.raises(ValueError, match=msg):
np.compress(cond, s, axis=1)
msg = "the 'out' parameter is not supported"
with pytest.raises(ValueError, match=msg):
np.compress(cond, s, out=s)
示例3: _min_or_max_axis
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import compress [as 别名]
def _min_or_max_axis(self, axis, min_or_max):
N = self.shape[axis]
if N == 0:
raise ValueError("zero-size array to reduction operation")
M = self.shape[1 - axis]
mat = self.tocsc() if axis == 0 else self.tocsr()
mat.sum_duplicates()
major_index, value = mat._minor_reduce(min_or_max)
not_full = np.diff(mat.indptr)[major_index] < N
value[not_full] = min_or_max(value[not_full], 0)
mask = value != 0
major_index = np.compress(mask, major_index)
value = np.compress(mask, value)
from . import coo_matrix
if axis == 0:
return coo_matrix((value, (np.zeros(len(value)), major_index)),
dtype=self.dtype, shape=(1, M))
else:
return coo_matrix((value, (major_index, np.zeros(len(value)))),
dtype=self.dtype, shape=(M, 1))
示例4: _min_or_max_axis
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import compress [as 别名]
def _min_or_max_axis(X, axis, min_or_max):
N = X.shape[axis]
if N == 0:
raise ValueError("zero-size array to reduction operation")
M = X.shape[1 - axis]
mat = X.tocsc() if axis == 0 else X.tocsr()
mat.sum_duplicates()
major_index, value = _minor_reduce(mat, min_or_max)
not_full = np.diff(mat.indptr)[major_index] < N
value[not_full] = min_or_max(value[not_full], 0)
mask = value != 0
major_index = np.compress(mask, major_index)
value = np.compress(mask, value)
if axis == 0:
res = sp.coo_matrix((value, (np.zeros(len(value)), major_index)),
dtype=X.dtype, shape=(1, M))
else:
res = sp.coo_matrix((value, (major_index, np.zeros(len(value)))),
dtype=X.dtype, shape=(M, 1))
return res.A.ravel()
示例5: test_small_large
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import compress [as 别名]
def test_small_large(self):
# test the small and large code paths, current cutoff 400 elements
for s in [5, 20, 51, 200, 1000]:
d = np.random.randn(4, s)
# Randomly set some elements to NaN:
w = np.random.randint(0, d.size, size=d.size // 5)
d.ravel()[w] = np.nan
d[:,0] = 1. # ensure at least one good value
# use normal median without nans to compare
tgt = []
for x in d:
nonan = np.compress(~np.isnan(x), x)
tgt.append(np.median(nonan, overwrite_input=True))
assert_array_equal(np.nanmedian(d, axis=-1), tgt)
示例6: test_compress
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import compress [as 别名]
def test_compress(self):
arr = [[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]]
tgt = [[5, 6, 7, 8, 9]]
out = np.compress([0, 1], arr, axis=0)
assert_equal(out, tgt)
示例7: _get_default_locs
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import compress [as 别名]
def _get_default_locs(self, vmin, vmax):
"Returns the default locations of ticks."
if self.plot_obj.date_axis_info is None:
self.plot_obj.date_axis_info = self.finder(vmin, vmax, self.freq)
locator = self.plot_obj.date_axis_info
if self.isminor:
return np.compress(locator['min'], locator['val'])
return np.compress(locator['maj'], locator['val'])
示例8: test_compress
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import compress [as 别名]
def test_compress(self):
cond = [True, False, True, False, False]
s = Series([1, -1, 5, 8, 7],
index=list('abcde'), name='foo')
expected = Series(s.values.compress(cond),
index=list('ac'), name='foo')
with tm.assert_produces_warning(FutureWarning):
result = s.compress(cond)
tm.assert_series_equal(result, expected)
示例9: _get_single_element
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import compress [as 别名]
def _get_single_element(self, row, col):
M, N = self.shape
if (row < 0):
row += M
if (col < 0):
col += N
if not (0 <= row < M) or not (0 <= col < N):
raise IndexError("index out of bounds: 0<=%d<%d, 0<=%d<%d" %
(row, M, col, N))
major_index, minor_index = self._swap((row, col))
start = self.indptr[major_index]
end = self.indptr[major_index + 1]
if self.has_sorted_indices:
# Copies may be made, if dtypes of indices are not identical
minor_index = self.indices.dtype.type(minor_index)
minor_indices = self.indices[start:end]
insert_pos_left = np.searchsorted(
minor_indices, minor_index, side='left')
insert_pos_right = insert_pos_left + np.searchsorted(
minor_indices[insert_pos_left:], minor_index, side='right')
return self.data[start + insert_pos_left:
start + insert_pos_right].sum(dtype=self.dtype)
else:
return np.compress(minor_index == self.indices[start:end],
self.data[start:end]).sum(dtype=self.dtype)
示例10: compress
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import compress [as 别名]
def compress(condition, x, axis=None):
"""
Return selected slices of an array along given axis.
It returns the input tensor, but with selected slices along a given axis
retained. If no axis is provided, the tensor is flattened.
Corresponds to numpy.compress
.. versionadded:: 0.7
Parameters
----------
x
Input data, tensor variable.
condition
1 dimensional array of non-zero and zero values
corresponding to indices of slices along a selected axis.
Returns
-------
object
`x` with selected slices.
"""
indices = theano.tensor.basic.flatnonzero(condition)
return x.take(indices, axis=axis)
示例11: test_op
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import compress [as 别名]
def test_op(self):
for axis, cond, shape in zip(self.axis_list, self.cond_list,
self.shape_list):
cond_var = theano.tensor.ivector()
data = numpy.random.random(size=shape).astype(theano.config.floatX)
data_var = theano.tensor.matrix()
f = theano.function([cond_var, data_var],
self.op(cond_var, data_var, axis=axis))
expected = numpy.compress(cond, data, axis=axis)
tested = f(cond, data)
assert tested.shape == expected.shape
assert numpy.allclose(tested, expected)
示例12: test_compress
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import compress [as 别名]
def test_compress(self):
cond = [True, False, True, False, False]
s = Series([1, -1, 5, 8, 7],
index=list('abcde'), name='foo')
expected = Series(s.values.compress(cond),
index=list('ac'), name='foo')
tm.assert_series_equal(s.compress(cond), expected)
示例13: test_numpy_compress
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import compress [as 别名]
def test_numpy_compress(self):
cond = [True, False, True, False, False]
s = Series([1, -1, 5, 8, 7],
index=list('abcde'), name='foo')
expected = Series(s.values.compress(cond),
index=list('ac'), name='foo')
tm.assert_series_equal(np.compress(cond, s), expected)
msg = "the 'axis' parameter is not supported"
tm.assert_raises_regex(ValueError, msg, np.compress,
cond, s, axis=1)
msg = "the 'out' parameter is not supported"
tm.assert_raises_regex(ValueError, msg, np.compress,
cond, s, out=s)
示例14: test_compress
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import compress [as 别名]
def test_compress(self):
if legacy_opset_pre_ver(9):
raise unittest.SkipTest(
"ONNX version {} doesn't support Compress.".format(
defs.onnx_opset_version()))
axis = 1
node_def = helper.make_node("Compress",
inputs=['X', 'condition'],
outputs=['Y'],
axis=axis)
x = self._get_rnd_float32(shape=[5, 5, 5])
cond = np.array([1, 0, 1])
output = run_node(node_def, inputs=[x, cond])
np.testing.assert_almost_equal(output['Y'], np.compress(cond, x, axis=axis))
示例15: test_compress
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import compress [as 别名]
def test_compress(self):
if legacy_opset_pre_ver(9):
raise unittest.SkipTest(
"ONNX version {} doesn't support Compress.".format(
defs.onnx_opset_version()))
axis = 1
node_def = helper.make_node("Compress",
inputs=['X', 'condition'],
outputs=['Y'],
axis=axis)
graph_def = helper.make_graph(
[node_def],
name="test_unknown_shape",
inputs=[
helper.make_tensor_value_info("X", TensorProto.FLOAT,
[None, None, None]),
helper.make_tensor_value_info("condition", TensorProto.BOOL, [None])
],
outputs=[
helper.make_tensor_value_info("Y", TensorProto.FLOAT,
[None, None, None])
])
x = self._get_rnd_float32(shape=[5, 5, 5])
cond = np.array([1, 0, 1])
tf_rep = onnx_graph_to_tensorflow_rep(graph_def)
output = tf_rep.run({"X": x, "condition": cond})
np.testing.assert_almost_equal(output['Y'], np.compress(cond, x, axis=axis))