数组类型
python
from numpy import *之前已经看过整数数组和布尔数组,除此之外还有浮点数数组和复数数组。
复数数组
产生一个复数数组:
python
a = array([1 + 1j, 2, 3, 4])Python会自动判断数组的类型:
python
a.dtypedtype('complex128')
对于复数我们可以查看它的实部和虚部:
python
a.realarray([ 1., 2., 3., 4.])
python
a.imagarray([ 1., 0., 0., 0.])
还可以设置它们的值:
python
a.imag = [1,2,3,4]查看 a:
python
aarray([ 1.+1.j, 2.+2.j, 3.+3.j, 4.+4.j])
查看复共轭:
python
a.conj()array([ 1.-1.j, 2.-2.j, 3.-3.j, 4.-4.j])
事实上,这些属性方法可以用在浮点数或者整数数组上:
python
a = array([0.,1,2,3])
a.dtypedtype('float64')
python
a.realarray([ 0., 1., 2., 3.])
python
a.imagarray([ 0., 0., 0., 0.])
python
a.conj()array([ 0., 1., 2., 3.])
但这里,虚部是只读的,并不能修改它的值:
python
# 会报错
a.imag = [1,2,3,4]---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-13-3db28f506ec9> in <module>()
1 # 会报错
----> 2 a.imag = [1,2,3,4]
TypeError: array does not have imaginary part to set
指定数组类型
之前已经知道,构建数组的时候,数组会根据传入的内容自动判断类型:
python
a = array([0,1.0,2,3])对于浮点数,默认为双精度:
python
a.dtypedtype('float64')
查看所用字节(8 bytes * 4):
python
a.nbytes32
当然,我们也可以在构建的时候指定类型:
python
a = array([0,1.0,2,3],
dtype=float32)此时类型为单精度浮点数:
python
a.dtypedtype('float32')
查看所用字节(4 bytes * 4):
python
a.nbytes16
除此之外,还可以指定有无符号,例如无符号整数:
python
a = array([0,1,2,3],
dtype=uint8)
a.dtypedtype('uint8')
uint8 只使用一个字节,表示 0 到 255 的整数。
还可以从二进制数据中读取。
先写入二进制数据:
python
a = array([102,111,212],
dtype=uint8)
a.tofile('foo.dat')从数据中读入,要指定类型:
python
b = frombuffer('foo',
dtype=uint8)
barray([102, 111, 111], dtype=uint8)
清理数据文件:
python
import os
os.remove('foo.dat')0-255 的数字可以表示ASCⅡ码,我们可以用 ord 函数来查看字符的ASCⅡ码值:
python
ord('f')102
python
ord('S')83
Numpy 类型
具体如下:
| 基本类型 | 可用的Numpy类型 | 备注 |
|---|---|---|
| 布尔型 | bool | 占1个字节 |
| 整型 | int8, int16, int32, int64, int128, int | int 跟C语言中的 long 一样大 |
| 无符号整型 | uint8, uint16, uint32, uint64, uint128, uint | uint 跟C语言中的 unsigned long 一样大 |
| 浮点数 | float16, float32, float64, float, longfloat | 默认为双精度 float64 ,longfloat 精度大小与系统有关 |
| 复数 | complex64, complex128, complex, longcomplex | 默认为 complex128 ,即实部虚部都为双精度 |
| 字符串 | string, unicode | 可以使用 dtype=S4 表示一个4字节字符串的数组 |
| 对象 | object | 数组中可以使用任意值 |
| Records | void | |
| 时间 | datetime64, timedelta64 |
任意类型的数组:
python
a = array([1,1.2,'hello', [10,20,30]],
dtype=object)乘法:
python
a * 2array([2, 2.4, 'hellohello', [10, 20, 30, 10, 20, 30]], dtype=object)
类型转换
转换数组的类型:
python
a = array([1.5, -3],
dtype=float32)
aarray([ 1.5, -3. ], dtype=float32)
asarray 函数
使用 asarray 函数:
python
asarray(a, dtype=float64)array([ 1.5, -3. ])
python
asarray(a, dtype=uint8)array([ 1, 253], dtype=uint8)
asarray 不会修改原来数组的值:
python
aarray([ 1.5, -3. ], dtype=float32)
但当类型相同的时候,asarray 并不会产生新的对象,而是使用同一个引用:
python
b = asarray(a, dtype=float32)python
b is aTrue
这么做的好处在与,asarray 不仅可以作用于数组,还可以将其他类型转化为数组。
有些时候为了保证我们的输入值是数组,我们需要将其使用 asarray 转化,当它已经是数组的时候,并不会产生新的对象,这样保证了效率。
python
asarray([1,2,3,4])array([1, 2, 3, 4])
astype 方法
astype 方法返回一个新数组:
python
a.astype(float64)array([ 1.5, -3. ])
python
a.astype(uint8)array([ 1, 253], dtype=uint8)
astype也不会改变原来数组的值:
python
aarray([ 1.5, -3. ], dtype=float32)
另外,astype 总是返回原来数组的一份复制,即使转换的类型是相同的:
python
b = a.astype(float32)
print a
print b[ 1.5 -3. ]
[ 1.5 -3. ]
python
a is bFalse
view 方法
python
a = array((1,2,3,4), dtype=int32)
aarray([1, 2, 3, 4])
view 会将 a 在内存中的表示看成是 uint8 进行解析:
python
b = a.view(uint8)
barray([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0], dtype=uint8)
python
a[0] = 2**30
aarray([1073741824, 2, 3, 4])
修改 a 会修改 b 的值,因为共用一块内存:
python
barray([ 0, 0, 0, 64, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0], dtype=uint8)