立即学习:https://edu.csdn.net/course/play/6861/326755?utm_source=blogtoedu
numpy 模块
1、数组的构造以及其优势
2、常用的数学函数和统计函数
傅里叶变化也是在numpy模块中实现的
列表是很灵活的一种存储单元,但是在有些时候在计算的时候还是不够灵活
举例:
#身高
height = [176,158,163,177,172,169]
#体重
weight = [82,61,69,89,78]
#BMI指数(身高质量指数=体重(kg)/身高(m)的平方)
BMI = weight/(height/100)**2
运行结果如下:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-1-d19406b0c4f6> in <module> 4 weight = [82,61,69,89,78] 5 #BMI指数(身高质量指数=体重(kg)/身高(m)的平方) ----> 6 BMI = weight/(height/100)**2 TypeError: unsupported operand type(s) for /: 'list' and 'int'
说明:列表知识一种容器,不具备任何计算能力。
#解决方案 (将两个列表中对应的元素取出来做运算)
BMI =[]
#身高
height = [176,158,163,177,172,169]
#体重
weight = [82,61,69,89,78,44,44,54]
for i in range(len(height)):
BMI.append(weight[i]/(height[i]/100)**2)
print(BMI)
有没有更好的办法:
一维数组:
import numpy as np
BMI =[]
#身高
height = [176,158,163,177,172,169]
#体重
weight = [82,61,69,89,78,44]
Height = np.array(height)
Weight = np.array(weight)
BMI = Weight/(Height/100)**2
BMI
结果如下:
array([26.47210744, 24.43518667, 25.97011555, 28.40818411, 26.36560303, 15.40562305])
说明:借助array函数可以将列表或者元组转换为数组