当前位置: 首页>>代码示例>>Python>>正文


Python Axes.insert方法代码示例

本文整理汇总了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)
开发者ID:koenvo,项目名称:dimarray,代码行数:86,代码来源:indexing.py


注:本文中的axes.Axes.insert方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。