Jagadeesh2411/Numpy_Notes
0
1import streamlit as st2import numpy as np3 4 5st.title(":blue[NumPy Array Iteration]")6 7st.markdown("""8<style>9@keyframes pulse {10 0% { background-color: #ff0000; }11 50% { background-color: #ffff00; }12 100% { background-color: #ff0000; }13}14hr.pulse {15 border: none;16 height: 5px;17 background-color: #ff0000;18 animation: pulse 2s infinite;19}20</style>21<hr class="pulse">22""", unsafe_allow_html=True)23 24 25 26st.markdown("""27**Iterating Arrays** means going through elements one by one. 28NumPy allows us to iterate through arrays of any dimension, making it a powerful tool for handling multi-dimensional data.29""")30 31 32st.subheader("Iterating 1-D Arrays")33st.markdown("""34You can iterate through a 1-D array using a basic for loop in Python. 35example of iterating through a 1-D array:36```python37import numpy as np38arr = np.array([1, 2, 3])39for x in arr:40 print(x)41```42""")43 44 45st.subheader("Iterating 2-D Arrays")46st.markdown("""47When iterating a 2-D array, you go through each row. 48Example:49```python50import numpy as np51arr = np.array([[1, 2, 3], [4, 5, 6]])52for x in arr:53 print(x)54```55To access each scalar element, you can nest another loop:56```python57for x in arr:58 for y in x:59 print(y)60```61""")62 63st.subheader("Iterating 3-D Arrays")64st.markdown("""65In a 3-D array, iteration will go through all 2-D arrays. 66Example:67```python68import numpy as np69arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])70for x in arr:71 print(x)72```73To access each scalar:74```python75for x in arr:76 for y in x:77 for z in y:78 print(z)79```80""")81 82 83st.subheader("Iterating Arrays Using nditer()")84st.markdown("""85The `nditer()` function allows for advanced iterations without needing to nest multiple loops.86Example for iterating through each scalar in a 3-D array:87```python88import numpy as np89arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])90for x in np.nditer(arr):91 print(x)92```93""")94 95st.subheader("Iterating with Different Data Types")96st.markdown("""97To change the datatype of elements while iterating, use the `op_dtypes` argument with `nditer()`. 98Example:99```python100import numpy as np101arr = np.array([1, 2, 3])102for x in np.nditer(arr, flags=['buffered'], op_dtypes=['S']):103 print(x)104```105""")106 107 108st.subheader("Iterating with Different Step Size")109st.markdown("""110You can iterate while skipping elements by filtering. 111Example:112```python113import numpy as np114arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])115for x in np.nditer(arr[:, ::2]):116 print(x)117```118""")119 120 121st.subheader("Enumerated Iteration Using ndenumerate()")122st.markdown("""123Use `ndenumerate()` when you need the index of each element while iterating.124Example on a 1-D array:125```python126import numpy as np127arr = np.array([1, 2, 3])128for idx, x in np.ndenumerate(arr):129 print(idx, x)130```131Example on a 2-D array:132```python133import numpy as np134arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])135for idx, x in np.ndenumerate(arr):136 print(idx, x)137```138""")139 140 