1.Numpy的学习
1.1ndarray对象
用于存放同类型元素的多维数组
import numpy as np
a = np.array([1, 2, 3])
print(a)
[1 2 3]
使数组多余一个维度
import numpy as np
b = np.array([[1,2],[3,4]])
print(b)
[[1 2]
[34]]
设置数组的最小维度
c = np.array([1,2,3,4],ndmin=2)
print(c)
[[1 2 3 4]]
2.Numpy数据类型
使用标量类型
import numpy as np
dt = np.dtype(np.int32)
print(dt)
int32
数据类型用字符串代替(int8 对应i1,int16对应i2,……)
import numpy as np
dt = np.dtype(
'i4')
print(dt)
int32
字节顺序的标注
import numpy as np
dt = np.dtype(
'<i4')
print(dt)
int32
结构化数据类型的创建:类型字段与对应的实际类型
import numpy as np
dt = np.dtype([(
'age',np.int8)])
print(dt)
[('age', 'i1')]
将数据类型应用于ndarray对象
import numpy as np
dt = np.dtype([(
'age', np.int8)])
a = np.array([(
10,), (20,), (30,), ], dtype=dt)
print(a)
[(10,) (20,) (30,)]
类型字段名可用于存取实际的age列
import numpy as np
dt = np.dtype([(
'age',np.int8)])
a =np.array([(
10,),(20,),(30),],dtype = dt)
print(a['age'])
定义结构化数据类型student,包含字符串字段name,整型字段age和浮点字段marks,将这个数据类型应用到对象student
import numpy as np
student = np.dtype([(
'name', 'S20'), ('age', 'i1'), ('marks', 'f4')])
print(student)
[('name', 'S20'), ('age', 'i1'), ('marks','
将定义的数据类型应用到对象中
import numpy as np
student = np.dtype([(
'name', 'S20'), ('age', 'i1'), ('marks', 'f4')])
a = np.array([(
'abc',21,50),('xyz',18,75)],dtype = student)
print(student)
[(b'abc', 21, 50.) (b'xyz', 18, 75.)][if !supportAnnotations][a1][endif]
每个内建类型有其唯一对应的字符代码,如b表示布尔型,i表示整型,f表示浮点型。
[if !supportAnnotations]
[endif]
[if !supportAnnotations]
[endif][if !supportAnnotations][endif]
[if !supportAnnotations][a1][endif]显示错误,b是什么?同时为什么没有0?
[if !supportAnnotations]
[endif]
�
网友评论