TensorFlowClass / pages /3_NumpyBasics.py
eaglelandsonce's picture
Update pages/3_NumpyBasics.py
5394934 verified
raw
history blame
5.15 kB
import streamlit as st
import numpy as np
# Example functions
def example1():
code = "import numpy as np"
exec(code)
return code
def example2():
code = "array = np.array([1, 2, 3, 4, 5])\narray"
array = np.array([1, 2, 3, 4, 5])
return code, array
def example3():
code = "array = np.arange(10)\narray"
array = np.arange(10)
return code, array
def example4():
code = "array = np.linspace(0, 1, 5)\narray"
array = np.linspace(0, 1, 5)
return code, array
def example5():
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 code, reshaped_array
def example6():
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 code, sliced_array
def example7():
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 code, fancy_indexed_array
def example8():
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 code, boolean_indexed_array
def example9():
code = "array = np.array([1, 2, 3, 4, 5])\narray * 2"
array = np.array([1, 2, 3, 4, 5])
result = array * 2
return code, result
def example10():
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 code, result
def example11():
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 code, result
def example12():
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 code, result
def example13():
code = "random_array = np.random.random((2, 2))\nrandom_array"
random_array = np.random.random((2, 2))
return code, random_array
def example14():
code = "array = np.array([3, 1, 2])\nnp.sort(array)"
array = np.array([3, 1, 2])
sorted_array = np.sort(array)
return code, sorted_array
def example15():
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 code, index
def example16():
from skimage import data
code = "from skimage import data\nimage = data.camera()\nnp.mean(image)"
image = data.camera()
mean_value = np.mean(image)
return code, mean_value
def example17():
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 code, result
def example18():
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 code, mean_values
def example19():
code = "array = np.array([1, 2, 3])\nnp.power(array, 3)"
array = np.array([1, 2, 3])
result = np.power(array, 3)
return code, result
def example20():
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 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)
if st.button(f"Run {title.split(':')[0]}"):
code, result = func()
st.code(code)
st.write("Output:", result)