Introduction to NumPy
NumPy, or Numerical Python, provides additional flexibility in working with data than standard Python lists. The foundational element of NumPy are arrays, and the main structure found in NumPy is the ndarray
.
Arrays can hold a collection of the same data type and can be one-dimesional, also called a vector, or multi-dimensional, such as a matrix. The benefits of NumPy arrays are efficient element access and data manipulation / transformation.
The basics of NumPy arrays
At its most straightforward level, arrays can be created using a Python list, such as
|
|
Output
[8 5 3 2 1 1]
Multi-dimensional arrays can be created applying the same method using mutiple nested lists, such as the following two-dimensional array (or matrix)
|
|
Output
[[5 1 3]
[1 8 7]
[3 7 9]]
Basic array operations
Many functions can be performed on arrays, such as determing attributes of the array, sorting, spliting or combining arrays, and, of course, numerical operations.
We will introduce a brief selection of the broad array (no pun intended) of functions which provide useful insights into NumPy arrays.
To start, we can determine the number of dimensions and the number of elements per dimension of an array using the ndim
and shape
attributes.
|
|
Output
2
(3, 3)
Sorting is performed using the sort
function, such as
|
|
Output
[1 1 2 3 5 8]
An array can be broken into multiple arrays using array_split
or multiple arrays can be merged using concatenate
. Let’s review examples of these
|
|
Output
[array([8, 5]), array([3, 2]), array([1, 1])]
|
|
Output
[[5 1 3]
[1 8 7]
[3 7 9]]
Lastly for this introduction, arthimetic functions can be performed on an array as a whole, or for a specific dimension.
|
|
Output
Mean of array2: 4.888888888888889
Standard Deviation of array2: 2.8458329944145997
Sum of array2: 44
|
|
Output
Mean of elements in axis 0 of array2: [3. 5.33333333 6.33333333]
Mean of elements in axis 1 of array2: [3. 5.33333333 6.33333333]
Check Your Understanding
True or False: Numerical analyses can only be performed on NumPy arrays as a whole.