import streamlit as st import numpy as np # Example functions with explanations def example1(): explanation = "Importing the NumPy library." code = "import numpy as np" return explanation, code, None def example2(): explanation = "Creating a simple NumPy array." code = "array = np.array([1, 2, 3, 4, 5])\narray" array = np.array([1, 2, 3, 4, 5]) return explanation, code, array def example3(): explanation = "Creating an array with a range of values from 0 to 9." code = "array = np.arange(10)\narray" array = np.arange(10) return explanation, code, array def example4(): explanation = "Creating an array with 5 evenly spaced values between 0 and 1." code = "array = np.linspace(0, 1, 5)\narray" array = np.linspace(0, 1, 5) return explanation, code, array def example5(): explanation = "Reshaping a 2D array from shape (2, 3) to (3, 2)." code = "array = np.array([[1, 2, 3], [4, 5, 6]])\nreshaped_array = array.reshape((3, 2))\nreshaped_array" array = np.array([[1, 2, 3], [4, 5, 6]]) reshaped_array = array.reshape((3, 2)) return explanation, code, reshaped_array def example6(): explanation = "Slicing a 1D array to get elements from index 1 to 3." code = "array = np.array([1, 2, 3, 4, 5])\narray[1:4]" array = np.array([1, 2, 3, 4, 5]) sliced_array = array[1:4] return explanation, code, sliced_array def example7(): explanation = "Using fancy indexing to select specific elements from a 2D array." code = "array = np.array([[1, 2], [3, 4], [5, 6]])\nfancy_indexed_array = array[[0, 1], [1, 0]]\nfancy_indexed_array" array = np.array([[1, 2], [3, 4], [5, 6]]) fancy_indexed_array = array[[0, 1], [1, 0]] return explanation, code, fancy_indexed_array def example8(): explanation = "Using boolean indexing to select elements greater than 3." code = "array = np.array([1, 2, 3, 4, 5])\nboolean_indexed_array = array[array > 3]\nboolean_indexed_array" array = np.array([1, 2, 3, 4, 5]) boolean_indexed_array = array[array > 3] return explanation, code, boolean_indexed_array def example9(): explanation = "Performing element-wise multiplication of a 1D array by 2." code = "array = np.array([1, 2, 3, 4, 5])\narray * 2" array = np.array([1, 2, 3, 4, 5]) result = array * 2 return explanation, code, result def example10(): explanation = "Calculating the sum of all elements in a 2D array." code = "array = np.array([[1, 2, 3], [4, 5, 6]])\nnp.sum(array)" array = np.array([[1, 2, 3], [4, 5, 6]]) result = np.sum(array) return explanation, code, result def example11(): explanation = "Calculating the dot product of two matrices." code = "matrix = np.array([[1, 2], [3, 4]])\nnp.dot(matrix, matrix)" matrix = np.array([[1, 2], [3, 4]]) result = np.dot(matrix, matrix) return explanation, code, result def example12(): explanation = "Performing broadcasting by adding two 1D arrays element-wise." code = "array = np.array([1, 2, 3])\narray + np.array([4, 5, 6])" array = np.array([1, 2, 3]) result = array + np.array([4, 5, 6]) return explanation, code, result def example13(): explanation = "Generating a 2x2 array with random values between 0 and 1." code = "random_array = np.random.random((2, 2))\nrandom_array" random_array = np.random.random((2, 2)) return explanation, code, random_array def example14(): explanation = "Sorting a 1D array in ascending order." code = "array = np.array([3, 1, 2])\nnp.sort(array)" array = np.array([3, 1, 2]) sorted_array = np.sort(array) return explanation, code, sorted_array def example15(): explanation = "Finding the index of a value in a sorted array." code = "array = np.array([1, 2, 3, 4, 5])\nnp.searchsorted(array, 3)" array = np.array([1, 2, 3, 4, 5]) index = np.searchsorted(array, 3) return explanation, code, index def example16(): from skimage import data explanation = "Calculating the mean value of an image." code = "from skimage import data\nimage = data.camera()\nnp.mean(image)" image = data.camera() mean_value = np.mean(image) return explanation, code, mean_value def example17(): explanation = "Simulating random positions and velocities in a 2D space." code = "positions = np.random.random((10, 2))\nvelocities = np.random.random((10, 2))\npositions + velocities" positions = np.random.random((10, 2)) velocities = np.random.random((10, 2)) result = positions + velocities return explanation, code, result def example18(): explanation = "Calculating the mean of each column in a 2D array." code = "data = np.random.random((100, 4))\nnp.mean(data, axis=0)" data = np.random.random((100, 4)) mean_values = np.mean(data, axis=0) return explanation, code, mean_values def example19(): explanation = "Calculating the element-wise power of a 1D array." code = "array = np.array([1, 2, 3])\nnp.power(array, 3)" array = np.array([1, 2, 3]) result = np.power(array, 3) return explanation, code, result def example20(): explanation = "Calculating the cumulative sum of a 1D array." code = "array = np.array([1, 2, 3, 4, 5])\nnp.cumsum(array)" array = np.array([1, 2, 3, 4, 5]) result = np.cumsum(array) return explanation, code, result examples = [ ("Example 1: Import NumPy", example1), ("Example 2: Create a simple array", example2), ("Example 3: Create an array with a range of values", example3), ("Example 4: Create an array with evenly spaced values using linspace", example4), ("Example 5: Reshape a 2D array", example5), ("Example 6: Slice a 1D array", example6), ("Example 7: Fancy indexing on a 2D array", example7), ("Example 8: Boolean indexing on a 1D array", example8), ("Example 9: Element-wise multiplication", example9), ("Example 10: Sum of all elements in a 2D array", example10), ("Example 11: Dot product of two matrices", example11), ("Example 12: Broadcasting example", example12), ("Example 13: Generate random numbers", example13), ("Example 14: Sort an array", example14), ("Example 15: Search for a value in a sorted array", example15), ("Example 16: Mean value of an image", example16), ("Example 17: Numerical simulation with random positions and velocities", example17), ("Example 18: Mean of each column in a 2D array", example18), ("Example 19: Element-wise power", example19), ("Example 20: Cumulative sum of an array", example20), ] st.title("NumPy Course with Streamlit") for title, func in examples: st.header(title) explanation, code, result = func() st.write(explanation) st.code(code) if st.button(f"Run {title.split(':')[0]}"): if result is not None: st.write("Output:", result)