numpy tutorial - introduction | numpy array vs python list

codebasics · Beginner ·🛠️ AI Tools & Apps ·9y ago

Key Takeaways

Introduces numpy and its features

Full Transcript

dear friends we are going to look into numpy module today it is extremely popular in Python community and it is heavily used for scientific Computing we will see why it is so popular so let's first start by installing it I will install it using pip you can run pip install numpy command to download and install it uh while it is installing uh numpy's main object is end dimensional array okay so we will look into that array so here uh I have my python idle shall open and I'm going to import numpy module here so num I imported it as NP and to create an array object you will use np. array and in this bracket you just pass down your list so this is my uh numpy array if you look into it now this is sort of similar to a list it is actually very similar to a list you can access the elements by index looks like a list so the question is I already have a list why do I need this numpy array well there are several benefits of of using numpy uh array uh there are three main benefit benefits uh first is it requires less memory it is fast and convenient and we'll go over all these three benefits one by one okay so essentially what we are doing is we are comparing numpy with numpy array with a python list here okay so let's first create a python list I'm just going to say range th000 so I create created a list which has th000 elements here and I'm going to print the size of the list now to print the size uh you need to get a size of one one element so one element here is one number you can give any number here and then length of your uh list second I'm going to create a numpy array so this is how you create a numi array this function a range is similar to range is going to create an array with element 0 to 999 okay so the size of this numpy array would be 1,000 and to print the size of this array you need to call array do size so array do size will give you, okay and and the size of every element will be it will be item size so just remember that array do item size is size of one element versus ar. size is the total length of an array when you run this program you'll notice that the python list is taking taking 14,000 bytes versus nump is taking only 4,000 bytes now this is because your python uh the size of one python object is like 14 okay H versus size of one numpy array object is here in this case since we are storing integer numbers it is only four bytes okay so if you look at this diagram it kind of show this shows the memory presentation of uh list and numpy array on the left hand side we have numpy array which uh points to a continuous or continuous uh location of memories where you have all your elements stored 1 2 3 4 5 and so on and each of these element con is occupying four by of memory versus in case of python list you all know that uh in Python everything is an object so the list will contain a list of pointers first as you can see in the diagram and each point pointer will then point to another location in the memory which will be your object and the size of that one object will be 14 bytes so this is the reason why uh numpy array the memory usage is very less compared to a plain python list now when your array size or List size is very small you won't notice much difference but let's say you are dealing with heavy amount of data where you have millions and billions of numbers to process in that case it will make sense to use numpy all right the second advantage of numpy is that it is fast and convenient so let me prove that point as well so I'm going to what I'm going to do here is um all right let me just close this guy here and I'm going to now create here two list first call it L1 and L2 okay so range uh size so size is th000 here and L2 is also similar so I created two list here and then I'm going to create two array so np. AR range again to create a numpy array and then second array will be called A2 okay now I want to measure the time uh between list processing and nump array processing so for this you use start is equal to time. time and now what I'm going to do is see the thing the operation that I'm doing here is I'm adding these two list I'm adding uh these two list and producing a new list call call it result okay so to order these two list you have to do something like this x + y for XY IN Zip L1 L2 what this will do is it will take first element from L1 first element from L2 add them together and put it in this result as a first element so it will just uh add individual elements from these two list and put it in a result okay pretty straightforward uh and I'm just going to say python list took this much time so right now the time is this I'm going to subtract start from here and I'm going to multiply this guy with th because by default it is in second I want to print milliseconds here all right now again at this point I will capture time one more time and then I will store result here so now so this was your python list okay so uh let me just say python list and here now I'm processing numpy array so that's the difference between these two code blocks uh now numpy is also convenient because if you want to add two arrays you just say A1 + A2 right so it is not like you have to write a list comprehension okay you can just add them together and you can just say print my numpy to uh this is just a way to measure the time that this code block took okay all right so let's run this program here okay says python list took 0.0 numi took 0.0 because this size is pretty small so I'm going to increase the size and do a processing for a about a million elements here now you can see that in order to add million uh elements from two list python list took 116 millisecond versus numpy array took 11.5 seconds second this is crazy fast so you can see that again when you're processing millions and millions of numbers it just makes sense to use numpy it it is also convenient because you can see that if you want to add two list together you just do A1 and A2 a1+ A2 I I will just demonstrate that using idle because it's uh it's little easier to demo that in idle uh so I'm going to to create let's say two numpy array okay A1 A1 is np. array it has 1 2 3 and A2 is again np. array it has let's say element 4 56 okay so you can do A1 + A2 so it will give you that see 4 and 1 5 2 and 5 7 and so on you can also do A2 - A1 so you can see you subtracted this guy from here you can also do multiplication so A1 cross A2 you can do division and so on you can do all these operations okay so that was all about numpy introduction you saw that why numpy is so better than so much better than the plain python uh list uh we will cover more about numpy in our next tutorial thank you for watching

