numpy tutorial: iterate numpy array using nditer | numpy nditer

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

Key Takeaways

Demonstrates iteration of a numpy array using nditer for various iteration styles

Full Transcript

dear friends in this tutorial we are going to look into how you can act rate through numpy array as usual I have launched Jupiter not book by running this command in my command prompt and created a ipython notebook and imported my numpy module as NP first thing we are going to do is create and alright so I will use a range function and let's say I have 12 elements and we can reshape it into 3 by 4 so now I got this numpy array which is 2 dimensional array the simple way of iterating through this array is just to run fold upon it I can say for row in a print row and that will print all the rows if you are interested in uttering or each of these elements one by one then you can introduce one more for loop ends you can say for cell in raw / in cell and that will print all individual elements now if you just want to flatten the list flatten this array into a listers then you can simply call flatten function this way you don't have to write to for loops you can just write one single for loop and be done with it now numpy offers a function called nd ITER which allows you to do a more sophisticated way of iteration for example I can I can achieve the same thing as flatten by using ndit function I can say for X n n P dot nd ITER the first argument is the array itself and the second argument is order so if you go by C order and print each of these elements it will have same effect as flatten essentially you are going row by row and then printing values in each of these columns so here 0 1 2 3 4 you can see it is printing it in this order here but let's say if you are a Fortran programmer and you want to go column by column instead of row by row so instead of printing 0 1 2 3 4 you want to print 0 4 8 1 5 9 then you can just change your order to be Fulton order and you will see that you are now printing it into for turn order so that's a benefit of nd either function it allows you to do iteration in a variety of ways if you check the documentation of nd ITER you will see a lot of options here I have flags and in the Flags one of the option is external loop this causes the values given to be one-dimensional arrays with multiple values instead of 0 dimensional arrays what this means is let's say I want to go in for an order but instead of going element by element I want to print an entire column so I want to first print 0 for 8 then 1 5 9 2 6 10 3 7 11 basically print entire column on each iteration you can do this by supplying flags here the value you want to provide is external loop and when you do that you see that each iteration is now printing the entire column if you check some other flags then one of the flags in Opa Flags is rewrite which means the operand will be read from and returned to what this means is let's say you are iterating through the array and you want to modify the elements you can do that using that particular flag so the way you write that is 4x in NP dot nd ITER and then in off no flags you will say read right and then the convention that you have to follow is you have to say X dot dot dot equal to X cross X so let's say I want to create square of each of these elements in an array and I want to modify the original array okay if something went wrong Oh P okay it is o P Flags actually so when I run this and now I print my array you will notice that my original array is modified and each of these elements is actually the square of the original element so 3 square is 9 4 squared is 16 and so on now how do you I trade through two numpy arrays simultaneously so sometimes based on what kind of problem you are solving you might have to iterate through two arrays at the same time okay so let's create a new array call it n P dot arrange and this time I'm going to create an array with a different dimension so I will create let's say three by one array okay and it has these elements in my original array is like this I mean I modified it but now it looks like this so is this B is this now I want to I trait through these two arrays simultaneously now when I add it through it what I want to do is I want to I trip to the first row and at that time the element from the second array should also be the first row which is three okay so you can use for XY in and P dot nd ITER in the bracket you can supply two arrays that you want to title it through and then print both of these you will notice what happened is when it goes through the first array the first row in the array okay zero one four nine at that time it is keeping this element constant three so you can see that now you can I trip through two arrays simultaneously now notice that in order to do this your arrays should be broadcast table which means either their shape should be same or one of the dimensions in one of the errors should be one okay so if you read the general broadcasting rules on number arrays it will say that two dimensions are compatible when they are either equal or one of them is one okay so for example let's say if I had different kind of right here five and this right let's see if I add this kind of case then I won't be able to write through it because there would be a branch could not be broadcast together because the shapes of these two arrays are not compatible with each other okay that's all we had for this tutorial I have provided the link of this Jupiter notebook in the video description below so make sure you play with it and let me know if you have any question please post your comment and feedback below in the comment section thank you bye

Original Description

nditer can be used to iterate through numpy array in variety of ways. C style and F style iteration is possible using flags in nditer. You can also iterate two broadcastable arrays concurrently using nditer | numpy nditer Topics that are covered in this Python Video: 0:00 Overview 0:41 Run for loop 1:16 Use flatten() function on for loop 1:41 numpy function nditer() function 3:34 supply flags on for loop 4:04 op_flag on for loop 6:09 iterate two arrays Do you want to learn technology from me? Check https://codebasics.io/ for my affordable video courses. https://github.com/codebasics/py/blob/master/numpy/nditer.ipynb Website: https://codebasics.io/ Facebook: https://www.facebook.com/codebasicshub Twitter: https://twitter.com/codebasicshub
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 (7)

Overview
0:41 Run for loop
1:16 Use flatten() function on for loop
1:41 numpy function nditer() function
3:34 supply flags on for loop
4:04 op_flag on for loop
6:09 iterate two arrays
Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →