Numpy Arrays Python Tutorial #3 - Array Math

Tech With Tim · Beginner ·⚡ Algorithms & Data Structures ·7y ago

Key Takeaways

This video tutorial covers array math operations using the NumPy library in Python, including addition, subtraction, multiplication, and division, as well as dot products and matrix operations. The tutorial also covers array transposition and summation using NumPy's built-in functions.

Full Transcript

hey guys and welcome back to the third video in my numpy tutorial series in today's video I'm gonna be going over all of the different math operations pretty well that we can do on different arrays of different sizes I'm going to be talking about dot products inner products element wise operations a bunch of fun stuff so make sure you guys stick around for the whole video so anyways I've just got two arrays here x and y and what I want to do is I want to add the elements so like 1 plus 5 2 + 6 3 + 7 4 + 8 so on so intuitively you think you can just do this x + y and in that case you would be correct so you can apply the standard operations addition subtraction multiplication and division which I will go through now there's another way that you can do this as well and you can do NP don't add and you can do X comma Y like so and you get the exact same result again if we want to store these values we need to store them in a new array so if I do something like Z equals prints ad then we have that now stored in a new array called set next one is subtract so let's do the same thing here so X minus y you can see we get negative 4 negative 4 negative 4 negative 4 now same thing if we do NP dot subtract x + y and the exact same thing now it's the identical for multiplication and Submission but I do want to show you multiplication because some of you that know a bit about linear algebra might be confused on how this multiplication works so if I do X multiplied by Y any of you that don't know linear algebra gonna say okay I'm just gonna get 1 multiplied by 5 2 by 6 3 by 7 and you'd be correct in that case this is because we're doing element wise multiplication now what that means is we're simply just multiplying all of the corresponding elements so like Row 1 column 1 Row 2 column 2 so on we're just multiplying those together and stirring them in a new array if we want to do matrix multiplication which I'm going to show later it's a different command now same thing here we go multiply and that X Y then we get the same thing and division I don't think I need to talk about it too much we get the exact same thing element-wise and you can do em pedo divine now last one to show here and this one there's no like short form for it you have to type NP dot s QR t if you want to get the square root of every element so for example at X and we get the square root so these are useful operations and they're good to know and it's also neat that you can ignore using this like dot multiplied I'll subtract and you can simply just apply the operation like you would think you'd be able to do okay so now let's talk about dot products so I'm going to create two new arrays here I'm going to V is equal to NP ray and in this case let's just give it values of nine ten let's go W is gonna be equal to NP dot array values uh oops what am I saying here eleven and thirteen just do this why not okay so we have V and W now if I want to take the dot product of these two vectors I could go through the tedious picking out each element in the array and doing it myself but like you might assume numpy has a trick for that so we can simply do something like V dot W in this case we're gonna get a scalar which is two to nine now if you guys don't know anything about linear algebra and you don't know what this is don't worry about it simply what this dot product does is it's gonna multiply the corresponding value so we're gonna say nine x 11 plus 10 x 13 and that's the value that we get here 2 to 9 now another way we can do this is NP dot dot and then in here we'll put our two so we say V and W like so and we're gonna get the exact same answer now this dot product is only going to give us a scalar if we're multiplying vectors of the same size so we have a vector or I'm saying vector but really is an array as well of 1113 so this is a 1 by 2 and this is a 1 by 2 so that's gonna allow us to give a dot product that gives us a scalar now i want to show you an example of what happens if i multiply vectors matrix is whatever arrays of different size and show you what we get so if I do something like let's see here NP dot dot and I remember I made X&Y up here we have multiple dimensions in here so we have two rows and two columns of x and y we actually get what's known as the inner product of these matrices matrix --is so we didn't simply just dot product like all the vectors in here together and add them we actually did the inner product now I'm not gonna explain the inner product cuz now we're getting too far until linear algebra but that's how you can do that with matrixes if you guys know what those are okay now another thing that we can do is we can transpose a matrix or a vector and what that simply means to do is to write all of the rows in columns and all of the columns and rows so like vice versa so you're pretty much just flipping all of the rows into columns so I'll show you what I mean here so I'm just gonna print X and you can see that we get a matrix that goes 1 2 3 4 now if I do X dot T and this is all you have to do is just very simple this is going to transpose our matrix and you can see that we now get 1 3 2 4 so this column has been written as the row and then this column has been written as the second row again if we wanted to well reverse the operation so we said like I don't know H equals X dot T that H is gonna be our transpose matron we go 1 reverse' and you know HT and we're gonna get back to 1 2 3 4 now the last one I want to show is some now we can numpy has a built in some functions already starting and MP dot some and just give it an array here so I'm gonna give it X and you can see we get a sum of 10 plus 1 plus 3 plus 2 plus 4 all the way around 1 plus 2 Plus 3 plus 4 gives us 10 and if we want to sum just one row or or just all the rows or all the columns or so on like that we can actually do something which is really cool and suppose NP dot some will give it our array X and we can specify an axis that we want some and I'll show you what this prints up so the axis is gonna be which column you want to sum so if I do that you can see we get 4 & 6 so we have our column here which is gonna be or sorry our row actually because I said axis equals zero naught x equals 1 excuse me there so we get 1 3 which is the first sum 4 and we get 2 4 which is the second sum of 6 and it stores that in well a new array so if now if I want to sum all the columns not the rows I'll do axis is equal to 1 and you can take it 3 7 so 1 plus 2 3 plus 4 and we get 3 7 like that now I encourage you guys to play around with a lot of stuff I've shown here there's a lot of cool things you can do with them and I'm only kind of showing the basis because I want you guys to discover them on your own anyways that's been it for this video if you guys enjoyed please make sure you leave a like and subscribe and I will see you again in the next one [Music]

