Code Efficiency and Vectorization

Compare code efficiency between Python built-in functions and component-wise programming.

Machine learning is about working with large collections of data. Such data is kept in databases, spreadsheets, or simply in text files, but to work with it, we load it into arrays. Since we define operations on such arrays, it is better to treat these arrays as vectors, matrices, or, generally, as tensors. Traditional programming languages such as C and Fortran require us to write code that loops over all the indices in order to specify operations that are defined on all the data. For example, let us define two random nnn*n matrices with the NumPy random number generator for uniformly distributed numbers as follows:

Get hands-on with 1200+ tech skills courses.