本文整理汇总了Python中numpy.ndim方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.ndim方法的具体用法?Python numpy.ndim怎么用?Python numpy.ndim使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.ndim方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __repr__
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndim [as 别名]
def __repr__(self):
"""x.__repr__() <==> repr(x)."""
if not hasattr(self, "__repr"):
params = self.params or {}
parsed_params = []
for k, v in params.items():
sk = str(k)
if np.ndim(v) != 0 and np.size(v) > MAX_VALUES_TO_REPR:
tv = type(v)
sv = f"<{tv.__module__}.{tv.__name__}>"
else:
sv = str(v)
parsed_params.append(f"{sk}={sv}")
str_params = ", ".join(parsed_params)
self.__repr = f"{self.name}({str_params})"
return self.__repr
示例2: augment
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndim [as 别名]
def augment(self, image, isArray=False):
if isArray: # if the input is a numpy array, convert back to PIL
image = Image.fromarray(image)
image = self.transform(image)
image = np.asarray(image).astype('f')
w, h = image.shape[0], image.shape[1]
if np.ndim(image) == 2:
ch = 1
else:
ch = np.shape(image)[2]
image = image.reshape(w, h, ch)
image = image.transpose((2, 0, 1))
if self.scaling == 'none':
return image
elif self.scaling == 'sigmoid':
return self._scaling_sigmoid(image)
elif self.scaling == 'tanh':
return self._scaling_tanh(image)
else:
raise NotImplementedError
示例3: rank
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndim [as 别名]
def rank(a):
"""
Return the number of dimensions of an array.
.. note::
This function is deprecated in NumPy 1.9 to avoid confusion with
`numpy.linalg.matrix_rank`. The ``ndim`` attribute or function
should be used instead.
See Also
--------
ndim : equivalent non-deprecated function
Notes
-----
In the old Numeric package, `rank` was the term used for the number of
dimensions, but in NumPy `ndim` is used instead.
"""
# 2014-04-12, 1.9
warnings.warn(
"`rank` is deprecated; use the `ndim` attribute or function instead. "
"To find the rank of a matrix see `numpy.linalg.matrix_rank`.",
VisibleDeprecationWarning, stacklevel=2)
return ndim(a)
示例4: _format_scalarmappable_value
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndim [as 别名]
def _format_scalarmappable_value(artist, idx): # matplotlib/matplotlib#12473.
data = artist.get_array()[idx]
if np.ndim(data) == 0:
if not artist.colorbar:
fig = Figure()
ax = fig.subplots()
artist.colorbar = fig.colorbar(artist, cax=ax)
# This hack updates the ticks without actually paying the cost of
# drawing (RendererBase.draw_path raises NotImplementedError).
try:
ax.yaxis.draw(RendererBase())
except NotImplementedError:
pass
fmt = artist.colorbar.formatter.format_data_short
return "[" + _strip_math(fmt(data).strip()) + "]"
else:
return artist.format_cursor_data(data) # Includes brackets.
示例5: deprocess_image
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndim [as 别名]
def deprocess_image(x):
""" Same normalization as in:
https://github.com/fchollet/keras/blob/master/examples/conv_filter_visualization.py
"""
if np.ndim(x) > 3:
x = np.squeeze(x)
# normalize tensor: center on 0., ensure std is 0.1
x = x - x.mean()
x = x / (x.std() + 1e-5)
x = x * 0.1
# clip to [0, 1]
x = x + 0.5
x = np.clip(x, 0, 1)
# convert to RGB array
x = x * 255
if K.image_dim_ordering() == 'th':
x = x.transpose((1, 2, 0))
x = np.clip(x, 0, 255).astype('uint8')
return x
示例6: compute_gradient
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndim [as 别名]
def compute_gradient(self, grad=None):
''' Compute the gradients for this operation wrt input values.
:param grad: The gradient of other operation wrt the addition output.
:type grad: number or a ndarray, default value is 1.0.
'''
x, y = [node.output_value for node in self.input_nodes]
if grad is None:
grad = np.ones_like(self.output_value)
grad_wrt_x = grad
while np.ndim(grad_wrt_x) > len(np.shape(x)):
grad_wrt_x = np.sum(grad_wrt_x, axis=0)
for axis, size in enumerate(np.shape(x)):
if size == 1:
grad_wrt_x = np.sum(grad_wrt_x, axis=axis, keepdims=True)
grad_wrt_y = grad
while np.ndim(grad_wrt_y) > len(np.shape(y)):
grad_wrt_y = np.sum(grad_wrt_y, axis=0)
for axis, size in enumerate(np.shape(y)):
if size == 1:
grad_wrt_y = np.sum(grad_wrt_y, axis=axis, keepdims=True)
return [grad_wrt_x, grad_wrt_y]
示例7: tndim
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndim [as 别名]
def tndim(z, x):
d[z] = numpy.ndim(d[x])
示例8: flatten_feature
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndim [as 别名]
def flatten_feature(self, feature, value, **kwargs):
"""Convert the features into a dict of 1 dimension values.
The methods check if the dimension of the value is 1 then a
dictionary with key the feature name, and the value the value.
In other cases an recursive approach is taken where every feature
has as name `feature_<N>` as name, where N is the current dimension.
Example
-------
.. code-block:: pycon
>>> e.flatten("name", 1)
{'name': 1}
>>> e.flatten("name", [1, 2, 3])
{'name_0': 1, 'name_1': 2, 'name_2': 3}
>>> e.flatten("name", [1, [2, 3]])
{'name_0': 1, 'name_1_0': 2, 'name_1_1': 3}
>>> flatten("name", [[1, 2], [3, 4]])
{'name_0_0': 1, 'name_0_1': 2, 'name_1_0': 3, 'name_1_1': 4}
"""
if np.ndim(value) == 0:
return {feature: value}
flatten_values = {}
for idx, v in enumerate(value):
flatten_name = f"{feature}_{idx}"
flatten_values.update(
self.flatten_feature(flatten_name, v, **kwargs)
)
return flatten_values
示例9: test_partial_fit_greedy0_r2
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndim [as 别名]
def test_partial_fit_greedy0_r2(self):
arms, mab = self.predict(arms=[1, 2, 3, 4],
decisions=[1, 1, 1, 2, 2, 3, 3, 3, 3, 3],
rewards=[0, 1, 1, 0, 0, 0, 0, 1, 1, 1],
learning_policy=LearningPolicy.EpsilonGreedy(epsilon=0),
neighborhood_policy=NeighborhoodPolicy.Radius(2),
context_history=[[0, 1, 2, 3, 5], [1, 1, 1, 1, 1], [0, 0, 1, 0, 0],
[0, 2, 2, 3, 5], [1, 3, 1, 1, 1], [0, 0, 0, 0, 0],
[0, 1, 4, 3, 5], [0, 1, 2, 4, 5], [1, 2, 1, 1, 3],
[0, 2, 1, 0, 0]],
contexts=[[0, 1, 2, 3, 5], [1, 1, 1, 1, 1]],
seed=123456,
num_run=1,
is_predict=True)
self.assertListEqual(arms, [3, 1])
self.assertEqual(len(mab._imp.decisions), 10)
self.assertEqual(len(mab._imp.rewards), 10)
self.assertEqual(len(mab._imp.contexts), 10)
self.assertEqual(np.ndim(mab._imp.decisions), 1)
decisions2 = [1, 2, 3]
rewards2 = [1, 1, 1]
context_history2 = [[0, 1, 2, 3, 5], [1, 1, 1, 1, 1], [0, 0, 1, 0, 0]]
mab.partial_fit(decisions2, rewards2, context_history2)
self.assertEqual(len(mab._imp.decisions), 13)
self.assertEqual(len(mab._imp.rewards), 13)
self.assertEqual(len(mab._imp.contexts), 13)
self.assertEqual(np.ndim(mab._imp.decisions), 1)
示例10: test_partial_fit_thompson_thresholds
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndim [as 别名]
def test_partial_fit_thompson_thresholds(self):
arm_to_threshold = {1: 1, 2: 5, 3: 2, 4: 3}
def binarize(arm, reward):
return reward >= arm_to_threshold[arm]
arms, mab = self.predict(arms=[1, 2, 3, 4],
decisions=[1, 1, 1, 2, 2, 3, 3, 3, 3, 3],
rewards=[0, 1, 7, 0, 1, 9, 0, 2, 6, 11],
learning_policy=LearningPolicy.ThompsonSampling(binarize),
neighborhood_policy=NeighborhoodPolicy.Radius(2),
context_history=[[0, 1, 2, 3, 5], [1, 1, 1, 1, 1], [0, 0, 1, 0, 0],
[0, 2, 2, 3, 5], [1, 3, 1, 1, 1], [0, 0, 0, 0, 0],
[0, 1, 4, 3, 5], [0, 1, 2, 4, 5], [1, 2, 1, 1, 3],
[0, 2, 1, 0, 0]],
contexts=[[0, 1, 2, 3, 5], [1, 1, 1, 1, 1]],
seed=123456,
num_run=1,
is_predict=True)
self.assertTrue(mab._imp.lp.is_contextual_binarized)
self.assertListEqual(arms, [3, 4])
self.assertEqual(len(mab._imp.decisions), 10)
self.assertEqual(len(mab._imp.rewards), 10)
self.assertEqual(len(mab._imp.contexts), 10)
self.assertEqual(np.ndim(mab._imp.decisions), 1)
self.assertTrue(mab._imp.rewards.all() in [0, 1])
decisions2 = [1, 2, 3]
rewards2 = [11, 1, 6]
context_history2 = [[0, 1, 2, 3, 5], [1, 1, 1, 1, 1], [0, 0, 1, 0, 0]]
mab.partial_fit(decisions2, rewards2, context_history2)
self.assertEqual(len(mab._imp.decisions), 13)
self.assertEqual(len(mab._imp.rewards), 13)
self.assertEqual(len(mab._imp.contexts), 13)
self.assertEqual(np.ndim(mab._imp.decisions), 1)
self.assertTrue(mab._imp.rewards.all() in [0, 1])
示例11: test_fit_twice_thompson_thresholds
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndim [as 别名]
def test_fit_twice_thompson_thresholds(self):
arm_to_threshold = {1: 1, 2: 5, 3: 2, 4: 3}
def binarize(arm, reward):
return reward >= arm_to_threshold[arm]
arms, mab = self.predict(arms=[1, 2, 3, 4],
decisions=[1, 1, 1, 2, 2, 3, 3, 3, 3, 3],
rewards=[0, 1, 7, 0, 1, 9, 0, 2, 6, 11],
learning_policy=LearningPolicy.ThompsonSampling(binarize),
neighborhood_policy=NeighborhoodPolicy.Radius(2),
context_history=[[0, 1, 2, 3, 5], [1, 1, 1, 1, 1], [0, 0, 1, 0, 0],
[0, 2, 2, 3, 5], [1, 3, 1, 1, 1], [0, 0, 0, 0, 0],
[0, 1, 4, 3, 5], [0, 1, 2, 4, 5], [1, 2, 1, 1, 3],
[0, 2, 1, 0, 0]],
contexts=[[0, 1, 2, 3, 5], [1, 1, 1, 1, 1]],
seed=123456,
num_run=1,
is_predict=True)
self.assertTrue(mab._imp.lp.is_contextual_binarized)
self.assertListEqual(arms, [3, 4])
self.assertEqual(len(mab._imp.decisions), 10)
self.assertEqual(len(mab._imp.rewards), 10)
self.assertEqual(len(mab._imp.contexts), 10)
self.assertEqual(np.ndim(mab._imp.decisions), 1)
self.assertTrue(mab._imp.rewards.all() in [0, 1])
decisions2 = [1, 2, 3]
rewards2 = [11, 1, 6]
context_history2 = [[0, 1, 2, 3, 5], [1, 1, 1, 1, 1], [0, 0, 1, 0, 0]]
mab.fit(decisions2, rewards2, context_history2)
self.assertEqual(len(mab._imp.decisions), 3)
self.assertEqual(len(mab._imp.rewards), 3)
self.assertEqual(len(mab._imp.contexts), 3)
self.assertEqual(np.ndim(mab._imp.decisions), 1)
self.assertTrue(mab._imp.rewards.all() in [0, 1])
示例12: test_partial_fit_thompson_thresholds
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndim [as 别名]
def test_partial_fit_thompson_thresholds(self):
arm_to_threshold = {1: 1, 2: 5, 3: 2, 4: 3}
def binarize(arm, reward):
return reward >= arm_to_threshold[arm]
arms, mab = self.predict(arms=[1, 2, 3, 4],
decisions=[1, 1, 1, 2, 2, 3, 3, 3, 3, 3],
rewards=[0, 1, 7, 0, 1, 9, 0, 2, 6, 11],
learning_policy=LearningPolicy.ThompsonSampling(binarize),
neighborhood_policy=NeighborhoodPolicy.Clusters(3),
context_history=[[0, 1, 2, 3, 5], [1, 1, 1, 1, 1], [0, 0, 1, 0, 0],
[0, 2, 2, 3, 5], [1, 3, 1, 1, 1], [0, 0, 0, 0, 0],
[0, 1, 4, 3, 5], [0, 1, 2, 4, 5], [1, 2, 1, 1, 3],
[0, 2, 1, 0, 0]],
contexts=[[0, 1, 2, 3, 5], [1, 1, 1, 1, 1]],
seed=123456,
num_run=1,
is_predict=True)
self.assertTrue(mab._imp.lp_list[0].is_contextual_binarized)
self.assertListEqual(arms, [3, 4])
self.assertEqual(len(mab._imp.decisions), 10)
self.assertEqual(len(mab._imp.rewards), 10)
self.assertEqual(len(mab._imp.contexts), 10)
self.assertEqual(np.ndim(mab._imp.decisions), 1)
self.assertListEqual(list(set(mab._imp.rewards)), [0, 1])
decisions2 = [1, 2, 3]
rewards2 = [11, 1, 6]
context_history2 = [[0, 1, 2, 3, 5], [1, 1, 1, 1, 1], [0, 0, 1, 0, 0]]
mab.partial_fit(decisions2, rewards2, context_history2)
self.assertEqual(len(mab._imp.decisions), 13)
self.assertEqual(len(mab._imp.rewards), 13)
self.assertEqual(len(mab._imp.contexts), 13)
self.assertEqual(np.ndim(mab._imp.decisions), 1)
self.assertListEqual(list(set(mab._imp.rewards)), [0, 1])
示例13: test_fit_twice_thompson_thresholds
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndim [as 别名]
def test_fit_twice_thompson_thresholds(self):
arm_to_threshold = {1: 1, 2: 5, 3: 2, 4: 3}
def binarize(arm, reward):
return reward >= arm_to_threshold[arm]
arms, mab = self.predict(arms=[1, 2, 3, 4],
decisions=[1, 1, 1, 2, 2, 3, 3, 3, 3, 3],
rewards=[0, 1, 7, 0, 1, 9, 0, 2, 6, 11],
learning_policy=LearningPolicy.ThompsonSampling(binarize),
neighborhood_policy=NeighborhoodPolicy.Clusters(3),
context_history=[[0, 1, 2, 3, 5], [1, 1, 1, 1, 1], [0, 0, 1, 0, 0],
[0, 2, 2, 3, 5], [1, 3, 1, 1, 1], [0, 0, 0, 0, 0],
[0, 1, 4, 3, 5], [0, 1, 2, 4, 5], [1, 2, 1, 1, 3],
[0, 2, 1, 0, 0]],
contexts=[[0, 1, 2, 3, 5], [1, 1, 1, 1, 1]],
seed=123456,
num_run=1,
is_predict=True)
self.assertTrue(mab._imp.lp_list[0].is_contextual_binarized)
self.assertListEqual(arms, [3, 4])
self.assertEqual(len(mab._imp.decisions), 10)
self.assertEqual(len(mab._imp.rewards), 10)
self.assertEqual(len(mab._imp.contexts), 10)
self.assertEqual(np.ndim(mab._imp.decisions), 1)
self.assertListEqual(list(set(mab._imp.rewards)), [0, 1])
decisions2 = [1, 2, 3]
rewards2 = [11, 1, 6]
context_history2 = [[0, 1, 2, 3, 5], [1, 1, 1, 1, 1], [0, 0, 1, 0, 0]]
mab.fit(decisions2, rewards2, context_history2)
self.assertEqual(len(mab._imp.decisions), 3)
self.assertEqual(len(mab._imp.rewards), 3)
self.assertEqual(len(mab._imp.contexts), 3)
self.assertEqual(np.ndim(mab._imp.decisions), 1)
self.assertListEqual(list(set(mab._imp.rewards)), [0, 1])
示例14: test_partial_fit_greedy0_r2
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndim [as 别名]
def test_partial_fit_greedy0_r2(self):
arms, mab = self.predict(arms=[1, 2, 3, 4],
decisions=[1, 1, 1, 2, 2, 3, 3, 3, 3, 3],
rewards=[0, 1, 1, 0, 0, 0, 0, 1, 1, 1],
learning_policy=LearningPolicy.EpsilonGreedy(epsilon=0),
neighborhood_policy=NeighborhoodPolicy.KNearest(2),
context_history=[[0, 1, 2, 3, 5], [1, 1, 1, 1, 1], [0, 0, 1, 0, 0],
[0, 2, 2, 3, 5], [1, 3, 1, 1, 1], [0, 0, 0, 0, 0],
[0, 1, 4, 3, 5], [0, 1, 2, 4, 5], [1, 2, 1, 1, 3],
[0, 2, 1, 0, 0]],
contexts=[[0, 1, 2, 3, 5], [1, 1, 1, 1, 1]],
seed=123456,
num_run=1,
is_predict=True)
self.assertListEqual(arms, [1, 1])
self.assertEqual(len(mab._imp.decisions), 10)
self.assertEqual(len(mab._imp.decisions), 10)
self.assertEqual(len(mab._imp.rewards), 10)
self.assertEqual(np.ndim(mab._imp.decisions), 1)
decisions2 = [1, 2, 3]
rewards2 = [1, 1, 1]
context_history2 = [[0, 1, 2, 3, 5], [1, 1, 1, 1, 1], [0, 0, 1, 0, 0]]
mab.partial_fit(decisions2, rewards2, context_history2)
self.assertEqual(len(mab._imp.decisions), 13)
self.assertEqual(len(mab._imp.rewards), 13)
self.assertEqual(len(mab._imp.contexts), 13)
self.assertEqual(np.ndim(mab._imp.decisions), 1)
示例15: test_partial_fit_thompson_thresholds
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndim [as 别名]
def test_partial_fit_thompson_thresholds(self):
arm_to_threshold = {1: 1, 2: 5, 3: 2, 4: 3}
def binarize(arm, reward):
return reward >= arm_to_threshold[arm]
arms, mab = self.predict(arms=[1, 2, 3, 4],
decisions=[1, 1, 1, 2, 2, 3, 3, 3, 3, 3],
rewards=[0, 1, 7, 0, 1, 9, 0, 2, 6, 11],
learning_policy=LearningPolicy.ThompsonSampling(binarize),
neighborhood_policy=NeighborhoodPolicy.KNearest(2),
context_history=[[0, 1, 2, 3, 5], [1, 1, 1, 1, 1], [0, 0, 1, 0, 0],
[0, 2, 2, 3, 5], [1, 3, 1, 1, 1], [0, 0, 0, 0, 0],
[0, 1, 4, 3, 5], [0, 1, 2, 4, 5], [1, 2, 1, 1, 3],
[0, 2, 1, 0, 0]],
contexts=[[0, 1, 2, 3, 5], [1, 1, 1, 1, 1]],
seed=123456,
num_run=1,
is_predict=True)
self.assertTrue(mab._imp.lp.is_contextual_binarized)
self.assertListEqual(arms, [4, 4])
self.assertEqual(len(mab._imp.decisions), 10)
self.assertEqual(len(mab._imp.rewards), 10)
self.assertEqual(len(mab._imp.contexts), 10)
self.assertEqual(np.ndim(mab._imp.decisions), 1)
self.assertListEqual(list(set(mab._imp.rewards)), [0, 1])
decisions2 = [1, 2, 3]
rewards2 = [11, 1, 6]
context_history2 = [[0, 1, 2, 3, 5], [1, 1, 1, 1, 1], [0, 0, 1, 0, 0]]
mab.partial_fit(decisions2, rewards2, context_history2)
self.assertEqual(len(mab._imp.decisions), 13)
self.assertEqual(len(mab._imp.rewards), 13)
self.assertEqual(len(mab._imp.contexts), 13)
self.assertEqual(np.ndim(mab._imp.decisions), 1)
arm = mab.predict([[0, 1, 2, 3, 5]])
self.assertEqual(arm, 3)
self.assertListEqual(list(set(mab._imp.rewards)), [0, 1])