Numpy Tutorial - Introduction to Arrays in Python

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

Key Takeaways

This video tutorial introduces the NumPy library in Python, covering its installation, basic operations, and array manipulation, demonstrating its utility for scientific computing and data structures. The tutorial highlights key methods and properties of NumPy arrays, such as indexing, sizing, and appending, showcasing their advantages over regular lists.

Full Transcript

Hey guys and welcome back to another video. In today's video, I'm actually starting a new series. And in this series, I'm going to be teaching you guys how to use the module numpy or numpy. I believe that's how you say it. Numpy uh within Python. So, this is actually not a built-in module, which means we're going to have to install it, which I'll show you in just a second. But first of all, let me just go through some of the main advantages of using NumPy. So, NumPy is a core library uh and it's meant for scientific computing in Python. So, it's meant for doing a lot of stuff with math. Um, and it's really meant for multi-dimensional arrays. So, the main thing that you're going to be using with nump numpy is multi-dimensional arrays. And it makes it a lot easier to not only index them, um, but to save data, create new multi-dimensional arrays. And throughout the videos, I'm going to be showing you different ways to do that. So, let's just get started and uh, start working with numpy. So, before we can actually do it, we have to install it. So, I'll show you here. If I try to import numpy, um, what happens? since we get no module named numpy because we haven't yet installed it. So this is really easy. All you have to do is go to your command prompt. So just cmd um down in your little search bar pop up here and just type pip install numpy like that. And then we'll just give it a second uh wait for it to connect to the internet and download our package. Now if for some reason your pip is not working I have a video on my channel. It's called how to install pygame on Windows. Um go watch that. that I'll leave a card up here and I explain how to get pip working if for some reason uh it's not installed or it's not recognized on your computer. Okay, so I'm going to assume that you guys have all done that now. So let's get started working with NumPy. So we're just going to import numpy as np. Now the reason I do this is just cuz np is a lot faster to use when I'm typing. Uh and that's what I'm going to do for the rest of the videos. Okay, so let's start by creating a new array. And that's pretty much all we're going to use uh numpy for. So to do this, I'm just going to say a r or r whatever equals np dot array. And then I'm going to give an iterable item. So something like a list. And this is the way that you have to create it. Uh unless you're using some other stuff I'm going to show in some other videos. But pretty much you're just going to put a list in here. And you can see if I click enter and I just type a ar r like that, we get an array and it says 1 2 3. Now just to show you uh what type this is, it's not a type list. It's its own data type. So if I just type type of ar r, you can see we get class numpy. Nd array. And I'll go through what a bunch of these different things do later on in the tutorial series. So a really useful thing with this is you can actually check um all the dimensions of the array. And the way that we can do this is we can do something arr.shape. And this is going to print to us three, now this means that we have an array of length three because we have three items. Um, and honestly, I don't know what the comma means there, but uh, just go with it. All right, so now let's create a multi-dimensional array just to show you why this is useful. Because in this case, you'd say, well, why don't we just use a list? It's the exact same thing. Okay, so we're going to say arr. And we're going to create a multi-dimensional list here. We're just going to have two lists. And here I'm just going to do 1 2 3 and 4 5 6 like that. Run this here. We'll print that out to the screen. And even notice here when I'm printing this the way that this array is printed and you can see we print it by rows and columns. So if I had done this with a list and I'll show you quickly. So if I do I don't know li equals this and then I print li you can see it just prints out in one flat line. So already we have this working with kind of rows and columns which makes it a lot easier not only to visualize when we're printing things out but when we're doing calculations and math and all that kind of stuff makes it a lot simpler. Okay. So let me show you how we can now index things within a multi-dimensional array. So we have this array here called a arr um and we have two lists within the list. So this is a little bit different than with lists. So just pay attention. So instead of using two square brackets like you might think by doing like array uh zero and then one which in this case would give us the element two, we're actually just going to use one square bracket. And we're going to do zero standing for the row and then we're going to do a comma and then the column. So in this case 01 and you can see we get the element two cuz we're in the first row um and the second or yeah second column I guess because we're indexing starting at zero, right? Okay. Uh now let me just print out the shape of this array to show you what this looks like. And you can see we get two three meaning we have two rows and three columns. Now some of you might argue that it's not correct to say this by rows and columns but that's kind of the way it's fundamental to understand it. Um and it just makes more sense because again if we add um more dimensions to this it's not going to work. But in this instance rows and columns are fine. There's a few other methods we can use on our arrays here which are really useful. So arrays size what this does is it gives us um it just breaks down all of the lists and gives us how many elements we actually have. So we don't care about this um as one element. We just care about every individual number. Now one thing I forgot to mention and it's very important is that these arrays are um typed. So you can't actually do something like you do in Python where you say like li is equal to and you say maybe hello or jello whatever one uh true like this. So that works fine for a list but for an array I'll show you what happens if I try to do that. So I'll just say r= np array and then I'll just take this list and I'll paste it in here like that. And you can see that we don't get any errors. But notice the type of all of our objects. So you see that the first object we typed in here was a string like this jello hello. And now notice that our one has actually been turned into a string and our true has been turned into a string as well. Now typically we don't want this. Uh so that's the thing with these arrays is they have to contain the same type. So all the data you give in, I recommend you make it the same type. Otherwise, it's going to automatically convert to I believe string by default if that's present. Um if you have something like a boolean and a number, I'm not quite sure what happens, but I'm I uh welcome you guys to play around with that. Okay, so there's three more methods now uh to show I believe uh actually two more that I haven't shown yet. So let me just do one here and then I'll say exactly what this does. So this gives us our dimensions. Okay, so n dim uh just gives us how many dimensions we have in the uh array. So pretty much this array here is one dimensional as it's just one list in there. So I can make another array here. So array equals np dot array and then we'll just have I don't know 1 2 3 and let's add another list in here. Actually let's create this list too and go four five six. And then if I go r dot n dim, you can see we get a dimension of two. The next one is data. So this one is not super useful as most times we're just going to be indexing to get our data. But you can see if we do ard data, uh, it gives us the memory location of our data. Don't ask me why you need to do that. I just figured I'd show it to you guys in case for some reason one of you wanted to know that. And one of the last things I guess I should show is how we can add things into our array. So in this case we have an array and we want to add a new element in for example this list here. So let's say we're going to say arappend this case let's say four. Now you should notice that this causes an error. And why does this happen? That's because with numpy we can't actually use aappend method. It doesn't have that method built in. So what we have to use is slightly different. It's going to look similar but it's slightly different. So the way we do this actually to add something to the end of the list is we do np dot append like this and then we put in what we want to append to. So in this case it's going to be our array. So array zero um and then comma and what we want to append in there. So in this case I want to append let's say one actually let's do like 99 so it's visible. So array zero we want to append 99. And you can see that returns us jello 99 because that's our array here. So we created a new dimension now and we've appended that in um at that uh area or whatever like that. Okay. So now uh I just want to show you what our array looks like. So if I now print array we should right we should say jello hello 99 one true but we don't. We just see jello hello 1 and true. Now why does this happen? This is because our array objects I believe are immutable and that's a way to think of them kind of in Python. So this function here uh np.append does not actually change our array. It simply returns a copy of the array with that value. So we can see that we actually got that returned value here of jello 99. Um but we didn't actually what do you want to call it? We didn't actually change our variable. So if we want to change our variable, what we have to do is we have to do a ar r equals np. In this case, I'm just going to change this to a ar r um append 999. And if now if we print a r r, you can see that we get jello 1 true 99. And again, this 99 has been changed to a string because that's the type of array. Okay, last thing I'm going to show here is the deleting something from an array. And I'm going to get more into this in the future videos. So if it's confusing now, don't worry about it. But to delete something is similar. So we're going to do np delete and then we need array. So this say ar r and then the index you want to delete at. So in this case I'm going to do one. Um and you can actually put in I believe a list of indexes. I'll let you guys try that out. So if I say one that should remove well one actually which just kind of lines up with it. And you can see we get a returned list with jhello true 99. Again though this does the same thing as the other one. It doesn't actually remove it from our list. it just returns a copy. So if we want that to be applied, we have to say a arr is equal to np.delete uh ar1. Same thing, go a r. And you can see that we now have that deleted from our list. Now, if some of you are wondering, well, why doesn't it just delete it? Why doesn't it just add it? These are actually really useful. And the way that these work can save you a lot of time and uh struggle when you're programming different things trying to create copies of lists because you don't have to do like certain slicing aspects that you would um for a regular list cuz we're now dealing with arrays and the way that these append and delete work. Anyways, that's been it for the kind of introduction to NumPy. Um if you guys are still confused, don't worry. I'm going to clarify all this stuff in future videos and I'm going to go into some more advanced stuff and show you why this is a really useful module. So, make sure you guys stay tuned for the rest of videos. If you liked this one, please leave a like and subscribe, and I'll see you again in the next ones. [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. 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/introduction/ 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
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 teaches the basics of NumPy arrays in Python, including their creation, indexing, and manipulation, and demonstrates their advantages over regular lists for scientific computing and data structures. By following this tutorial, viewers can learn how to install and use NumPy to perform various array operations. The key insight is that NumPy arrays offer a more efficient and convenient way to work with multi-dimensional data compared to regular lists.

Key Takeaways
  1. Install NumPy using pip
  2. Import NumPy as np
  3. Create a new array using np.array()
  4. Check the dimensions of an array using arr.shape
  5. Index a multi-dimensional array
  6. Print the shape of an array
  7. Print the size of an array
  8. Print the number of dimensions of an array
  9. Append an element to an array using np.append
  10. Delete an element from an array using np.delete
💡 NumPy arrays provide a more efficient and convenient way to work with multi-dimensional data compared to regular lists, offering advantages in terms of memory usage and computational speed.

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 →