Original Description

This tutorial covers an introduction to numpy python module. We'll see why numpy is very popular and talk about its main feature "n dimensional array". It is memory efficient, fast and convenient compared to python native list. Topics that are covered in this Python Video: 0:00 what is numpy? 1:24 benefits of numpy array over python list 3:48 numpy using less memory (Memory presentation diagram of numpy and python array ) 5:08 why numpy is fast and convenient Do you want to learn technology from me? Check https://codebasics.io/ for my affordable video courses. Next Video: numpy tutorial - basic array operations: https://www.youtube.com/watch?v=a8aDcLk4vRc&list=PLeo1K3hjS3usILfyvQlvUBokXkHPSve6S&index=40 Website: https://codebasics.io/ 📸 Codebasics Instagram: https://www.instagram.com/codebasicshub/ 🔊 Codebasics Facebook: https://www.facebook.com/codebasicshub 📝 Codebasics LinkedIn: https://www.linkedin.com/company/codebasics/ 📱 Codebasics X handle: https://twitter.com/codebasicshub 📸 Dhaval's Instagram: https://www.instagram.com/dhavalsays/ 📝 Dhaval's LinkedIn: https://www.linkedin.com/in/dhavalsays/ 📱 Dhaval's X handle: https://x.com/dpcodebasics
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from codebasics · codebasics · 0 of 60

