1.初识numpy
‘‘‘ndarray:
N维数组类型ndarray,它描述了相同类型的“items”集合‘‘‘
importnumpy#存储数据
score =numpy.array([
[80,89,86,67,79],
[78,97,89,67,81],
[90,94,90,67,69],
[91,91,90,67,69],
[76,87,75,67,86],
[70,79,84,67,84],
[94,92,93,67,64],
[86,85,83,67,80]
])print(score,"\n",type(score))
View Code
2.ndarray与python原生list对比
importnumpyimporttimeimportrandom
a=[]for i in range(100000000):
a.append(random.random())
t1=time.time()
sum1=sum(a)
t2=time.time()
b=numpy.array(a)
t4=time.time()#numpy求和
sum3 =numpy.sum(b)
t5=time.time()print(t2-t1,t5-t4)
View Code
3.ndarray的优势
ndarray的优势
1.存储风格
ndarray - 相同类型 - 通用性差 顺序存储
list - 不同类型 - 通用性强 链式存储
2.并行化计算
ndarray支持向量化运算
3.底层语言
Numpy底层使用C语言编写,内部接触了GIL(全局解释器锁),可支持多处理机,
其对数据的操作速度不受Python解释器的限制,效率远高于纯Python代码
4.ndarray的属性
importnumpy#ndarray的属性
score=numpy.array([
[80,89,86,67,79],
[78,97,89,67,81],
[90,94,90,67,69],
[91,91,90,67,69],
[76,87,75,67,86],
[70,79,84,67,84],
[94,92,93,67,64],
[86,85,83,67,80]
])#数组的行列 (8, 5)
print(score.shape)#数组的维度
print(score.ndim)#数组的元素个数
print(score.size)#数组的类型 int32
print(score.dtype)#数组中每个元素所占的字节数 4
print(score.itemsize)#数组中各元素的虚部
print(score.imag)
View Code
5.ndarray的形状
名称描述
bool_
布尔型数据类型(True 或者 False)