上一篇主要介绍了ndarray对象的一些基本属性以及创建ndarray对象的一些非常常用的方法。接下来主要介绍ndarray对象比较常用的对象方法。需要注意的是,以下ndarray对象方法也是numpy中的函数:all, any, argmax, argmin, argpartition, argsort, choose, clip, compress, copy, cumprod, cumsum, diagonal, imag, max, mean, min, nonzero, partition, prod, ptp, put, ravel, real, repeat, reshape, round, searchsorted, sort, squeeze, std, sum, swapaxes, take, trace, transpose, var
。
1 数组转换方法
常用方法 | 功能 |
---|---|
ndarray.item(*args) | 复制数组中的一个元素,并返回 |
ndarray.tolist() | 将数组转换成python标准list |
ndarray.itemset(*args) | 修改数组中某个元素的值 |
ndarray.tostring([order]) | 构建一个包含ndarray的原始字节数据的字节字符串 |
ndarray.tobytes([order]) | 功能同tostring |
ndarray.byteswap(inplace) | 将ndarray中每个元素中的字节进行大小端转换 |
ndarray.copy([order]) | 复制数组并返回(深拷贝) |
ndarray.fill(value) | 使用值value填充数组 |
示例:
>>> a = np.random.randint(12, size=(3,4))
>>> a
array([[11, 1, 0, 11],
[11, 0, 4, 6],
[ 0, 1, 6, 7]])
>>> a.item(7) #获取第7个元素
6
>>> a.item((1, 2)) #获取元组对应的元素
4
>>> a.itemset(7, 111) #设置元素
>>> a
array([[ 11, 1, 0, 11],
[ 11, 0, 4, 111],
[ 0, 1, 6, 7]])
>>> a.itemset((1, 2), 12)
>>> a
array([[ 11, 1, 0, 11],
[ 11, 0, 12, 111],
[ 0, 1, 6, 7]])
>>> a.tolist() #返回python标准列表,
[[11, 1, 0, 11], [11, 0, 4, 6], [0, 1, 6, 7]]
>>> a.tostring()
'\x0b\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0b\x00\x00\x00\x00\x00\x00\x00\x0b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00\x07\x00\x00\x00\x00\x00\x00\x00'
>>> b = a.copy() #深拷贝,b与a是两个无关的数组
>>> b[0,0] = 10
>>> b
array([[10, 1, 0, 11],
[11, 0, 4, 6],
[ 0, 1, 6, 7]])
>>> a
array([[11, 1, 0, 11],
[11, 0, 4, 6],
[ 0, 1, 6, 7]])
>>> b.fill(9)
>>> b
array([[9, 9, 9, 9],
[9, 9, 9, 9],
[9, 9, 9, 9]])
>>> a.byteswap() #元素大小端转换,a不变
array([[ 792633534417207296, 72057594037927936, 0,
792633534417207296],
[ 792633534417207296, 0, 864691128455135232,
7998392938210000896],
[ 0, 72057594037927936, 432345564227567616,
504403158265495552]])
>>> a
array([[ 11, 1, 0, 11],
[ 11, 0, 12, 111],
[ 0, 1, 6, 7]])
>>> a.byteswap(True) #原地转换,a被修改
array([[ 792633534417207296, 72057594037927936, 0,
792633534417207296],
[ 792633534417207296, 0, 864691128455135232,
7998392938210000896],
[ 0, 72057594037927936, 432345564227567616,
504403158265495552]])
>>> a
array([[ 792633534417207296, 72057594037927936, 0,
792633534417207296],
[ 792633534417207296, 0, 864691128455135232,
7998392938210000896],
[ 0, 72057594037927936, 432345564227567616,
504403158265495552]])
如下几个方法:ndarray.tofile, ndarray.dump, ndarray.dumps, ndarray.astype, ndarray.view, ndarray.getfield, ndarray.setflags
,还没有用过,暂时不对其进行详细介绍,等用到了在补充。关于这些方法的详细介绍可以查阅numpy的官方文档。
2 形状操作
常用方法 | 功能 |
---|---|
ndarray.reshape(shape[,order]) | 返回一个具有相同数据域,但shape不一样的视图 |
ndarray.resize(new_shape[,orefcheck]) | 原地修改数组的形状(需要保持元素个数前后相同) |
ndarray.transpose(*axes) | 返回数组针对某一轴进行转置的视图 |
ndarray.swapaxes(axis1, asix2) | 返回数组axis1轴与axis2轴互换的视图 |
ndarray.flatten([order]) | 返回将原数组压缩成一维数组的拷贝(全新的数组) |
ndarray.ravel([order]) | 返回将原数组压缩成一维数组的视图 |
ndarray.squeeze([axis]) | 返回将原数组中的shape中axis==1的轴移除之后的视图 |
注意事项!!!
上述方法中,除resize
、flatten
外其他的方法返回的都是原数组修改shape或者axes之后的视图,也就是说,对返回数组中的元素进行修改,原数组中对应的元素也会被修改(因为它们是公用同一个数据域的)。同时,resize
方法会修改原数组的shape属性,其他方法不会修改原数组任何内部数据。
示例:
>>> x = np.arange(0,12)
>>> x
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
>>> x_reshape = x.reshape((3,4))
>>> x_reshape
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],