码迷,mamicode.com
首页 > 编程语言 > 详细

NumPy学习笔记:3、更加复杂的数组

时间:2017-07-19 22:02:33      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:logs   org   浮点   dir   不能   .sh   相同   row   eve   

一、更多的数据类型

1、Casting

(1) 在混合数据类型的运算中,最终运算结果的数据类型与size更大的数据类型相同。

>>> np.array([1, 2, 3]) + 1.5
array([ 2.5,  3.5,  4.5])

(2) 给数组中的元素赋值,并不能改变整个数组的数据类型。(若把整型数组中的某个元素赋值为浮点型数据,则这个浮点型数据会被截断为整型)

>>> a = np.array([1, 2, 3])
>>> a.dtype
dtype(int64)
>>> a[0] = 1.9     # <-- float is truncated to integer
>>> a
array([1, 2, 3])

(3) 强制类型转换

>>> a = np.array([1.7, 1.2, 1.6])
>>> b = a.astype(int)  # <-- truncates to integer
>>> b
array([1, 1, 1])

(4) 四舍五入,但并不改变数组的数据类型

>>> a = np.array([1.2, 1.5, 1.6, 2.5, 3.5, 4.5])
>>> b = np.around(a)
>>> b                    # still floating-point
array([ 1.,  2.,  2.,  2.,  4.,  4.])
>>> c = np.around(a).astype(int)
>>> c
array([1, 2, 2, 2, 4, 4])

2、Different data type sizes - 不同数据类型的大小

有符号整型 int8 8 bits
int16 16 bits
int32 32 bits(32位平台上 int 型默认大小)
int64 64 bits(64位平台上 int 型默认大小)
无符号整型 uint8 8 bits
uint16 16 bits
uint32 32 bits
uint64 64 bits
浮点型 float16 16 bits
float32 32 bits
float64 64 bits(float 默认大小)
float96 96 bits, platform-dependent(与 np.longdouble 相同)
float128 128 bits, platform-dependent(与 np.longdouble 相同)
复数浮点型 complex64 two 32-bit floats
complex128 two 64-bit floats
complex192 two 96-bit floats, platform-dependent
complex256 two 128-bit floats, platform-dependent

 

 

 

 

 

 

 

 

 

 

 

 

二、结构化数据类型

sensor_code (4-character string)
position (float)
value (float)

 

 

>>> samples = np.zeros((6,), dtype=[(sensor_code, S4),
...                                 (position, float), (value, float)])
>>> samples.ndim
1
>>> samples.shape
(6,)
>>> samples.dtype.names
(sensor_code, position, value)

>>> samples[:] = [(ALFA,   1, 0.37), (BETA, 1, 0.11), (TAU, 1,   0.13),
...               (ALFA, 1.5, 0.37), (ALFA, 3, 0.11), (TAU, 1.2, 0.13)]
>>> samples     
array([(ALFA, 1.0, 0.37), (BETA, 1.0, 0.11), (TAU, 1.0, 0.13),
       (ALFA, 1.5, 0.37), (ALFA, 3.0, 0.11), (TAU, 1.2, 0.13)],
      dtype=[(sensor_code, S4), (position, <f8), (value, <f8)])

# 使用列名进行索引
>>> samples[sensor_code]    
array([ALFA, BETA, TAU, ALFA, ALFA, TAU],
      dtype=|S4)
>>> samples[value]
array([ 0.37,  0.11,  0.13,  0.37,  0.11,  0.13])
>>> samples[0]    
(ALFA, 1.0, 0.37)

>>> samples[0][sensor_code] = TAU
>>> samples[0]    
(TAU, 1.0, 0.37)

# 同时获取多列
>>> samples[[position, value]]
array([(1.0, 0.37), (1.0, 0.11), (1.0, 0.13), (1.5, 0.37), (3.0, 0.11),
       (1.2, 0.13)],
      dtype=[(position, <f8), (value, <f8)])

# 花式索引同样适用
>>> samples[samples[sensor_code] == ALFA]    
array([(ALFA, 1.5, 0.37), (ALFA, 3.0, 0.11)],
      dtype=[(sensor_code, S4), (position, <f8), (value, <f8)])

更多信息请参考:https://docs.scipy.org/doc/numpy/user/basics.rec.html

                             https://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html#specifying-and-constructing-data-types

三、maskedarray: dealing with (propagation of) missing data - 蒙面数组:缺失值的处理

对于浮点型数据来说,我们可以使用NaN来处理缺失值,但是面具可以处理任何类型的数据的缺失值。

>>> x = np.ma.array([1, 2, 3, 4], mask=[0, 1, 0, 1])
>>> x
masked_array(data = [1 -- 3 --],
             mask = [False  True False  True],    
       fill_value = 999999)

>>> y = np.ma.array([1, 2, 3, 4], mask=[0, 1, 1, 1])   # 0代表False,表示没有面具,也就是不会被当作不可用的数据
>>> y
masked_array(data = [1 -- -- --],
             mask = [False  True  True  True],
       fill_value = 999999)

>>> x + y
masked_array(data = [2 -- -- --],
             mask = [False  True  True  True],
       fill_value = 999999)

 

NumPy学习笔记:3、更加复杂的数组

标签:logs   org   浮点   dir   不能   .sh   相同   row   eve   

原文地址:http://www.cnblogs.com/steacyMAR/p/7202411.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!