Jagadeesh2411/Numpy_Notes
0
1import streamlit as st2import numpy as np3 4# Title and Header5st.title(":blue[NumPy Functions] โจ")6 7# Subtitle with Styling8st.markdown("""9<div style="text-align: center; border-bottom: 2px solid black; line-height: 0.1em; margin: 10px 0 20px;">10 <span style="background:#fff; padding:0 10px;">NumPy Functions Exploration</span>11</div>12""", unsafe_allow_html=True)13 14st.header("Welcome to NumPy Function Exploration! ๐")15 16# Function 1: Dot Product17st.subheader("**1. Dot Product (np.dot)** โ๏ธ")18st.write("The dot product is a fundamental operation for multiplying vectors, matrices, or arrays. It plays a significant role in machine learning, physics, and computer graphics.")19 20st.write("To compute the dot product using NumPy, we use `np.dot()`, which supports both 1D and 2D arrays. The function performs matrix multiplication for 2D arrays and element-wise multiplication for 1D arrays. ๐ค")21 22first_array = np.array([[1, 2, 3], [4, 5, 6]])23second_array = np.array([[7, 8], [9, 10], [11, 12]])24 25st.write("### First Array: ")26st.code(first_array)27 28st.write("### Second Array: ")29st.code(second_array)30 31result = np.dot(first_array, second_array)32st.write("### Result of `np.dot(first_array, second_array)`:")33st.code(result)34 35# Example36st.write("Example: `np.dot(np.array([1, 2]), np.array([3, 4]))` results in: 11 โ
")37 38# Function 2: Round Function39st.subheader("**2. Round Function (np.round)** ๐")40st.write("""41The `np.round()` function helps round numbers to the nearest integer or a specified decimal point. 42This function is very useful for formatting data or rounding off float values.43""")44st.write("Use it to round data to the nearest whole number or to a specific decimal place. Example below โฌ๏ธ")45 46float_values = np.array([85.56, 45.32])47rounded_values = np.round(float_values)48st.write("Rounding `[85.56, 45.32]` to nearest integers:")49st.code(rounded_values)50 51st.write("Rounding `[85.567, 45.324]` to 2 decimal places:")52float_values_with_decimals = np.array([85.567, 45.324])53rounded_with_decimals = np.round(float_values_with_decimals, 2)54st.code(rounded_with_decimals)55 56# Function 3: Ceiling Function57st.subheader("**3. Ceiling Function (np.ceil)** ๐")58st.write("""59The `np.ceil()` function rounds numbers **up** to the nearest integer.60It is helpful when you need to ensure that a number always rounds to a greater value. ๐61""")62ceil_values = np.ceil(np.array([3.9, 1.2, -2.7]))63st.write("Rounding up `[3.9, 1.2, -2.7]` using `np.ceil()`: ")64st.code(ceil_values)65 66# Function 4: Floor Function67st.subheader("**4. Floor Function (np.floor)** โฌ๏ธ")68st.write("""69The `np.floor()` function rounds numbers **down** to the nearest integer.70Use this when you want to make sure a number doesn't exceed a given value. ๐ฏ71""")72floor_values = np.floor(np.array([3.9, 1.2, -2.7]))73st.write("Rounding down `[3.9, 1.2, -2.7]` using `np.floor()`: ")74st.code(floor_values)75 76# Trigonometric Functions77st.header(":blue[Mathematical Trigonometric Functions] ๐ง ")78 79st.write("""80In NumPy, trigonometric functions are applied element-wise to arrays. These functions expect inputs in radians, not degrees.81You can convert degrees to radians using `np.deg2rad()`, and convert radians back to degrees using `np.rad2deg()`.82 83""")84st.subheader("**5. Sine Function (np.sin)** ๐ถ")85st.write("The `np.sin()` function computes the sine of each element in the array. Input angles must be in radians. Example: `np.sin(np.pi / 2)` returns `1.0` ๐")86 87st.subheader("**6. Cosine Function (np.cos)** ๐")88st.write("The `np.cos()` function computes the cosine of each element in the array. Example: `np.cos(0)` returns `1.0` โ
")89 90st.subheader("**7. Tangent Function (np.tan)** ๐ฅ")91st.write("The `np.tan()` function computes the tangent of each element in the array. Example: `np.tan(np.pi / 4)` returns `1.0` ๐งโ๐ซ")92 93st.subheader("**8. Convert Degrees to Radians (np.deg2rad)** โฉ๏ธ")94st.write("The `np.deg2rad()` function converts angles from degrees to radians. Example: `np.deg2rad(180)` returns `ฯ` or approximately `3.14159` ๐")95 96st.subheader("**9. Convert Radians to Degrees (np.rad2deg)** ๐")97st.write("The `np.rad2deg()` function converts angles from radians to degrees. Example: `np.rad2deg(np.pi)` returns `180` degrees ๐")98 99# Exponential and Logarithmic Functions100st.header(":blue[Exponential and Logarithmic Functions] ๐")101 102st.subheader("**10. Exponential Function (np.exp)** ๐")103st.write("The `np.exp()` function computes the exponential (e^x) of each element in the array. Example: `np.exp(1)` returns approximately `2.71828` ๐")104 105st.subheader("**11. Natural Logarithm (np.log)** ๐งฎ")106st.write("The `np.log()` function computes the natural logarithm (log base e) of each element in the array. Example: `np.log(np.e)` returns `1.0` ๐ข")107 108st.subheader("**12. Logarithm of (1 + x) (np.log1p)** ๐")109st.write("The `np.log1p()` function computes the natural logarithm of (1 + x). This is useful for small values of x. Example: `np.log1p(0)` returns `0.0` ๐")110 111st.subheader("**13. Logarithm Base 2 (np.log2)** 2๏ธโฃ")112st.write("The `np.log2()` function computes the base-2 logarithm of each element in the array. Example: `np.log2(8)` returns `3.0` ๐งโ๐ป")113 114st.subheader("**14. Logarithm Base 10 (np.log10)** ๐")115st.write("The `np.log10()` function computes the base-10 logarithm of each element in the array. Example: `np.log10(100)` returns `2.0` ๐")116 117# Array Manipulation Functions118st.header(":blue[Array Manipulation Functions] ๐")119 120st.subheader("**Flattening Arrays** ๐ฝ")121st.write("NumPy provides functions like `flatten()` and `ravel()` to convert multi-dimensional arrays into a 1D array.")122st.write("- `flatten()`: Returns a new 1D array.")123st.write("- `ravel()`: Returns a flattened array but may be a view.")124 125st.subheader("**Reshaping and Resizing Arrays** ๐")126st.write("You can reshape arrays using `reshape()` and resize them in place using `resize()`. Example usage below!")127 128st.subheader("**Joining and Splitting Arrays** ๐")129st.write("You can join arrays using `concatenate()` and split them into multiple sub-arrays with `split()`.")130 131st.subheader("**Repeat vs Tile** ๐")132st.write("""133- **`np.repeat()`**: Repeats individual elements.134- **`np.tile()`**: Repeats the entire array.135""")136 137# Example Usage of Repeat and Tile138st.subheader("**17. repeat** ๐")139st.write("Repeat individual elements using `np.repeat()`. Example: `np.repeat([1, 2, 3], 2)` โ `[1, 1, 2, 2, 3, 3]`.")140 141st.subheader("**18. tile** ๐งฉ")142st.write("Repeat the entire array using `np.tile()`. Example: `np.tile([1, 2], 2)` โ `[1, 2, 1, 2]`.")143 144st.subheader("**19. copy** ๐")145st.write("The `array.copy()` method creates a deep copy of an array, where changes to the copy won't affect the original array.")146 147st.subheader("**20. view** ๐")148st.write("The `array.view()` method returns a view of the original array, meaning changes to the view will affect the original.")149 150# End of the page151st.markdown("<hr>", unsafe_allow_html=True)152st.write(":green[Happy Coding! ๐]")153 154 