版权声明:License CC BY-NC-SA 4.0 https://blog.csdn.net/wizardforcel/article/details/72823633
>>> from numpy import *
多维数组
>>> m = array([arange(2), arange(2)])
>>> m
array([[0, 1],
[0, 1]])
>>> m.shape
(2, 2)
>>> a = array([[1,2],[3,4]])
>>> a
array([[1, 2],
[3, 4]])
>>> a[0,0]
1
>>> a[0,1]
2
>>> a[1,0]
3
>>> a[1,1]
4
数值类型
类型 |
描述 |
bool |
布尔值,一位 |
int |
平台相关整数,int32 或int64 |
int8 |
字节(-128 ~ 127 ) |
int16 |
整数(-32768 ~ 32767 ) |
int32 |
整数(-2 ** 31 ~ 2 ** 31 - 1 ) |
int64 |
整数(-2 ** 63 ~ 2 ** 63 - 1 ) |
uint8 |
无符号整数(0 ~ 255 ) |
uint16 |
无符号整数(0 ~ 65535 ) |
uint32 |
无符号整数(0 ~ 2 ** 32 - 1 ) |
uint64 |
无符号整数(0 ~ 2 ** 64 - 1 ) |
float16 |
半精度浮点,符号位,5 位指数,10 位尾数 |
float32 |
单精度浮点,符号位,8 位指数,23 位尾数 |
float64 或float |
双精度浮点,符号位,11 位指数,52 位尾数 |
complex64 |
复数,由两个 32 位浮点表示(实部和虚部) |
complex128 或complex |
复数,由两个 64 位浮点表示(实部和虚部) |
>>> float64(42)
42.0
>>> int8(42.0)
42
>>> bool(42)
True
>>> bool(0)
False
>>> bool(42.0)
True
>>> float(True)
1.0
>>> float(False)
0.0
>>> int(42.0 + 1.j)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't convert complex to int
数据类型对象(dtype)
# 从数值类型构造
>>> dtype(float)
dtype('float64')
# 从字符代码构造
>>> dtype('f')
dtype('float32')
>>> dtype('d')
dtype('float64')
# 从双字符代码构造
>>> dtype('f8')
dtype('float64')
# 获取所有字符代码
>>> sctypeDict.keys()
[0, … 'i2', 'int0']
# char 属性获取字符代码
>>> t = dtype('Float64')
>>> t.char
'd'
# type 属性获取类型
>>> t.type
<type 'numpy.float64'>
# str 属性获取完整字符串表示
# 第一个字符是字节序,< 表示小端,> 表示大端,| 表示平台的字节序
>>> t.str
'<f8'
# 获取大小
>>> t.itemsize
8
# 许多函数拥有 dtype 参数
# 传入数值类型、字符代码和 dtype 都可以
>>> arange(7, dtype=uint16)
array([0, 1, 2, 3, 4, 5, 6], dtype=uint16)
类型 |
字符代码 |
bool |
? , b1 |
int8 |
b , i1 |
uint8 |
B , u1 |
int16 |
h , i2 |
uint16 |
H , u2 |
int32 |
i , i4 |
uint32 |
I , u4 |
int64 |
q , i8 |
uint64 |
Q , u8 |
float16 |
f2 , e |
float32 |
f4 , f |
float64 |
f8 , d |
complex64 |
F4 , F |
complex128 |
F8 , D |
str |
S (可以在S 后面添加数字,表示字符串长度,比如S3 表示长度为三的字符串,不写则为最大长度) |
unicode |
U |
object |
O |
void |
V |
记录类型
>>> t = dtype([('name', str_, 40), ('numitems', int32), ('price', float32)])
>>> t
dtype([('name', '|S40'), ('numitems', '<i4'), ('price','<f4')])
>>> t['name']
dtype('|S40')
>>> itemz = array([('Meaning of life DVD', 42, 3.14), ('Butter', 13,2.72)], dtype=t)
>>> itemz[1]
('Butter', 13, 2.7200000286102295)
操作形状
>>> b = array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
>>> b.ravel()
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,14, 15, 16,
17, 18, 19, 20, 21, 22, 23])
>>> b.flatten()
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,14, 15, 16,
17, 18, 19, 20, 21, 22, 23])
>>> b.shape = (6,4)
>>> b
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]])
>>> b.transpose()
array([[ 0, 4, 8, 12, 16, 20],
[ 1, 5, 9, 13, 17, 21],
[ 2, 6, 10, 14, 18, 22],
[ 3, 7, 11, 15, 19, 23]])
>>> b.resize((2,12))
>>> b
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]])
堆叠
>>> a = arange(9).reshape(3,3)
>>> a
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> b = 2 * a
>>> b
array([[ 0, 2, 4],
[ 6, 8, 10],
[12, 14, 16]])
>>> hstack((a, b))
array([[ 0, 1, 2, 0, 2, 4],
[ 3, 4, 5, 6, 8, 10],
[ 6, 7, 8, 12, 14, 16]])
>>> concatenate((a, b), axis=1)
array([[ 0, 1, 2, 0, 2, 4],
[ 3, 4, 5, 6, 8, 10],
[ 6, 7, 8, 12, 14, 16]])
>>> vstack((a, b))
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 0, 2, 4],
[ 6, 8, 10],
[12, 14, 16]])
>>> concatenate((a, b), axis=0)
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 0, 2, 4],
[ 6, 8, 10],
[12, 14, 16]])
>>> dstack((a, b))
array([[[ 0, 0],
[ 1, 2],
[ 2, 4]],
[[ 3, 6],
[ 4, 8],
[ 5, 10]],
[[ 6, 12],
[ 7, 14],
[ 8, 16]]])
>>> oned = arange(2)
>>> oned
array([0, 1])
>>> twice_oned = 2 * oned
>>> twice_oned
array([0, 2])
>>> column_stack((oned, twice_oned))
array([[0, 0],
[1, 2]])
>>> column_stack((a, b))
array([[ 0, 1, 2, 0, 2, 4],
[ 3, 4, 5, 6, 8, 10],
[ 6, 7, 8, 12, 14, 16]])
>>> column_stack((a, b)) == hstack((a, b))
array([[ True, True, True, True, True, True],
[ True, True, True, True, True, True],
[ True, True, True, True, True, True]], dtype=bool)
>>> row_stack((oned, twice_oned))
array([[0, 1],
[0, 2]])
>>> row_stack((a, b))
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 0, 2, 4],
[ 6, 8, 10],
[12, 14, 16]])
>>> row_stack((a,b)) == vstack((a, b))
array([[ True, True, True],
[ True, True, True],
[ True, True, True],
[ True, True, True],
[ True, True, True],
[ True, True, True]], dtype=bool)
分割
>>> a
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> hsplit(a, 3)
[array([[0],
[3],
[6]]),
array([[1],
[4],
[7]]),
array([[2],
[5],
[8]])]
>>> split(a, 3, axis=1)
[array([[0],
[3],
[6]]),
array([[1],
[4],
[7]]),
array([[2],
[5],
[8]])]
>>> vsplit(a, 3)
[array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7,8]])]
>>> split(a, 3, axis=0)
[array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7,8]])]
>>> c = arange(27).reshape(3, 3, 3)
>>> c
array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]])
>>> dsplit(c, 3)
[array([[[ 0],
[ 3],
[ 6]],
[[ 9],
[12],
[15]],
[[18],
[21],
[24]]]),
array([[[ 1],
[ 4],
[ 7]],
[[10],
[13],
[16]],
[[19],
[22],
[25]]]),
array([[[ 2],
[ 5],
[ 8]],
[[11],
[14],
[17]],
[[20],
[23],
[26]]])]
属性
>>> b
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]])
>>> b.ndim
2
>>> b.size
24
>>> b.itemsize
8
>>> b.nbytes
192
>>> b.size * b.itemsize
192
>>> b.resize(6,4)
>>> b
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]])
>>> b.T
array([[ 0, 4, 8, 12, 16, 20],
[ 1, 5, 9, 13, 17, 21],
[ 2, 6, 10, 14, 18, 22],
[ 3, 7, 11, 15, 19, 23]])
>>> b.ndim
1
>>> b.T
array([0, 1, 2, 3, 4])
>>> b = array([1.j + 1, 2.j + 3])
>>> b
array([ 1.+1.j, 3.+2.j])
>>> b.dtype
dtype('complex128')
>>> b.dtype.str
'<c16'
>>> b.real
array([ 1., 3.])
>>> b.imag
array([ 1., 2.])
>>> b = arange(4).reshape(2,2)
>>> b
array([[0, 1],
[2, 3]])
>>> f = b.flat
>>> f
<numpy.flatiter object at 0x103013e00>
>>> for item in f: print item
0
1
2
3
>>> b.flat[2]
2
>>> b.flat[[1,3]]
array([1, 3])
>>> b.flat = 7
>>> b
array([[7, 7],
[7, 7]])
>>> b.flat[[1,3]] = 1
>>> b
array([[7, 1],
[7, 1]])
转换
>>> b
array([ 1.+1.j, 3.+2.j])
>>> b.tolist()
[(1+1j), (3+2j)]
>>> b
array([ 1.+1.j, 3.+2.j])
>>> b.astype(int)
/usr/local/bin/ipython:1: ComplexWarning: Casting complex values to real discards the imaginary part
array([1, 3])