Original Description

In this video I explain how to implement arrays in python using the module numpy. This is a module you must download as it is not built into python. Numpy is extremely useful for using data structures and multi-dimensional lists. It has some built-in methods and properties that will save you a lot of time. In this specific tutorial I explain and show many different math operations and methods you can use on numpy arrays. If you want to learn more about numpy and some more advanced examples stay tuned for the rest of the videos and subscribe! Text-Based Tutorial: https://techwithtim.net/tutorials/python-module-walk-throughs/numpy-module/array-math/ Twitter: https://twitter.com/TechWithTimm Want To Support This Channel? Bitcoin: 1PbkAYLFaJBgjbKn2ptGyBz65xWN8hJgBU Ethereum: 0xdd42dbbdba60f7163fc7a840e189474b6e8bfcad Ripple: rD4arM9CVjQWqi8f1kxdpCgkCgEkqBgtud Please leave a LIKE and SUBSCRIBE for more content! Tags: - Tech - Tech With Tim - Programming - Coding - Pygame - Python Tutorials - Numpy - Numpy Arrays - Arrays in python - Numpy module - Nump tutorial - How to use an array in python - Array math - Math with arrays - Matrixes python
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tech With Tim · Tech With Tim · 0 of 60

← Previous Next →
1 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

This video tutorial teaches array math operations using NumPy, including dot products, matrix operations, transposition, and summation. It covers the basics of NumPy and its applications in linear algebra and data structures.

Key Takeaways
  1. Import the NumPy library
  2. Create arrays using NumPy's array function
  3. Perform array math operations (addition, subtraction, multiplication, division)
  4. Compute dot products using NumPy's dot function
  5. Transpose arrays using NumPy's transpose function
  6. Sum arrays using NumPy's sum function
  7. Specify axes for summation
💡 NumPy provides an efficient and convenient way to perform array math operations, including dot products and matrix operations, which are essential in linear algebra and data structures.

Related AI Lessons

Bloom Filters, Explained Properly
Learn how Bloom filters work and their benefits, including tiny memory and blazing speed, in exchange for potential false positives.
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Learn how prefix sums enable instant range queries in arrays, boosting performance in various applications
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
A simple math question can destroy a developer's interview, highlighting the importance of being prepared for unexpected questions
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Learn to remove duplicates from a sorted array using the two pointers technique, improving from brute force to optimized solutions
Medium · Python
Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →