Python's @ Operator: The Key to Better Readability in Matrix Operations

NeuralNine · Beginner ·💻 AI-Assisted Coding ·3y ago

Key Takeaways

The video explains the use of Python's @ operator for matrix multiplication, enhancing code readability in AI coding applications.

Full Transcript

what is going on guys welcome back in today's video we're going to discuss one of the not so well-known python operators which is the matrix multiplication operator so let's get right into it [Music] all right so there's this python operator that a lot of you guys have probably never used before or maybe even never seen before and this is the operator for matrix multiplication now what's the idea of matrix multiplication it's quite simple we're going to import numpy S and P by the way if you don't have numpy you can just install it via the command line pip install numpy and the basic idea is the following we have a matrix A and P the dot matrix let's say one two in the First Dimension we're in the first block here in the first row of The Matrix and then three four in the second row and then we're going to copy that and we're going to say that we have also Matrix B and Matrix B is let's do 0 5. 10. 15. and the idea of a matrix multiplication now is that the Matrix c um the first the first row and First Column of C will be determined by taking those two and those two values and combining them in a certain way and how you do that I'm going to go through the manual process here you can skip that if you're not interested in that um how you do that is let's define here first of all the whole thing is none none none none um the basic idea is that the first row and First Column of C is going to be equal to the first row and First Column of a times the first row and the First Column of B and then we add to this the first row second column of a and you do the other thing on B which is you take the first uh the second the second row uh and the first column so to get this value here we take those two and those two so it's like a coordinate system a targets the row b targets the column and this is what you get as a result here now we can copy that and we can change this for the first row second column second row First Column second row second column and the basic idea is always the same you want to Target this value you take the first row uh so the a matrix specifies which Row in the B Matrix specifies which column so for this one now we're going to go again first row but we're going to go second column so what we want to do here is we want to say a stays the same basically but for B we're now going to Target the second column uh which means we're going to say 0 1 and 1 1. and then for this one so for the for the second row First Column we're going to say okay second row on a first column on B so we're going to say second row on a oh sorry and for beat stays the same and then for the last one for second row second column we want to Target the second row with a and the second column with B so we want to say one zero zero one one one one one that's a basic idea now if we print that if we print C you're going to see the result of the matrix multiplication so one two three four times zero five ten fifteen results in 20 35 40 75 that's the result of the matrix multiplication now I'm not going to go into all the details when this is necessary when this is useful just know that machine learning is basically linear algebra and linear algebra is matrix multiplication vectors all that stuff so if you're interested in machine learning you should also know about matrices and matrix multiplication or linear algebra in general but for this video here now this is just the definition of what a matrix multiplication is and with numpy we can do that way simpler by just saying print NP dot matte mall for matte multiplication for matrix multiplication and then we can specify A and B has two parameters and the result is going to be the same 20 35 40 75 that's the result of the matrix multiplication now this is not the same as calling the NP dot function this is because the NP dot function behaves differently when it comes to higher dimensions and the matte multiplication or the mat mole function or method also does not support multiplication with the scalar so those are two different methods matmo is the actual matrix multiplication now the operator this is what this video is about the operator now is the at operator like the male symbol so a at B is a matrix multiplication a matrix multiplication B so that's the same as doing that we can see that this is the case by just printing a at B and then you can see that this results in the same thing and basically again this is just syntactic sugar like a lot of stuff that we get with the different python versions this was introduced in um in Python 3.5 um and this is just making things more readable if you know that this is a matrix multiplication this is way more readable than uh constantly using NP matmo and I have actually an example for you guys I I'm going to copy paste this here this is a linear regression from scratch so this is a simple class that has a fit and predict method here and this is the linear regression implemented from scratch using this matrix multiplication operator here and then I also have the same thing using the NP matte Mall method and you can see that this is way less readable down here because you have the thing being called here three times in a row and in nested calls and you don't really know what is being multiplied by what right now you have to analyze where the commas are you have to analyze which bracket belongs to which function call and stuff like that here you exactly know okay this year is being multiplied with this this whole thing here is being multiplied with that and then this is multiplied by y That's the basic idea I also have prepared this here as two images so that you can see the difference here and that's basically all this is about this is not about performance this is not faster than using the function this is the exact same thing basically this operator calls the function math mole so it's the exact same performance but you have way better readability because you can see here clearly what is being multiplied until you have some nested function calls that you don't really that you have to analyze a little bit closer to see what is actually happening here so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you next video and bye [Music]

Original Description

Today we will learn about the "@" operator, which is used for matrix multiplication. ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾ 📚 Programming Books & Merch 📚 🐍 The Python Bible Book: https://www.neuralnine.com/books/ 💻 The Algorithm Bible Book: https://www.neuralnine.com/books/ 👕 Programming Merch: https://www.neuralnine.com/shop 🌐 Social Media & Contact 🌐 📱 Website: https://www.neuralnine.com/ 📷 Instagram: https://www.instagram.com/neuralnine 🐦 Twitter: https://twitter.com/neuralnine 🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/ 📁 GitHub: https://github.com/NeuralNine 🎙 Discord: https://discord.gg/JU4xr8U3dm 🎵 Outro Music From: https://www.bensound.com/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from NeuralNine · NeuralNine · 0 of 60

← Previous Next →
1 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video teaches the use of Python's @ operator for matrix multiplication, making code more readable and efficient. It is particularly useful for beginners in AI coding. By watching this video, viewers can improve their Python skills and learn how to write more readable code.

Key Takeaways
  1. Import necessary Python libraries
  2. Define matrices for multiplication
  3. Use the @ operator for matrix multiplication
  4. Compare the result with other multiplication methods
💡 The @ operator in Python provides a concise and readable way to perform matrix multiplication, making it an essential tool for AI coding applications.

Related Reads

📰
The Secret Sauce of Apple Silicon: Why Unified Memory is a Game-Changer for AI If you’ve spent…
Learn how Apple's Unified Memory boosts AI performance, especially for large language models and generative AI, and why it's a game-changer for professionals
Medium · Machine Learning
📰
The Secret Sauce of Apple Silicon: Why Unified Memory is a Game-Changer for AI If you’ve spent…
Learn how Apple's Unified Memory boosts AI performance, especially for large language models and generative AI, and why it's a game-changer for these applications
Medium · NLP
📰
We Gave Our Engineering Team a Memory — Here’s How PRECOG Uses Cognee
Learn how PRECOG uses Cognee to build predictive engineering intelligence and enhance their engineering team's capabilities
Medium · Startup
📰
You’ve Had the Same GPU Rumor Tab Open for Eight Months, and NVIDIA Has Never Once Confirmed the…
Stop waiting for GPU rumors and focus on building with current technology
Medium · Machine Learning
Up next
Copilot Cowork: Setup, Skills, Plugins & Pricing
Matt Tutorials
Watch →