numpy属性方法合集

数组属性方法总结

作用

1

基本属性

a.dtype

数组元素类型float32,uint8,...

a.shape

数组形状(m,n,o,...)

a.size

数组元素数

a.itemsize

每个元素占字节数

a.nbytes

所有元素占的字节

a.ndim

数组维度

2

形状相关

a.flat

所有元素的迭代器

a.flatten()

返回一个1维数组的复制

a.ravel()

返回一个1维数组,高效

a.resize(new_size)

改变形状

a.swapaxes(axis1, axis2)

交换两个维度的位置

a.transpose(*axex)

交换所有维度的位置

a.T

转置,a.transpose()

a.squeeze()

去除所有长度为1的维度

3

填充复制

a.copy()

返回数组的一个复制

a.fill(value)

将数组的元组设置为特定值

4

转化

a.tolist()

将数组转化为列表

a.tostring()

转换为字符串

a.astype(dtype)

转化为指定类型

a.byteswap(False)

转换大小字节序

a.view(type_or_dtype)

生成一个使用相同内存,但使用不同的表示方法的数组

5

复数

a.imag

虚部

a.real

实部

a.conjugate()

复共轭

a.conj()

复共轭(缩写)

6

保存

a.dump(file)

将二进制数据存在file中

a.dump()

将二进制数据表示成字符串

a.tofile(fid, sep="",format="%s")

格式化ASCⅡ码写入文件

7

查找排序

a.nonzero()

返回所有非零元素的索引

a.sort(axis=-1)

沿某个轴排序

a.argsort(axis=-1)

沿某个轴,返回按排序的索引

a.searchsorted(b)

返回将b中元素插入a后能保持有序的索引值

8

元素数学操作

a.clip(low, high)

将数值限制在一定范围内

a.round(decimals=0)

近似到指定精度

a.cumsum(axis=None)

累加和

a.cumprod(axis=None)

累乘积

9

约简操作

a.sum(axis=None)

求和

a.prod(axis=None)

求积

a.min(axis=None)

最小值

a.max(axis=None)

最大值

a.argmin(axis=None)

最小值索引

a.argmax(axis=None)

最大值索引

a.ptp(axis=None)

最大值减最小值

a.mean(axis=None)

平均值

a.std(axis=None)

标准差

a.var(axis=None)

方差

a.any(axis=None)

只要有一个不为0,返回真,逻辑或

a.all(axis=None)

所有都不为0,返回真,逻辑与

In[1]:

from numpy import *

In[2]:

a = array([[0, 1, 2, 3], [4, 5, 6, 7]])a

Out[2]:

array([[0, 1, 2, 3],

[4, 5, 6, 7]])

数组元素属性:

In[3]:

Out[3]:

dtype('int32')

形状:

In[4]:

Out[4]:

(2L, 4L)

元素数目:

In[5]:

Out[5]:

元素占字节大小:

In[6]:

Out[6]:

所有元素所占字节:

In[7]:

Out[7]:

32

数据维度:

In[8]:

Out[8]:

In[9]:

for row in a:

print row

[0 1 2 3]

[4 5 6 7]

所有元素的迭代器:

In[10]:

for elt in a.flat:

print elt

0

所有元素组成的一维数组,按照行排列:

In[11]:

Out[11]:

array([0, 1, 2, 3, 4, 5, 6, 7])

In[12]:

Out[12]:

重新改变形状:

In[13]:

a.resize((4,2))a

Out[13]:

array([[0, 1],

[2, 3],

[4, 5],

[6, 7]])

交换这两个轴的顺序:

In[14]:

a.swapaxes(0,1)

Out[14]:

array([[0, 2, 4, 6],

[1, 3, 5, 7]])

转置:

In[15]:

a.transpose()

Out[15]:

In[16]:

Out[16]:

In[17]:

a2 = array([1,2,3])a2.shape

Out[17]:

(3L,)

In[18]:

a2.resize((1,3,1))a2.shape

Out[18]:

(1L, 3L, 1L)

去除长度为1的维度:

In[19]:

a2 = a2.squeeze()a2.shape

Out[19]:

复制:

In[20]:

b = a.copy()b

Out[20]:

复制不影响原来的数组:

In[21]:

b[0][0] = -1b # First value changed

