Download Numerical Computing with NumPy: A Comprehensive Guide to Arrays and Vectorization and more Slides Programming Languages in PDF only on Docsity!
Numerical Computing with Numpy
Python interpreter has built-in data structures.
NumPy adds valuable functionality to existing structures.
This session focuses on NumPy. à Provides multidimensional array object which stores homogeneous or heterogeneous data arrays.
It also Supports code vectorization.
Arrays of Data
Python has useful and flexible general data structures, like lists.
Lists can have higher memory usage and slower performance.
Scientific and financial applications require high-performing operations on special data structures, like arrays.
Arrays structure objects of the same data type in rows and columns.
One-dimensional arrays represent vectors, while multidimensional arrays can represent matrices, cubes, or n-dimensional shapes.
Arrays with Python Lists
Arrays can be built using built-in data structures.
List objects are well-suited for this purpose.
A simple list can represent a one-dimensional array.
Since list objects can contain arbitrary other objects à they can also contain other list object and can be used to construct two- and higher-dimensional arrays
Arrays with Python Lists
Accessing the matrix elements
Nesting can be pushed further for even more general structures:
The Python array Class
Consider the following code, which instantiates an array object out of a list object:
The Python array Class
Trying to append an object of different data type than the one specified raises a TypeError:
The Python array Class
The data type of the array object is of importance when reading the data from disk:
Regular Numpy Arrays
List objects can be used for composing array structures.
Lists are not specifically designed for this purpose.
List class has a broader and more general scope.
Array class is more specialized for working with arrays of data.
A truly specialized class could greatly benefit array-type structures handling.
numpy.ndarray is such a class, built with the specific goal of handling dimensional arrays both conveniently and efficiently, i.e. in a highly performance manner
Regular Numpy Arrays
A major feature of the ndarray class is the multitude of built-in methods:
Regular Numpy Arrays
Another major feature is the (vectorized) mathematical operations defined on ndarray objects:
Regular Numpy Arrays
Universal functions are another important feature of the Numpy Package
Operate on ndarray objects and basic Python data types.
Applicable to Python float objects, but with reduced performance.
Performance advantage when using math module for basic Python data types.
Regular Numpy Arrays
Cont..
Multiple Dimensions
Cont.
Multiple Dimensions
Multiple ways to initialize ndarray objects.
np.array used when all elements are available.
Alternative approach: instantiate ndarray first.
Populate ndarray later with results from code execution.