Team Ai
Apppublic

Jagadeesh2411/Numpy_Notes

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
7_Numpy-trignometric functions-Few-Functions.py154 linesDownload Raw Back to pages
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