1. ndarray基本属性:arr.dytpe , arr.shape
2. 数组构建
3. 数组类型 type(arr)
4. 数组属性修改:arr.reshape(…), arr.astype(‘…’)
5. str.format()函数
6. np.dtype()结构化数据类型
python内置类型:
bool , int, float, complex, str, tuple, list, dict, setnumpy内置类型:
bool_,
int8(i1) / int16(i2) / int32(i4) / int64(i8)…,
uint8(u1) / uint16 / uint32(u4) / uint64…, (int 和uint 值域不同,且uint不含负数)
float16() / float32 / float64,
complex64 / complex128,
str_(s20 长度20的字符串)自定义类型:(常用1,2,6,7)
t = np.dtype(T)
a = np.array([…], dtype=t)
或
a = np.array([…], dtype=T)1) T:python内置类型 或 numpy内置类型
t = np.dtype(int) # python内置类型, 没有 ' ' t = np.dtype(np.int32) # numpy内置类型
2)T:类型字符串
t = np.dtype('int') # 有引号 ' ' t = np.dtype('int32')
3)T:类型字符编码 (不推荐使用)
t = np.dtype(‘>(2,3)4i4’)
说明:>: 大端字节序 (2,3): 维度 4: 分量数 i: 分量类型 4: 分量字节数
4) T:tuple (变长类型,长度) ….不推荐
t = np.dtype((np.str_, 14))5) T:tuple (定长类型,维度) ….不推荐
t = np.dtype((int32,5))6) T:逗号分隔的多个类型字符串
t = np.dytpe(‘U14 , i4‘)7)T: 结构化数据类型 [(名称,类型,维度),(字段描述),…] …..需要灵活掌握
t = np.dtype([(‘name’, np.str_,14), (‘age’, np.int32)])
NOTE: 相当于MySQL中给字段名进行类型设置
f = np.array([('abc', 1243) ,('def', 3456)], dtype = 'U14, i4')
f
Out[26]:
array([('abc', 1243), ('def', 3456)],
dtype=[('f0', '<U14'), ('f1', '<i4')])
f[1][0]
Out[27]: 'def'
f.dtype
Out[28]: dtype([('f0', '<U14'), ('f1', '<i4')])
g = np.array([('abc', 1243) ,('def', 3456)], dtype = [('name',np.str_,14),('age',np.int32)])
g
Out[30]:
array([('abc', 1243), ('def', 3456)],
dtype=[('name', '<U14'), ('age', '<i4')])
g[1]['name']
Out[31]: 'def'
- 函数dtype() :结构化数组类型
结构化数据类型
In [3]: student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')])
In [4]: print student
[('name', 'S20'), ('age', 'i1'), ('marks', '<f4')]
将其应用于 ndarray 对象
In [5]: a = np.array([('abc', 21, 50),('xyz', 18, 75)], dtype = student)
In [6]: print a
[('abc', 21, 50.) ('xyz', 18, 75.)]
采用文件名称来访问name, age, marks列的内容
In [7]: print a['name']
['abc' 'xyz']
In [8]: print a['marks']
[ 50. 75.]
In [9]: print a['age']
[21 18]
- code:
import os
import sys
import numpy as np
def main (argc, argv, envp):
one_dim = np.arange(1,5)
one_dim_list = list(one_dim)
print('array output:', one_dim) # array 输出: [1 2 3 4]
print('list output:', one_dim_list) # list 输出: [1, 2, 3, 4]
print('容器类型:', type(one_dim)) # 容器类型 : <class 'numpy.ndarray'>
# 数组array的基本属性
print('元素类型:', one_dim.dtype) # 元素类型 : int32
print('容器形状:', one_dim.shape) # 容器形状 : (4,)
# array.reshape(x,y,z) 重新shape数组形状
three_dim = np.arange(1,25,1).reshape(2,3,4)
print('数组reshape:', three_dim.shape) # 数组形状 : (2, 3, 4)
# array.astype('float/int..') 修改元素类型
three_dim = np.arange(1,25,1).reshape(2,3,4).astype('float')
print('数组astype:', three_dim.dtype) # 元素类型 : float64
print('three_dim:', '\n', three_dim)
# 循环输出3D-array数组元素
print('\n','循环输出3D-array数组元素:')
for i in range(three_dim.shape[0]): # in range(2): three_dim.shape[0]==2
for j in range(three_dim.shape[1]): # range 一定不能漏掉
for k in range(three_dim.shape[2]):
# str.format函数, int()函数:
# {:^5d}.format(int) 整数 宽度为5 中间对齐
print('{:^5d}'.format(int(three_dim[i,j,k])), end = '/')
print('-', end = '/')
# {:10}'.format() 浮点数 宽度为10 右对齐
print('{:10}'.format(three_dim[i,j,k]), end = '')
print() # 内层循环结束后换行
print() # 内层循环结束后换行
return 0
if __name__ == '__main__':
sys.exit(main(len(sys.argv), sys.argv, os.environ))
输出结果:
array output: [1 2 3 4]
list output: [1, 2, 3, 4]
容器类型: <class 'numpy.ndarray'>
元素类型: int32
容器形状: (4,)
数组reshape: (2, 3, 4)
数组astype: float64
three_dim:
[[[ 1. 2. 3. 4.]
[ 5. 6. 7. 8.]
[ 9. 10. 11. 12.]]
[[ 13. 14. 15. 16.]
[ 17. 18. 19. 20.]
[ 21. 22. 23. 24.]]]
循环输出3D-array数组元素:
1 /-/ 1.0 2 /-/ 2.0 3 /-/ 3.0 4 /-/ 4.0
5 /-/ 5.0 6 /-/ 6.0 7 /-/ 7.0 8 /-/ 8.0
9 /-/ 9.0 10 /-/ 10.0 11 /-/ 11.0 12 /-/ 12.0
13 /-/ 13.0 14 /-/ 14.0 15 /-/ 15.0 16 /-/ 16.0
17 /-/ 17.0 18 /-/ 18.0 19 /-/ 19.0 20 /-/ 20.0
21 /-/ 21.0 22 /-/ 22.0 23 /-/ 23.0 24 /-/ 24.0
[Finished in 1.3s]
- str.format() 参考:
http://www.runoob.com/python/att-string-format.html