Array Function Workout
- 02:24
Practice using Array Functions in Python.
Downloads
No associated resources to download.
Glossary
NumPy Numpy arrays PythonTranscript
In your Jupyter Notebook, you're going to see arrays defined for the current and target stock prices shown in the Excel table below. And notice that we're looking at three tech companies, three financial companies, and three healthcare companies. And each of those industries is organized into a column. Start by executing that cell to define the arrays already given to you in your Jupyter Notebook, and then complete the following steps. First, create a new array called price increases by subtracting current prices from target prices. Then print the new array price increases, create a new array called returns by dividing price, increases by current prices, and round the result to three decimal places. Print the new array returns, then print the maximum value in returns. Finally, print the mean returns for each industry column rounded to three decimal places. And remember, to do that, you're gonna have to use the axis argument.
The first step is to execute this code cell so that current prices and target prices are defined as new arrays. Now let's take a look at the code that I've written here. First, I'm creating a new array called price increases, which is target prices minus current prices. Then I'm printing that new array price increases, and here I just have a little spacer for formatting so that it's easier to read after I print it out. Then I'm creating a new array called returns. And to do that, I'm using the NumPy function round, applied to price increases divided by current prices. And then this 3 tells the round function to round this calculation to three decimal places. And then I'm printing returns and a spacer. I'm using the NumPy max function to get the maximum value in the whole array returns. And then finally, I'm using the mean function to get the mean of returns, and I'm applying that to axis zero, which is across all columns. And then I've applied the round function to that, to round this calculation to three decimal places.
And there's your result.