← Previous Next →
1 Python Tutorial - 1. Install python on windows
Python Tutorial - 1. Install python on windows
codebasics
2 Python Tutorial - 2. Variables
Python Tutorial - 2. Variables
codebasics
3 Python Tutorial - 3. Numbers
Python Tutorial - 3. Numbers
codebasics
4 Python Tutorial - 4. Strings
Python Tutorial - 4. Strings
codebasics
5 Python Tutorial - 5. Lists
Python Tutorial - 5. Lists
codebasics
6 Python Tutorial - 6. Install PyCharm on Windows
Python Tutorial - 6. Install PyCharm on Windows
codebasics
7 PyCharm Tutorial - 7. Debug python code using PyCharm
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
8 Python Tutorial -  8. If Statement
Python Tutorial - 8. If Statement
codebasics
9 Python Tutorial - 9. For loop
Python Tutorial - 9. For loop
codebasics
10 Python Tutorial -  10. Functions
Python Tutorial - 10. Functions
codebasics
11 Python Tutorial - 11. Dictionaries and Tuples
Python Tutorial - 11. Dictionaries and Tuples
codebasics
12 Python Tutorial - 12. Modules
Python Tutorial - 12. Modules
codebasics
13 Python Tutorial - 13. Reading/Writing Files
Python Tutorial - 13. Reading/Writing Files
codebasics
14 How to install Julia on Windows
How to install Julia on Windows
codebasics
15 Python Tutorial - 14. Working With JSON
Python Tutorial - 14. Working With JSON
codebasics
16 Julia Tutorial - 1. Variables
Julia Tutorial - 1. Variables
codebasics
17 Julia Tutorial - 2. Numbers
Julia Tutorial - 2. Numbers
codebasics
18 Python Tutorial - 15. if __name__ == "__main__"
Python Tutorial - 15. if __name__ == "__main__"
codebasics
19 Julia Tutorial - Why Should I Learn Julia Programming Language
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
20 Python Tutorial  - 16. Exception Handling
Python Tutorial - 16. Exception Handling
codebasics
21 Julia Tutorial - 3. Complex and Rational Numbers
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
22 Julia Tutorial - 4. Strings
Julia Tutorial - 4. Strings
codebasics
23 Python Tutorial -  17. Class and Objects
Python Tutorial - 17. Class and Objects
codebasics
24 Julia Tutorial - 5. Functions
Julia Tutorial - 5. Functions
codebasics
25 Julia Tutorial - 6. If Statement and Ternary Operator
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
26 Julia Tutorial - 7. For While Loop
Julia Tutorial - 7. For While Loop
codebasics
27 Python Tutorial  - 18. Inheritance
Python Tutorial - 18. Inheritance
codebasics
28 Julia Tutorial - 8. begin and (;) Compound Expressions
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
29 Python Tutorial - 12.1 - Install Python Module (using pip)
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
30 Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
31 Julia Tutorial - 10. Exception Handling
Julia Tutorial - 10. Exception Handling
codebasics
32 Python Tutorial  - 19. Multiple Inheritance
Python Tutorial - 19. Multiple Inheritance
codebasics
33 Python Tutorial - 20. Raise Exception And Finally
Python Tutorial - 20. Raise Exception And Finally
codebasics
34 Python Tutorial - 21. Iterators
Python Tutorial - 21. Iterators
codebasics
35 Python Tutorial - 22. Generators
Python Tutorial - 22. Generators
codebasics
36 Python Tutorial - 23. List Set Dict Comprehensions
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
37 Python Tutorial - 24. Sets and Frozen Sets
Python Tutorial - 24. Sets and Frozen Sets
codebasics
38 Python Tutorial - 25. Command line argument processing using argparse
Python Tutorial - 25. Command line argument processing using argparse
codebasics
39 Debugging Tips - What is bug and debugging?
Debugging Tips - What is bug and debugging?
codebasics
40 Debugging Tips - Conditional Breakpoint
Debugging Tips - Conditional Breakpoint
codebasics
41 Debugging Tips - Watches and Call Stack
Debugging Tips - Watches and Call Stack
codebasics
42 Python Tutorial - 26. Multithreading - Introduction
Python Tutorial - 26. Multithreading - Introduction
codebasics
43 Git Tutorial 3:  How To Install Git
Git Tutorial 3: How To Install Git
codebasics
44 Git Tutorial 1: What is git / What is version control system?
Git Tutorial 1: What is git / What is version control system?
codebasics
45 Git Tutorial 2 : What is Github? | github tutorial
Git Tutorial 2 : What is Github? | github tutorial
codebasics
46 Git Tutorial 4: Basic Commands: add, commit, push
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
47 Git Tutorial 5: Undoing/Reverting/Resetting code changes
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
48 Git Tutorial 6: Branches (Create, Merge, Delete a branch)
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
49 Git Github Tutorial 10: What is Pull Request?
Git Github Tutorial 10: What is Pull Request?
codebasics
50 Git Tutorial 7: What is HEAD?
Git Tutorial 7: What is HEAD?
codebasics
51 Git Tutorial 9: Diff and Merge using meld
Git Tutorial 9: Diff and Merge using meld
codebasics
52 Difference between Multiprocessing and Multithreading
Difference between Multiprocessing and Multithreading
codebasics
53 Python Tutorial - 27. Multiprocessing Introduction
Python Tutorial - 27. Multiprocessing Introduction
codebasics
54 Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
55 Git Tutorial 8 - .gitignore file
Git Tutorial 8 - .gitignore file
codebasics
56 Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
57 Python Tutorial - 30. Multiprocessing Lock
Python Tutorial - 30. Multiprocessing Lock
codebasics
58 Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
59 What is code?
What is code?
codebasics
60 Python unit testing - pytest introduction
Python unit testing - pytest introduction
codebasics

Related AI Lessons

Chapters (4)

what is numpy?
1:24 benefits of numpy array over python list
3:48 numpy using less memory (Memory presentation diagram of numpy and python array )
5:08 why numpy is fast and convenient
Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →