本文整理匯總了Python中axes.Axes.insert方法的典型用法代碼示例。如果您正苦於以下問題:Python Axes.insert方法的具體用法?Python Axes.insert怎麽用?Python Axes.insert使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類axes.Axes
的用法示例。
在下文中一共展示了Axes.insert方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _take_broadcast
# 需要導入模塊: from axes import Axes [as 別名]
# 或者: from axes.Axes import insert [as 別名]
def _take_broadcast(a, indices):
""" broadcast array-indices & integers, numpy's classical
Examples:
---------
>>> a = da.zeros(shape=(3,4,5,6))
>>> a[:,[0, 1],:,2].shape
(2, 3, 5)
>>> a[:,[0, 1],2,:].shape
(3, 2, 6)
"""
# new values
newval = a.values[indices]
# if the new values is a scalar, then just return it
if np.isscalar(newval):
return newval
# new axes: broacast indices (should do the same as above, since integers are just broadcast)
indices2 = broadcast_indices(indices)
# assert np.all(newval == a.values[indices2])
# make a multi-axis with tuples
is_array2 = np.array([np.iterable(ix) for ix in indices2])
nb_array2 = is_array2.sum()
# If none or one array is present, easy
if nb_array2 <= 1:
newaxes = [
a.axes[i][ix] for i, ix in enumerate(indices) if not np.isscalar(ix)
] # indices or indices2, does not matter
# else, finer check needed
else:
# same stats but on original indices
is_array = np.array([np.iterable(ix) for ix in indices])
array_ix_pos = np.where(is_array)[0]
# Determine where the axis will be inserted
# - need to consider the integers as well (broadcast as arrays)
# - if two indexed dimensions are not contiguous, new axis placed at first position...
# a = zeros((3,4,5,6))
# a[:,[1,2],:,0].shape ==> (2, 3, 5)
# a[:,[1,2],0,:].shape ==> (3, 2, 6)
array_ix_pos2 = np.where(is_array2)[0]
if np.any(np.diff(array_ix_pos2) > 1): # that mean, if two indexed dimensions are not contiguous
insert = 0
else:
insert = array_ix_pos2[0]
# Now determine axis value
# ...if originally only one array was provided, use these values correspondingly
if len(array_ix_pos) == 1:
i = array_ix_pos[0]
values = a.axes[i].values[indices[i]]
name = a.axes[i].name
# ...else use a list of tuples
else:
values = zip(*[a.axes[i].values[indices2[i]] for i in array_ix_pos])
name = ",".join([a.axes[i].name for i in array_ix_pos])
broadcastaxis = Axis(values, name)
newaxes = Axes()
for i, ax in enumerate(a.axes):
# axis is already part of the broadcast axis: skip
if is_array2[i]:
continue
else:
newaxis = ax[indices2[i]]
## do not append axis if scalar
# if np.isscalar(newaxis):
# continue
newaxes.append(newaxis)
# insert the right new axis at the appropriate position
newaxes.insert(insert, broadcastaxis)
return a._constructor(newval, newaxes, **a._metadata)