Learn how to perform simple arithmetic. This video covers standard arithmetic, arithmetic multiplication, and matrix multiplication.

[Instructor] In this section, we're going to be talking about how to do simple arithmetic in Python. The benefit of the NumPy library is that it makes it really easy to do math on data that's stored in either arrays or matrices. Now, an array is just a one-dimensional container for elements that are all of the same data type. And a matrix is just a two-dimensional container for elements that are stored in an array. The basic arithmetic operators in Python are addition, subtraction, multiplication, and division. Let me give you an example of where NumPy can come in handy. Have you ever tried to use a spreadsheet application to perform mathematical operations on a data set that has more than 300,000 rows? What happened? If the application didn't crash, then it took a lot of time and effort to get the program to make the computation. With NumPy on the other hand, you can quickly and easily do mathematical and statistical operations on data sets, with even millions of records. Simply put, NumPy makes it easy to do math on large data sets. With this slide here, I just wanted to familiarize you with the operators that you will use in Python to achieve these arithmetic operations. So, we're starting out the coding demonstration with having imported NumPy and then also imported the random number generator from NumPy. It never looks good to see more than two digits after a decimal point, so let's ahead and limit the number of decimal places returned in this demonstration. So to do that, you just say: "np.set_printoptions," and then you pass in the parameter: "(precision=2)." And then we just only get two places after the decimal point. Okay, so you run that. Now, the first thing we're going to look at is how to do math with arrays. So, we of course need to create some arrays to do that math, and our first array will be array "a," and we'll set it equal to an array of six values, from one to six. Now to do that, we need to call the array function, which is: "np.array." And then we just pass in the list with the values that we want the array to contain, which would be: "1,2,3,4,5,6." And we can just print that out so we can see it. All right, great. And then, let's create the second object we will create will be a matrix. And we'll call it "b." And then we just call the "np.array" function. And this time, we're going to pass in two lists. So, the first one is going to contain the numbers "10,20,30." And then the next one will contain the numbers "40,50,60." Okay, and then we print that out. Very good. Okay. Now before going into actually doing the math, let's just create an array via assignment. I wanted to show you how to do that. So, let's this time use the random number generator in NumPy, and what we're going to do is we're going to create six random values. So, to set the seed for our random number generator, we say: "np.random.seed." And this is so you get the same numbers on your machine as that are showing randomly on mine. And then we just assign a value to the variable "c." This is going to create an array of six values. And, let's put a multiplier on here, we'll say 36 times the random numbers that are generated by the random number generator in NumPy. So we'll say: "np.random.randn," this is our function for generating random numbers, and then we'll pass in the number "6" here. And "6" just says, "Okay we want six values." And then we print this out. Okay, cool. So one thing I would like to point out here is that when we use the randn function, which is the random number generator in Python, what that actually is doing is it's generating both positive and negative random numbers. And let's create a fourth array, which is going to be called "d." So what we're going to do here is we are going to generate a series of sequential numbers between 1 and 34. And so, since it's a sequential series of numbers we can use the arrange function which would be "np.arrange." And then we just pass in the start and end number we want in our series, and then we can print this out. All right, so, now that you kind of understand how to create arrays and matrices, let's just start performing some math. First things first, let's just multiply the array "a" by the number 10. So, let's just look back at "a" here. So it's just 1-6, and we want to multiply each element of this array by the number 10. To do that, it's very easy, you would just say: "a*10," and run it. And then as you can see, each element here, each number in the array, has been multiplied by the number 10, so this is very straight-forward. Now, let's try an addition operation. So we'll say: "c+a." And, let's just kind of review, I'll just run this so we have an output. And, okay so what we have here, we have our "c" array and then we have our "a" array, so we would expect that we add these two it's going to be 9.22, right? And then you can see the first element here is "9.22." So it's very simple to do straight out arithmetic with arrays and matrices in Python. Now let's just do "c-a." It's the same exact functions so in this case we're going to have one less than 8.22. So, okay, "7.22," all of this makes perfect sense. Let's try "c*a." And if we go back up and just make sure it makes sense. "a" is a "1," "c" here is "8.22," so 8.22*1 is 8.22, so yeah. Okay, cool. So it's really, really simple as you can see. And if you wanted to do division here, you would just go "c/a." Okay, cool. And 8.22/1 is 8.22, this also makes sense.

###### Released

10/24/2019*Python for Data Science Essential Training*is one of the most popular data science courses at LinkedIn Learning. It has now been updated and expanded to two partsâ€”for even more hands-on experience with Python. In this course, instructor Lillian Pierson takes you step by step through a practical data science project: a web scraper that downloads and analyzes data from the web. Along the way, she introduces techniques to clean, reformat, transform, and describe raw data; generate visualizations; remove outliers; perform simple data analysis; and generate interactive graphs using the Plotly library. You should walk away from this training with basic coding experience that you can take to your organization and quickly apply to your own custom data science projects.

- Why use Python for working with data
- Filtering and selecting data
- Concatenating and transforming data
- Data visualization best practices
- Visualizing data
- Creating a plot
- Creating statistical data graphics
- Performing basic math and linear algebra
- Correlation analysis
- Multivariate analysis
- Data sourcing via web scraping
- Introduction to natural language processing
- Collaborative analytics with Plotly

## Share this video

## Embed this video

Video: Simple arithmetic