Team Ai
Apppublic

Jagadeesh2411/Numpy_Notes

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
2_NumPY_Arrays.py282 linesDownload Raw Back to pages
1import streamlit as st2import numpy as np3 4# Title Section5st.title(":blue[NumPy Arrays: Dive into Array Magic!] โœจ")6st.markdown("<hr style='border: none; height: 5px; background-color: purple;'>", unsafe_allow_html=True)7 8# Introduction Section9st.markdown("""10<hr style='border: none; height: 5px; background-color: rgba(0, 0, 255, 0.5);'>11""", unsafe_allow_html=True)12 13st.markdown("""14**๐Ÿ”— Joining Arrays** means putting the contents of two or more arrays into a single array. 15In SQL, we join tables based on a key, whereas in NumPy, we join arrays by axes. 16The `concatenate()` function allows us to join arrays along a specified axis.17""")18 19# Subsections with Examples20st.subheader("๐Ÿ“š Arrays")21st.markdown(""" An **array** is a collection of data of the same type, organized in dimensions. ๐Ÿงฎ """)22 23st.subheader("๐Ÿ’ก **How to Create an n-Dimensional Array**")24 25st.subheader("๐Ÿ”ข Dimensions in Array")26st.write("A **dimension** in arrays is one level of array depth (nearest arrays).")27 28# Joining 0-D Arrays29st.subheader("๐ŸŽฏ Joining 0-D Arrays")30st.markdown("""310-D arrays, also known as ":red[SCALARS]," are single values in an array. 32Hereโ€™s an example:33```python34import numpy as np35arr1 = np.array(18)36print(arr1)  # Output: 1837print(arr1.ndim)  # Output: 038print(arr1.shape)  # Output: ()39```40""")41 42# Joining 1-D Arrays43st.subheader("๐Ÿ“ˆ Joining 1-D Arrays")44st.write("""1-D arrays are called ":red[VECTORS]." """)45st.markdown("""46You can join two 1-D arrays using the `concatenate()` function. 47Hereโ€™s an example:48```python49import numpy as np50arr1 = np.array([1, 2, 3])51arr2 = np.array([4, 5, 6])52arr = np.concatenate((arr1, arr2))53print(arr)  # Output: [1 2 3 4 5 6]54print(arr1.ndim)  # Output: 155print(arr1.shape)  # Output: (3,)56```57""")58 59# Joining 2-D Arrays60st.subheader("๐Ÿงฎ Joining 2-D Arrays Along Rows")61st.write("""2-D arrays are called ":red[MATRIX]." """)62st.markdown("""63To join two 2-D arrays along rows (axis=1), you can use `concatenate()`:64```python65import numpy as np66arr1 = np.array([[1, 2], [3, 4]])67arr2 = np.array([[5, 6], [7, 8]])68arr = np.concatenate((arr1, arr2), axis=1)69print(arr)70# Output: [[1 2 5 6]71           [3 4 7 8]]72```73""")74 75# Joining 3-D Arrays76st.subheader("๐Ÿ”— Joining 3-D Arrays")77st.write("""3-D arrays are called ":red[TENSOR]." """)78st.markdown("""79```python80import numpy as np81arr3d = np.array([[[1, 2, 3], [4, 5, 6]],82                  [[7, 8, 9], [10, 11, 12]]])83print(arr3d)84# Output: 85 [[[ 1  2  3]86   [ 4  5  6]]87  [[ 7  8  9]88   [10 11 12]]]89print(arr3d[0, 1, 2])  # Output: 690print(arr3d.shape)  # Output: (2, 2, 3)91print(arr3d.ndim)   # Output: 392```93""")94 95# Higher Dimensions96st.subheader("๐Ÿš€ Higher Dimensions")97st.markdown("""98```python99import numpy as np100arr4d = np.array([[[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]],101                  [[[13, 14, 15], [16, 17, 18]], [[19, 20, 21], [22, 23, 24]]]])102print(arr4d)103# Output:104 [[[[ 1  2  3]105    [ 4  5  6]]106   [[ 7  8  9]107    [10 11 12]]]108  [[[13 14 15]109    [16 17 18]]110   [[19 20 21]111    [22 23 24]]]]112print(arr4d.shape)  # Output: (2, 2, 2, 3)113print(arr4d.ndim)   # Output: 4114```115""")116 117# Stacking Arrays118st.subheader("๐Ÿ”€ Stacking Arrays Using Stack Functions")119st.markdown("""120Stacking is similar to concatenation, but it is done along a new axis. 121Hereโ€™s an example of stacking along the second axis (1):122```python123import numpy as np124arr1 = np.array([1, 2, 3])125arr2 = np.array([4, 5, 6])126arr = np.stack((arr1, arr2), axis=1)127print(arr)128# Output: [[1 4]129           [2 5]130           [3 6]]131```132""")133 134# Horizontal and Vertical Stacking135st.subheader("๐Ÿ“Š Stacking Along Rows")136st.markdown("""137You can use the `hstack()` function to stack arrays along rows:138```python139import numpy as np140arr1 = np.array([1, 2, 3])141arr2 = np.array([4, 5, 6])142arr = np.hstack((arr1, arr2))143print(arr)  # Output: [1 2 3 4 5 6]144```145""")146 147st.subheader("๐Ÿ“Š Stacking Along Columns")148st.markdown("""149You can use the `vstack()` function to stack arrays along columns:150```python151import numpy as np152arr1 = np.array([1, 2, 3])153arr2 = np.array([4, 5, 6])154arr = np.vstack((arr1, arr2))155print(arr)156# Output: [[1 2 3]157         [4 5 6]]158```159""")160 161st.subheader("๐Ÿ“Š Stacking Along Height (Depth)")162st.markdown("""163You can use the `dstack()` function to stack arrays along height:164```python165import numpy as np166arr1 = np.array([1, 2, 3])167arr2 = np.array([4, 5, 6])168arr = np.dstack((arr1, arr2))169print(arr)170# Output: [[[1 4]171            [2 5]172            [3 6]]]173```174""")175 176# Advanced Stacking177st.subheader("โœจ Advanced Stacking")178 179st.markdown("""180You can use the `c_()` function to stack arrays along columns:181```python182import numpy as np183a1 = np.arange(1, 11).reshape(2, 5)184a2 = np.arange(11, 21).reshape(2, 5)185a3 = np.arange(21, 31).reshape(2, 5)186print(np.c_[a1, a2, a3])187# Output: array([[ 1,  2,  3,  4,  5, 11, 12, 13, 14, 15, 21, 22, 23, 24, 25],188                 [ 6,  7,  8,  9, 10, 16, 17, 18, 19, 20, 26, 27, 28, 29, 30]])189```190""")191 192st.markdown("""193You can use the `r_()` function to stack arrays along rows:194```python195import numpy as np196a1 = np.arange(1, 11).reshape(2, 5)197a2 = np.arange(11, 21).reshape(2, 5)198a3 = np.arange(21, 31).reshape(2, 5)199print(np.r_[a1, a2, a3])200# Output: array([[ 1,  2,  3,  4,  5],201                 [ 6,  7,  8,  9, 10],202                 [11, 12, 13, 14, 15],203                 [16, 17, 18, 19, 20],204                 [21, 22, 23, 24, 25],205                 [26, 27, 28, 29, 30]])206```207""")208 209# NumPy Where Function and Conditional Indexing210st.title(":blue[NumPy Where Function and Conditional Indexing] ๐Ÿง")211 212st.markdown("""213**NumPy's `where` function** allows you to access and manipulate elements of an array based on specific conditions. 214This feature is particularly useful for filtering elements based on their values and applying logical conditions.215""")216 217st.subheader("๐Ÿ”ข Creating a 2D Array")218st.markdown("""219First, let's create a 2D NumPy array using `arange()` and `reshape()`:220```python221import numpy as np222a = np.arange(1, 31)223a.shape = (6, 5)224print(a)225```226This will output:227```228array([[ 1,  2,  3,  4,  5],229       [ 6,  7,  8,  9, 10],230       [11, 12, 13, 14, 15],231       [16, 17, 18, 19, 20],232       [21, 22, 23, 24, 25],233       [26, 27, 28, 29, 30]])234```235""")236 237st.subheader("๐Ÿ“ Array Shape")238st.markdown("""239The shape of the array can be checked using:240```python241a.shape242```243This will return:244```245(6, 5)246```247""")248 249st.subheader("โšก Applying Index-Based Conditions")250st.markdown("""251You can apply conditions to filter the elements of the array. 252For example, find elements that are even and multiples of 6:253```python254condition = (a % 2 == 0) & (a % 6 == 0)255print(condition)256```257This will output:258```259array([[False, False, False, False, False],260       [ True, False, False, False, False],261       [False,  True, False, False, False],262       [False, False,  True, False, False],263       [False, False, False,  True, False],264       [False, False, False, False,  True]])265```266""")267 268st.subheader("๐ŸŽฏ Accessing Elements Based on Conditions")269st.markdown("""270You can access the elements that meet the specified conditions by using the condition array:271```python272result = a[condition]273print(result)274```275This will output:276```277array([ 6, 12, 18, 24, 30])278```279These are the elements of the array that are both even and multiples of 6.280""")281st.write("Hai Sanjay")282