深入了解 NumPy 数组

面向 MATLAB 用户的 Python

Justin Kiggins

Product Manager

在 NumPy 数组上进行计算

print(radius)
[[1, 2, 3]]
type(radius)
 <class 'numpy.ndarray'>
area = np.pi * (radius ** 2)
print(area)
[3.14159265 12.56637061 
28.27433388]
操作 运算符
加法 +
减法 -
乘法 *
除法 /
幂运算 **
面向 MATLAB 用户的 Python

NumPy 数组间的运算

type(revenue)
<class 'numpy.ndarray'>
print(revenue)
[[100, 114, 96, 120]]
type(cost)
<class 'numpy.ndarray'>
print(cost)
[102. 104. 101. 102.]
profit = revenue - cost
print(profit)
[-2. 10. -5. 18.]
面向 MATLAB 用户的 Python

汇总 NumPy 数组中的数据

import numpy as np

np.mean(revenue)
107.5

一些常用的 NumPy 函数:

  • np.min() 返回数组中的最小值
  • np.max() 返回数组中的最大值
  • np.sum() 返回数组所有值之和
面向 MATLAB 用户的 Python

让我们练习一下!

面向 MATLAB 用户的 Python

Preparing Video For Download...