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