Team Ai
Apppublic

Jagadeesh2411/Numpy_Notes

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
9_NumPy Splitting Arrays.py201 linesDownload Raw Back to pages
1import streamlit as st2import numpy as np3 4 5st.title(":blue[NumPy Splitting Arrays]")6 7st.markdown("""8<hr style='border: none; width: 80%; height: 8px; background: linear-gradient(to right, blue, pink); opacity: 0.8; border-radius: 10px; margin-left: auto; margin-right: auto;'>9""", unsafe_allow_html=True)10 11st.markdown("""12**Splitting NumPy Arrays** is the reverse operation of joining. 13merges multiple arrays into one, splitting breaks one array into multiple arrays. 14""")15 16 17st.subheader("Splitting a 1D Array")18st.markdown("""19You can use the `array_split()` function to split an array into multiple parts. 20This function takes the array you want to split and the number of splits as arguments.21example, to split an array into 3 parts:22```python23import numpy as np24arr = np.array([1, 2, 3, 4, 5, 6])25newarr = np.array_split(arr, 3)26print(newarr)27```28This will output:29```30[array([1, 2]), array([3, 4]), array([5, 6])]31```32Note: The return value is a list containing the split arrays.33""")34 35 36st.subheader("Handling Less Elements")37st.markdown("""38If the array has fewer elements than required splits, it will adjust from the end accordingly.39For example, if we split an array with 6 elements into 4 parts:40```python41newarr = np.array_split(arr, 4)42print(newarr)43```44This will output:45```46[array([1, 2]), array([3, 4]), array([5, 6]), array([], dtype=int64)]47```48""")49 50 51st.subheader("Accessing Split Arrays")52st.markdown("""53You can access the split arrays like any array element. For instance:54```python55print(newarr[0])  # Output: [1 2]56print(newarr[1])  # Output: [3 4]57print(newarr[2])  # Output: [5 6]58```59""")60 61 62st.subheader("Splitting 2D Arrays")63st.markdown("""64You can split 2D arrays in the same manner. For example, to split a 2D array into three 2D arrays:65```python66arr = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]])67newarr = np.array_split(arr, 3)68print(newarr)69```70This will output three 2D arrays:71```72[array([[1, 2],73       [3, 4]]), array([[5, 6],74       [7, 8]]), array([[ 9, 10],75       [11, 12]])]76```77""")78 79 80st.subheader("Splitting with More Elements")81st.markdown("""82For a more complex example, if we split an array with 6 rows and 3 columns into three 2D arrays:83```python84arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]])85newarr = np.array_split(arr, 3)86print(newarr)87```88This will output:89```90[array([[1, 2, 3],91       [4, 5, 6]]), array([[7, 8, 9],92       [10, 11, 12]]), array([[13, 14, 15],93       [16, 17, 18]])]94```95""")96 97 98st.subheader("Splitting Along Different Axes")99st.markdown("""100You can specify which axis you want to split along. For example, to split the previous 2D array along columns:101```python102newarr = np.array_split(arr, 3, axis=1)103print(newarr)104```105This will output:106```107[array([[1],108       [4],109       [7],110       [10],111       [13],112       [16]]), array([[2],113       [5],114       [8],115       [11],116       [14],117       [17]]), array([[3],118       [6],119       [9],120       [12],121       [15],122       [18]])]123```124""")125 126st.subheader("Using hsplit for Horizontal Splitting")127st.markdown("""128NumPy also provides an alternate solution using `hsplit()`, which is specifically for horizontal splitting.129```python130newarr = np.hsplit(arr, 3)131print(newarr)132```133This will yield the same result as splitting along rows:134```135[array([[1, 2, 3],136       [4, 5, 6]]), array([[7, 8, 9],137       [10, 11, 12]]), array([[13, 14, 15],138       [16, 17, 18]])]139```140""")141 142 143st.title(":blue[NumPy Array Functions]")144 145st.subheader("1. `np.concatenate()`")146st.write("**Definition**: Joins a sequence of arrays along an existing axis. Returns a new array.")147st.write("**Example**: `np.concatenate((a, b), axis=0)` joins arrays `a` and `b` vertically.")148 149 150st.subheader("2. `np.hstack()`")151st.write("**Definition**: Stacks arrays in sequence horizontally (column-wise).")152st.write("**Example**: `np.hstack((a, b))` combines arrays `a` and `b` side by side.")153 154st.subheader("3. `np.vstack()`")155st.write("**Definition**: Stacks arrays in sequence vertically (row-wise).")156st.write("**Example**: `np.vstack((a, b))` combines arrays `a` and `b` on top of each other.")157 158st.subheader("4. `np.r_[]`")159st.write("**Definition**: Provides a convenient way to concatenate arrays along the first axis.")160st.write("**Example**: `np.r_[a, b]` combines arrays `a` and `b` vertically.")161 162st.subheader("5. `np.c_[]`")163st.write("**Definition**: Provides a convenient way to concatenate arrays along the second axis.")164st.write("**Example**: `np.c_[a, b]` combines arrays `a` and `b` horizontally.")165 166st.subheader("6. `np.where()`")167st.write("**Definition**: Returns the indices of elements that satisfy a given condition.")168st.write("**Example**: `np.where(a > 0)` returns the indices of elements in `a` greater than 0.")169 170st.subheader("7. `np.min()`")171st.write("**Definition**: Returns the minimum value of an array.")172st.write("**Example**: `np.min(a)` gives the smallest element in array `a`.")173 174st.subheader("8. `np.argmax()`")175st.write("**Definition**: Returns the indices of the maximum values along an axis.")176st.write("**Example**: `np.argmax(a)` returns the index of the maximum value in array `a`.")177 178st.subheader("9. `np.max()`")179st.write("**Definition**: Returns the maximum value of an array.")180st.write("**Example**: `np.max(a)` gives the largest element in array `a`.")181 182st.subheader("10. `np.mean()`")183st.write("**Definition**: Computes the arithmetic mean along the specified axis.")184st.write("**Example**: `np.mean(a)` calculates the average of the elements in array `a`.")185 186st.subheader("11. `np.median()`")187st.write("**Definition**: Computes the median of the array elements.")188st.write("**Example**: `np.median(a)` returns the median value of the elements in array `a`.")189 190st.subheader("12. `np.std()`")191st.write("**Definition**: Computes the standard deviation of the array elements.")192st.write("**Example**: `np.std(a)` calculates the standard deviation of the elements in array `a`.")193 194st.subheader("13. `np.var()`")195st.write("**Definition**: Computes the variance of the array elements.")196st.write("**Example**: `np.var(a)` returns the variance of the elements in array `a`.")197 198st.subheader("14. `np.unique()`")199st.write("**Definition**: Finds the unique elements of an array.")200st.write("**Example**: `np.unique(a)` returns the unique values in array `a`.")201