Learn to create universal functions that operate upon NumPy arrays in this video tutorial by Charles Kelly. These are explained in the context of computer science and data science to technologists and students in preparation for machine learning, applied
- [Narrator] The universal functions file in your exercise file's folder, is pre-populated with an import statement, and a definition for a function, named truncated binomial. According to NumPy's documentation, a universal function, is a function that operates on NumPy arrays in an element by element fashion. Universal functions support array broadcasting, typecasting, and other standard features. A universal function is a vectorized wrapper for a Python function, and has a fixed number of scalar inputs, and produces a fixed number of scalar outputs.
I'll explain this via the function definition for truncated binomial. It expands the binomial x plus one to the third power and subtracts the value of x cubed. And we can note that two asterisks are the symbol for the exponentiation operator. Before you use the cells in this file, you need to go to the cell menu, and say run all. I'm copying a snippet from your exercises file.
We'll perform an elementary test by evaluating our function at four. Four plus one raised to the third power equals 125. Four cubed equals 64, and their difference is equal to 61. When you execute this cell, nothing is returned. When you're using a testing function, this is exactly the result that you want. Suppose however, that I take this line, copy it, and set the value we are testing to, to 65.
In this case we'll generate an error. This is the purpose of the testing function. Now this is certainly not a complete introduction to unit testing. But it gives us an idea of how to use testing dot assert equal within the context of NumPy arrays and NumPy functions. In order to use this Python function with NumPy arrays, we need to convert it to a universal function. NumPy's frompy function, function provides this conversion.
In this snippet, which I've copied from your exercise files, we're using np dot frompyfunc to translate our function, truncated binomial into a NumPy function, a universal function. The second parameter is the number of arguments that we are passing to our function, in this case one. The thirds parameter is the number of scalars that our function will return. Again, this is one.
Let's create a test array that we can use to test our universal function. I'll use NumPy's arange function and set the parameter value to ten. Now what I'll do is I'll take my NumPy function and I'll pass test array to that function. And although we won't do all the math, for example we have zero, one, two.
So if we look at this and we take two plus one is three, cube it is 27, two cubed is eight, subtract 27 minus eight, and the value is 19. If you want you can check the calculations for the other elements in this array. Let's create an array that we can use to test our universal function. What I'll do is I'll use NumPys outer function and pass test array to it as both parameters.
We can display test array. What we have here is the product of each element in test array, multiplied by the element in test array. What this means is we've created every possible product of all of the elements in test array times test array. Now let's use my NumPy function, and pass the big test array to it.
What we've done is we've used our universal function, and our big test array to create every possible combination of our defined function with the elements of big test array. In this video we learned how to create functions that operate upon NumPy arrays on an element by element basis.
- Using Jupyter Notebook
- Creating NumPy arrays from Python structures
- Slicing arrays
- Using Boolean masking and broadcasting techniques
- Plotting in Jupyter notebooks
- Joining and splitting arrays
- Rearranging array elements
- Creating universal functions
- Finding patterns
- Building magic squares and magic cubes with NumPy and Python