Out[21]:

array([[-1, 1],

[ 2, 3],

[ 4, 5],

[ 6, 7]])

In[22]:

a # original not changed because b is a copy

Out[22]:

填充:

In[23]:

b.fill(4)b

Out[23]:

array([[4, 4],

[4, 4],

[4, 4]])

转化为列表:

In[24]:

Out[24]:

[[0, 1], [2, 3], [4, 5], [6, 7]]

转化为字符串:

In[25]:

Out[25]:

''

改变数组元素类型:

In[26]:

a.astype(float)

Out[26]:

array([[ 0., 1.],

[ 2., 3.],

[ 4., 5.],

[ 6., 7.]])

In[27]:

b = a.copy()b.byteswap(False)

Out[27]:

array([[ 0, 16777216],

[ 33554432, 50331648],

[ 67108864, 83886080],

[100663296, 117440512]])

将它看成16位整数:

In[28]:

a.view(dtype=int16)

Out[28]:

array([[0, 0, 1, 0],

[2, 0, 3, 0],

[4, 0, 5, 0],

[6, 0, 7, 0]], dtype=int16)

实部:

In[29]:

b = array([1+2j, 3+4j, 5+6j])b.real

Out[29]:

array([ 1., 3., 5.])

虚部:

In[30]:

b.imag

Out[30]:

array([ 2., 4., 6.])

共轭:

In[31]:

b.conj()

Out[31]:

array([ 1.-2.j, 3.-4.j, 5.-6.j])

In[32]:

b.conjugate()

Out[32]:

保存成文本:

In[33]:

a.dump("file.txt")

字符串:

In[34]:

a.dumps()

Out[34]:

'cnumpy.core.multiarray_reconstructqcnumpyndarrayqKUbRq(KcnumpydtypeqUi4KKRq(KU<NNNJJKtbU tb.'

写入文件:

In[35]:

a.tofile('foo.csv', sep=',', format="%s")

非零元素的索引:

In[36]:

Out[36]:

(array([0, 1, 1, 2, 2, 3, 3], dtype=int64),

array([1, 0, 1, 0, 1, 0, 1], dtype=int64))

排序:

In[37]:

b = array([3,2,7,4,1])b.sort()b

Out[37]:

array([1, 2, 3, 4, 7])

排序的索引位置:

In[38]:

b = array([2,3,1])b.argsort(axis=-1)

Out[38]:

array([2, 0, 1], dtype=int64)

将b插入a中的索引,使得a保持有序:

In[39]:

a = array([1,3,4,6])b = array([0,2,5])a.searchsorted(b)

Out[39]:

array([0, 1, 3], dtype=int64)

限制在一定范围:

In[40]:

a = array([[4,1,3],[2,1,5]])a.clip(0,2)

Out[40]:

array([[2, 1, 2],

[2, 1, 2]])

近似:

In[41]:

a = array([1.344, 2.449, 2.558])a.round(decimals=2)

Out[41]:

array([ 1.34, 2.45, 2.56])

累加和:

In[42]:

a = array([[4,1,3],[2,1,5]])a.cumsum(axis=None)

Out[42]:

array([ 4, 5, 8, 10, 11, 16])

累乘积:

In[43]:

Out[43]:

array([ 4, 4, 12, 24, 24, 120])

求和:

In[44]:

a = array([[4,1,3],[2,1,5]])a.sum(axis=None)

Out[44]:

16

求积:

In[45]:

Out[45]:

120

最小值:

In[46]:

Out[46]:

最大值:

In[47]:

Out[47]:

最小值索引:

In[48]:

Out[48]:

最大值索引:

In[49]:

Out[49]:

最大间隔:

In[50]:

Out[50]:

均值:

In[51]:

Out[51]:

2.6666666666666665

标准差:

In[52]:

Out[52]:

1.49071198499986

方差:

In[53]:

Out[53]:

2.2222222222222228

是否有非零元素:

In[54]:

Out[54]:

True

是否全部非零:

In[55]:

a.all()

Out[55]:

删除生成的文件:

In[56]:

import osos.remove('foo.csv')os.remove('file.txt')

转载自:https://baijiahao.baidu.com/s?id=1576827524017294960&wfr=spider&for=pc


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值