Team Ai
Apppublic

Jagadeesh2411/Numpy_Notes

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
6_NumPy Functions.py142 linesDownload Raw Back to root
1import streamlit as st2import numpy as np3 4st.title(":green[Exploring NumPy Operations with Fun Examples ๐ŸŽ‰]")5 6st.markdown("""7<hr style='border: none; height: 0; border-top: 3px dashed; border-image: linear-gradient(to right, blue, green) 1;'>8""", unsafe_allow_html=True)9 10# Creating different arrays for demonstration11array_1d = np.array([10, 20, 30, 40, 50])12array_2d = np.array([[1, 2], [3, 4], [5, 6]])13array_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])14 15# Displaying the arrays16st.write(":star: **1D Array**:")17st.code(array_1d)18 19st.write(":star: **2D Array**:")20st.code(array_2d)21 22st.write(":star: **3D Array**:")23st.code(array_3d)24 25# Function to display results26def show_example(func_name, func, example_input, result):27    st.subheader(func_name)28    st.write(f"**Input:** {example_input}")29    st.write(f"**Result:** {result}")30    st.write("---")31 32# Basic Operations with Arrays33st.header("๐Ÿ”ข Basic NumPy Arithmetic Operations")34 35# Addition36st.subheader("โž• The `np.add()` function adds a constant to each element in the array.")37add_result_1d = np.add(array_1d, 5)38add_result_2d = np.add(array_2d, 5)39add_result_3d = np.add(array_3d, 5)40 41show_example("Add (1D)", "np.add(array_1d, 5)", array_1d, add_result_1d)42show_example("Add (2D)", "np.add(array_2d, 5)", array_2d, add_result_2d)43show_example("Add (3D)", "np.add(array_3d, 5)", array_3d, add_result_3d)44 45# Subtraction46st.subheader("โž– The `np.subtract()` function subtracts a constant from each element in the array.")47sub_result_1d = np.subtract(array_1d, 5)48sub_result_2d = np.subtract(array_2d, 5)49sub_result_3d = np.subtract(array_3d, 5)50 51show_example("Subtract (1D)", "np.subtract(array_1d, 5)", array_1d, sub_result_1d)52show_example("Subtract (2D)", "np.subtract(array_2d, 5)", array_2d, sub_result_2d)53show_example("Subtract (3D)", "np.subtract(array_3d, 5)", array_3d, sub_result_3d)54 55# Multiplication56st.subheader("โœ–๏ธ The `np.multiply()` function multiplies each element in the array by a constant.")57mul_result_1d = np.multiply(array_1d, 3)58mul_result_2d = np.multiply(array_2d, 3)59mul_result_3d = np.multiply(array_3d, 3)60 61show_example("Multiply (1D)", "np.multiply(array_1d, 3)", array_1d, mul_result_1d)62show_example("Multiply (2D)", "np.multiply(array_2d, 3)", array_2d, mul_result_2d)63show_example("Multiply (3D)", "np.multiply(array_3d, 3)", array_3d, mul_result_3d)64 65# Division66st.subheader("โž— The `np.divide()` function divides each element by a constant.")67div_result_1d = np.divide(array_1d, 5)68div_result_2d = np.divide(array_2d, 5)69div_result_3d = np.divide(array_3d, 5)70 71show_example("Divide (1D)", "np.divide(array_1d, 5)", array_1d, div_result_1d)72show_example("Divide (2D)", "np.divide(array_2d, 5)", array_2d, div_result_2d)73show_example("Divide (3D)", "np.divide(array_3d, 5)", array_3d, div_result_3d)74 75# Modulo Operation76st.subheader("๐Ÿ”ข The `np.mod()` function returns the remainder of division for each element.")77mod_result_1d = np.mod(array_1d, 4)78mod_result_2d = np.mod(array_2d, 4)79mod_result_3d = np.mod(array_3d, 4)80 81show_example("Mod (1D)", "np.mod(array_1d, 4)", array_1d, mod_result_1d)82show_example("Mod (2D)", "np.mod(array_2d, 4)", array_2d, mod_result_2d)83show_example("Mod (3D)", "np.mod(array_3d, 4)", array_3d, mod_result_3d)84 85# Reciprocal86st.subheader("๐Ÿ”„ The `np.reciprocal()` function computes the reciprocal (1/x) for each element.")87rec_result_1d = np.reciprocal(array_1d)88rec_result_2d = np.reciprocal(array_2d)89rec_result_3d = np.reciprocal(array_3d)90 91show_example("Reciprocal (1D)", "np.reciprocal(array_1d)", array_1d, rec_result_1d)92show_example("Reciprocal (2D)", "np.reciprocal(array_2d)", array_2d, rec_result_2d)93show_example("Reciprocal (3D)", "np.reciprocal(array_3d)", array_3d, rec_result_3d)94 95# Handling NaN and Inf Values96st.header("โš ๏ธ Handling NaN and Inf in Arrays")97 98array_with_nan = np.array([1, 2, np.nan, 4, np.nan])99array_with_inf = np.array([1, 2, np.inf, 4, -np.inf])100 101st.write("The `np.nansum()` function computes the sum of an array, ignoring NaN values. It effectively treats NaN as zero.")102st.write("The `np.sum()` function includes infinity values in the sum, affecting the result accordingly.")103 104sum_nan = np.nansum(array_with_nan)105sum_inf = np.sum(array_with_inf)106 107show_example("Sum (NaN)", "np.nansum(array_with_nan)", array_with_nan, sum_nan)108show_example("Sum (Inf)", "np.sum(array_with_inf)", array_with_inf, sum_inf)109 110# Cumulative Sum111st.write("The `np.nancumsum()` function computes the cumulative sum while ignoring NaN values.")112st.write("The `np.cumsum()` function computes the cumulative sum, including infinity values.")113 114cumsum_nan = np.nancumsum(array_with_nan)115cumsum_inf = np.cumsum(array_with_inf)116 117show_example("Cumulative Sum (NaN)", "np.nancumsum(array_with_nan)", array_with_nan, cumsum_nan)118show_example("Cumulative Sum (Inf)", "np.cumsum(array_with_inf)", array_with_inf, cumsum_inf)119 120# Product of Elements121st.write("The `np.nanprod()` function computes the product of array elements, ignoring NaN values.")122st.write("The `np.prod()` function computes the product, including infinity values.")123 124prod_nan = np.nanprod(array_with_nan)125prod_inf = np.prod(array_with_inf)126 127show_example("Product (NaN)", "np.nanprod(array_with_nan)", array_with_nan, prod_nan)128show_example("Product (Inf)", "np.prod(array_with_inf)", array_with_inf, prod_inf)129 130# Cumulative Product131st.write("The `np.nancumprod()` function computes the cumulative product while ignoring NaN values.")132st.write("The `np.cumprod()` function computes the cumulative product, including infinity values.")133 134cumprod_nan = np.nancumprod(array_with_nan)135cumprod_inf = np.cumprod(array_with_inf)136 137show_example("Cumulative Product (NaN)", "np.nancumprod(array_with_nan)", array_with_nan, cumprod_nan)138show_example("Cumulative Product (Inf)", "np.cumprod(array_with_inf)", array_with_inf, cumprod_inf)139 140# Conclusion141st.write("๐ŸŒŸ **Conclusion:** NumPy provides powerful tools to perform efficient operations on arrays, handling complex mathematical operations, as well as special cases such as `NaN` and `Inf` values. ๐Ÿš€")142