Diving into NumPy arrays

Python for MATLAB Users

Justin Kiggins

Product Manager

Calculations on NumPy arrays

print(radius)
[[1, 2, 3]]
type(radius)
 <class 'numpy.ndarray'>
area = np.pi * (radius ** 2)
print(area)
[3.14159265 12.56637061 
28.27433388]
Operation Operator
Addition +
Subtraction -
Multiplication *
Division /
Exponentiation **
Python for MATLAB Users

Math between NumPy arrays

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.]
Python for MATLAB Users

Summarizing data in NumPy arrays

import numpy as np

np.mean(revenue)
107.5

Some useful NumPy functions:

  • np.min() returns smallest value in array
  • np.max() returns largest value in array
  • np.sum() returns sum of all values in array
Python for MATLAB Users

Let's practice!

Python for MATLAB Users

Preparing Video For Download...