numpy的用法们-边用边记,不断更新

1.数组均分

numpy.array_split()

>>> x = np.arange(8.0)
>>> np.array_split(x, 3)
    [array([ 0.,  1.,  2.]), array([ 3.,  4.,  5.]), array([ 6.,  7.])]

2.数组叠加合并

列合并/扩展:np.column_stack()

行合并/扩展:np.row_stack()

>>> a
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>>> top = np.column_stack((a, np.zeros((3,3))))
>>> top
array([[ 0.,  1.,  2.,  0.,  0.,  0.],
       [ 3.,  4.,  5.,  0.,  0.,  0.],
       [ 6.,  7.,  8.,  0.,  0.,  0.]])
3.删除某个元素

np.delete()

numpy. delete ( arrobjaxis=None ) [source]

Return a new array with sub-arrays along an axis deleted. For a one dimensional array, this returns those entries not returned by arr[obj].

Parameters:

arr : array_like

Input array.

obj : slice, int or array of ints

Indicate which sub-arrays to remove.

axis : int, optional

The axis along which to delete the subarray defined by obj. If axis is None, obj is applied to the flattened array.

Returns:

out : ndarray

A copy of arr with the elements specified by obj removed. Note that deletedoes not occur in-place. If axis is None, out is a flattened array.

>>> arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
>>> arr
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])
>>> np.delete(arr, 1, 0)
array([[ 1,  2,  3,  4],
       [ 9, 10, 11, 12]])
>>>
>>> np.delete(arr, np.s_[::2], 1)
array([[ 2,  4],
       [ 6,  8],
       [10, 12]])
>>> np.delete(arr, [1,3,5], None)
array([ 1,  3,  5,  7,  8,  9, 10, 11, 12])

3.数据遍历

np.delete()

 a = np.arange(9).reshape(3,3)

it = np.nditer(a,flags=['multi_index'], op_flags=['readwrite'])

while not it.finished:

print it.multi_index

it.iternext()

(0,0)

(0,1)

...

(2,2)

https://docs.scipy.org/doc/numpy/reference/generated/numpy.nditer.html



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值