Spaces:
Sleeping
Sleeping
import streamlit as st | |
import numpy as np | |
# Example functions with explanations | |
def example1(): | |
explanation = "Creating a simple 1D 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 example2(): | |
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 example3(): | |
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 example4(): | |
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 example5(): | |
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 example6(): | |
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 example7(): | |
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 example8(): | |
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 example9(): | |
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 example10(): | |
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 example11(): | |
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 example12(): | |
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 example13(): | |
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 example14(): | |
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 example15(): | |
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 example16(): | |
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 example17(): | |
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 example18(): | |
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 example19(): | |
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 | |
def example20(): | |
explanation = "Generating a 3x3 identity matrix." | |
code = "identity_matrix = np.eye(3)\nidentity_matrix" | |
identity_matrix = np.eye(3) | |
return explanation, code, identity_matrix | |
examples = [ | |
("Example 1: Create a simple NumPy array", example1), | |
("Example 2: Create an array with a range of values", example2), | |
("Example 3: Create an array with evenly spaced values using linspace", example3), | |
("Example 4: Reshape a 2D array", example4), | |
("Example 5: Slice a 1D array", example5), | |
("Example 6: Fancy indexing on a 2D array", example6), | |
("Example 7: Boolean indexing on a 1D array", example7), | |
("Example 8: Element-wise multiplication", example8), | |
("Example 9: Sum of all elements in a 2D array", example9), | |
("Example 10: Dot product of two matrices", example10), | |
("Example 11: Broadcasting example", example11), | |
("Example 12: Generate random numbers", example12), | |
("Example 13: Sort an array", example13), | |
("Example 14: Search for a value in a sorted array", example14), | |
("Example 15: Mean value of an image", example15), | |
("Example 16: Numerical simulation with random positions and velocities", example16), | |
("Example 17: Mean of each column in a 2D array", example17), | |
("Example 18: Element-wise power", example18), | |
("Example 19: Cumulative sum of an array", example19), | |
("Example 20: Generate a 3x3 identity matrix", 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) | |