本文整理汇总了Python中numpy.isin方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.isin方法的具体用法?Python numpy.isin怎么用?Python numpy.isin使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.isin方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: dataframe_select
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isin [as 别名]
def dataframe_select(df, *cols, **filters):
'''
dataframe_select(df, k1=v1, k2=v2...) yields df after selecting all the columns in which the
given keys (k1, k2, etc.) have been selected such that the associated columns in the dataframe
contain only the rows whose cells match the given values.
dataframe_select(df, col1, col2...) selects the given columns.
dataframe_select(df, col1, col2..., k1=v1, k2=v2...) selects both.
If a value is a tuple/list of 2 elements, then it is considered a range where cells must fall
between the values. If value is a tuple/list of more than 2 elements or is a set of any length
then it is a list of values, any one of which can match the cell.
'''
ii = np.ones(len(df), dtype='bool')
for (k,v) in six.iteritems(filters):
vals = df[k].values
if pimms.is_set(v): jj = np.isin(vals, list(v))
elif pimms.is_vector(v) and len(v) == 2: jj = (v[0] <= vals) & (vals < v[1])
elif pimms.is_vector(v): jj = np.isin(vals, list(v))
else: jj = (vals == v)
ii = np.logical_and(ii, jj)
if len(ii) != np.sum(ii): df = df.loc[ii]
if len(cols) > 0: df = df[list(cols)]
return df
示例2: _emg_activation_activations
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isin [as 别名]
def _emg_activation_activations(activity, duration_min=0.05):
activations = events_find(activity, threshold=0.5, threshold_keep="above", duration_min=duration_min)
activations["offset"] = activations["onset"] + activations["duration"]
baseline = events_find(activity == 0, threshold=0.5, threshold_keep="above", duration_min=duration_min)
baseline["offset"] = baseline["onset"] + baseline["duration"]
# Cross-comparison
valid = np.isin(activations["onset"], baseline["offset"])
onsets = activations["onset"][valid]
offsets = activations["offset"][valid]
new_activity = np.array([])
for x, y in zip(onsets, offsets):
activated = np.arange(x, y)
new_activity = np.append(new_activity, activated)
# Prepare Output.
info = {"EMG_Onsets": onsets, "EMG_Offsets": offsets, "EMG_Activity": new_activity}
return info
示例3: find_all_elements_with_node
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isin [as 别名]
def find_all_elements_with_node(self, node_nr):
""" Finds all elements that have a given node
Parameters
-----------------
node_nr: int
number of node
Returns
---------------
elm_nr: np.ndarray
array with indices of element numbers
"""
elm_with_node = np.any(
np.isin(self.node_number_list, node_nr),
axis=1)
return self.elm_number[elm_with_node]
示例4: nodes_with_tag
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isin [as 别名]
def nodes_with_tag(self, tags):
''' Gets all nodes indexes that are part of at least one element with the given
tags
Parameters
-----------
tags: list
Integer tags to search
Returns
-------------
nodes: ndarray of integer
Indexes of nodes with given tag
'''
nodes = np.unique(self[np.isin(self.tag1, tags)].reshape(-1))
nodes = nodes[nodes > 0]
return nodes
示例5: mean_field_norm
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isin [as 别名]
def mean_field_norm(self):
''' Calculates V*w/sum(w)
Where V is the norm of the field, and w is the volume or area of the mesh where
the field is defined. This can be used as a focality metric. It should give out
small values when the field is focal and
Returns
----------
eff_area: float
Area or volume of mesh, weighted by the field
'''
self._test_msh()
if np.all(np.isin([2, 4], self.mesh.elm.elm_type)):
warnings.warn('Calculating effective volume/area of fields in meshes with'
' triangles and tetrahedra can give misleading results')
norm = self._norm()
weights = self._weights()
return np.sum(norm * weights) / np.sum(weights)
示例6: test_isin
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isin [as 别名]
def test_isin(self):
# the tests for in1d cover most of isin's behavior
# if in1d is removed, would need to change those tests to test
# isin instead.
a = np.arange(24).reshape([2, 3, 4])
mask = np.zeros([2, 3, 4])
mask[1, 2, 0] = 1
a = array(a, mask=mask)
b = array(data=[0, 10, 20, 30, 1, 3, 11, 22, 33],
mask=[0, 1, 0, 1, 0, 1, 0, 1, 0])
ec = zeros((2, 3, 4), dtype=bool)
ec[0, 0, 0] = True
ec[0, 0, 1] = True
ec[0, 2, 3] = True
c = isin(a, b)
assert_(isinstance(c, MaskedArray))
assert_array_equal(c, ec)
#compare results of np.isin to ma.isin
d = np.isin(a, b[~b.mask]) & ~a.mask
assert_array_equal(c, d)
示例7: _sample_intermittent
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isin [as 别名]
def _sample_intermittent(self, group):
# we need to collect also the residence
# function
# the residence function (1 if in the reference group, 0 otherwise)
mask = np.isin(self.reference, group)
# append the residence function to its timeseries
self.maskseries.append(list(mask))
if self.observable is not None:
# this copies a vector of zeros with the correct shape
sampled = self.reference_obs.copy()
obs = self.observable.compute(group)
sampled[np.where(mask)] = obs
self.timeseries.append(list(sampled.flatten()))
else:
self.timeseries = self.maskseries
if self.shape is None:
self.shape = (1, )
sampled = mask
return sampled
示例8: test_sampler
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isin [as 别名]
def test_sampler():
torch.manual_seed(12345)
edge_index = erdos_renyi_graph(num_nodes=10, edge_prob=0.5)
E = edge_index.size(1)
loader = NeighborSampler(edge_index, sizes=[2, 4], batch_size=2)
assert loader.__repr__() == 'NeighborSampler(sizes=[2, 4])'
assert len(loader) == 5
for batch_size, n_id, adjs in loader:
assert batch_size == 2
assert all(np.isin(n_id, torch.arange(10)).tolist())
assert n_id.unique().size(0) == n_id.size(0)
for (edge_index, e_id, size) in adjs:
assert int(edge_index[0].max() + 1) <= size[0]
assert int(edge_index[1].max() + 1) <= size[1]
assert all(np.isin(e_id, torch.arange(E)).tolist())
assert e_id.unique().size(0) == e_id.size(0)
assert size[0] >= size[1]
out = loader.sample([1, 2])
assert len(out) == 3
示例9: create_mask_from_class_map
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isin [as 别名]
def create_mask_from_class_map(class_map_path, out_path, classes_of_interest, buffer_size=0, out_resolution=None):
"""Creates a mask from a classification mask: 1 for each pixel containing one of classes_of_interest, otherwise 0"""
# TODO: pull this out of the above function
class_image = gdal.Open(class_map_path)
class_array = class_image.GetVirtualMemArray()
mask_array = np.isin(class_array, classes_of_interest)
out_mask = create_matching_dataset(class_image, out_path, datatype=gdal.GDT_Byte)
out_array = out_mask.GetVirtualMemArray(eAccess=gdal.GA_Update)
np.copyto(out_array, mask_array)
class_array = None
class_image = None
out_array = None
out_mask = None
if out_resolution:
resample_image_in_place(out_path, out_resolution)
if buffer_size:
buffer_mask_in_place(out_path, buffer_size)
return out_path
示例10: create_mask_from_fmask
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isin [as 别名]
def create_mask_from_fmask(in_l1_dir, out_path):
log = logging.getLogger(__name__)
log.info("Creating fmask for {}".format(in_l1_dir))
with TemporaryDirectory() as td:
temp_fmask_path = os.path.join(td, "fmask.tif")
apply_fmask(in_l1_dir, temp_fmask_path)
fmask_image = gdal.Open(temp_fmask_path)
fmask_array = fmask_image.GetVirtualMemArray()
out_image = create_matching_dataset(fmask_image, out_path, datatype=gdal.GDT_Byte)
out_array = out_image.GetVirtualMemArray(eAccess=gdal.GA_Update)
log.info("fmask created, converting to binary cloud/shadow mask")
out_array[:,:] = np.isin(fmask_array, (2, 3, 4), invert=True)
out_array = None
out_image = None
fmask_array = None
fmask_image = None
resample_image_in_place(out_path, 10)
示例11: life_rule
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isin [as 别名]
def life_rule(X: np.ndarray, rulestring: str) -> np.ndarray:
"""A generalized life rule that accepts a rulestring in B/S notation
Rulestrings are commonly expressed in the B/S notation where B (birth) is a
list of all numbers of live neighbors that cause a dead cell to come alive,
and S (survival) is a list of all the numbers of live neighbors that cause
a live cell to remain alive.
Parameters
----------
X : np.ndarray
The input board matrix
rulestring : str
The rulestring in B/S notation
Returns
-------
np.ndarray
Updated board after applying the rule
"""
birth_req, survival_req = _parse_rulestring(rulestring)
neighbors = _count_neighbors(X)
birth_rule = (X == 0) & (np.isin(neighbors, birth_req))
survival_rule = (X == 1) & (np.isin(neighbors, survival_req))
return birth_rule | survival_rule
示例12: GetPolys2D
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isin [as 别名]
def GetPolys2D(self):
"""Returns the polys as a 2D VTKArray instance.
Returns
-------
polys : 2D ndarray, shape = (n_points, n)
PolyData polys.
Raises
------
ValueError
If PolyData has different poly types.
"""
v = self.GetPolys()
if v is None:
return v
ct = self.cell_types
if np.isin([VTK_QUAD, VTK_TRIANGLE], ct).all() or VTK_POLYGON in ct:
raise ValueError('PolyData contains different poly types')
return v.reshape(-1, v[0] + 1)[:, 1:]
示例13: set_node_transfers
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isin [as 别名]
def set_node_transfers(self):
for index, mapping in enumerate(self.tmap):
for pair, node in mapping.items():
i, j = pair
comm = int(node.comm)
comm_elev = node.elev
neighbors = Grid._select_surround_ravel(self, comm, self.dem.shape)
ser = pd.DataFrame(np.column_stack([neighbors, self.dem.flat[neighbors],
self.ws[index].flat[neighbors]]))
ser = ser[ser[2].isin(list(pair))]
g = ser.groupby(2).idxmin()[1].apply(lambda x: ser.loc[x, 0])
fullix = self.drop.flat[g.values.astype(int)]
lv = self.dropmap.loc[fullix][0].values
nm = self.dropmap.loc[fullix][1].values
g = pd.DataFrame(np.column_stack([lv, nm]), index=g.index.values.astype(int),
columns=['level', 'name']).to_dict(orient='index')
# Children will always be in numeric order from left to right
lt, rt = g[j], g[i]
node.l.t = self.nodes[lt['level']][lt['name']]
node.r.t = self.nodes[rt['level']][rt['name']]
self.set_singleton_transfer(self.root)
示例14: set_cumulative_capacities
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isin [as 别名]
def set_cumulative_capacities(self, node):
if node.l:
self.set_cumulative_capacities(node.l)
if node.r:
self.set_cumulative_capacities(node.r)
if node.parent:
if node.name:
elevdiff = node.parent.elev - self.dem[self.ws[node.level] == node.name]
vol = abs(np.asscalar(elevdiff[elevdiff > 0].sum()) * self.x * self.y)
node.vol = vol
else:
leaves = []
self.enumerate_leaves(node, level=node.level, stack=leaves)
mask = np.isin(self.ws[node.level], leaves)
boundary = list(chain.from_iterable([self.b[node.level].setdefault(pair, [])
for pair in combinations(leaves, 2)]))
mask.flat[boundary] = True
elevdiff = node.parent.elev - self.dem[mask]
vol = abs(np.asscalar(elevdiff[elevdiff > 0].sum()) * self.x * self.y)
node.vol = vol
示例15: test_reward
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import isin [as 别名]
def test_reward(self, monkeypatch, physical_system, reference_generator, observed_state_idx, violated_state_idx):
observed_states = list(np.array(physical_system.state_names)[observed_state_idx])
rf = RewardFunction(observed_states)
rf.set_modules(physical_system, reference_generator)
monkeypatch.setattr(rf, "_reward", self.mock_standard_reward)
monkeypatch.setattr(rf, "_limit_violation_reward", self.mock_limit_violation_reward)
state = np.ones_like(physical_system.state_names, dtype=float) * 0.5
state[violated_state_idx] = 1.5
reward, done = rf.reward(state, None)
if np.any(np.isin(observed_state_idx, violated_state_idx)):
assert reward == -1
assert done
else:
assert reward == 1
assert not done
# Test negative limit violations
state[violated_state_idx] = -1.5
reward, done = rf.reward(state, None)
if np.any(np.isin(observed_state_idx, violated_state_idx)):
assert reward == -1
assert done
else:
assert reward == 1
assert not done