Spaces:
Sleeping
Sleeping
eaglelandsonce
commited on
Update pages/3_NumpyBasics.py
Browse files- pages/3_NumpyBasics.py +44 -23
pages/3_NumpyBasics.py
CHANGED
@@ -1,123 +1,143 @@
|
|
1 |
import streamlit as st
|
2 |
import numpy as np
|
3 |
|
4 |
-
# Example functions
|
5 |
def example1():
|
|
|
6 |
code = "import numpy as np"
|
7 |
exec(code)
|
8 |
-
return code
|
9 |
|
10 |
def example2():
|
|
|
11 |
code = "array = np.array([1, 2, 3, 4, 5])\narray"
|
12 |
array = np.array([1, 2, 3, 4, 5])
|
13 |
-
return code, array
|
14 |
|
15 |
def example3():
|
|
|
16 |
code = "array = np.arange(10)\narray"
|
17 |
array = np.arange(10)
|
18 |
-
return code, array
|
19 |
|
20 |
def example4():
|
|
|
21 |
code = "array = np.linspace(0, 1, 5)\narray"
|
22 |
array = np.linspace(0, 1, 5)
|
23 |
-
return code, array
|
24 |
|
25 |
def example5():
|
|
|
26 |
code = "array = np.array([[1, 2, 3], [4, 5, 6]])\nreshaped_array = array.reshape((3, 2))\nreshaped_array"
|
27 |
array = np.array([[1, 2, 3], [4, 5, 6]])
|
28 |
reshaped_array = array.reshape((3, 2))
|
29 |
-
return code, reshaped_array
|
30 |
|
31 |
def example6():
|
|
|
32 |
code = "array = np.array([1, 2, 3, 4, 5])\narray[1:4]"
|
33 |
array = np.array([1, 2, 3, 4, 5])
|
34 |
sliced_array = array[1:4]
|
35 |
-
return code, sliced_array
|
36 |
|
37 |
def example7():
|
|
|
38 |
code = "array = np.array([[1, 2], [3, 4], [5, 6]])\nfancy_indexed_array = array[[0, 1], [1, 0]]\nfancy_indexed_array"
|
39 |
array = np.array([[1, 2], [3, 4], [5, 6]])
|
40 |
fancy_indexed_array = array[[0, 1], [1, 0]]
|
41 |
-
return code, fancy_indexed_array
|
42 |
|
43 |
def example8():
|
|
|
44 |
code = "array = np.array([1, 2, 3, 4, 5])\nboolean_indexed_array = array[array > 3]\nboolean_indexed_array"
|
45 |
array = np.array([1, 2, 3, 4, 5])
|
46 |
boolean_indexed_array = array[array > 3]
|
47 |
-
return code, boolean_indexed_array
|
48 |
|
49 |
def example9():
|
|
|
50 |
code = "array = np.array([1, 2, 3, 4, 5])\narray * 2"
|
51 |
array = np.array([1, 2, 3, 4, 5])
|
52 |
result = array * 2
|
53 |
-
return code, result
|
54 |
|
55 |
def example10():
|
|
|
56 |
code = "array = np.array([[1, 2, 3], [4, 5, 6]])\nnp.sum(array)"
|
57 |
array = np.array([[1, 2, 3], [4, 5, 6]])
|
58 |
result = np.sum(array)
|
59 |
-
return code, result
|
60 |
|
61 |
def example11():
|
|
|
62 |
code = "matrix = np.array([[1, 2], [3, 4]])\nnp.dot(matrix, matrix)"
|
63 |
matrix = np.array([[1, 2], [3, 4]])
|
64 |
result = np.dot(matrix, matrix)
|
65 |
-
return code, result
|
66 |
|
67 |
def example12():
|
|
|
68 |
code = "array = np.array([1, 2, 3])\narray + np.array([4, 5, 6])"
|
69 |
array = np.array([1, 2, 3])
|
70 |
result = array + np.array([4, 5, 6])
|
71 |
-
return code, result
|
72 |
|
73 |
def example13():
|
|
|
74 |
code = "random_array = np.random.random((2, 2))\nrandom_array"
|
75 |
random_array = np.random.random((2, 2))
|
76 |
-
return code, random_array
|
77 |
|
78 |
def example14():
|
|
|
79 |
code = "array = np.array([3, 1, 2])\nnp.sort(array)"
|
80 |
array = np.array([3, 1, 2])
|
81 |
sorted_array = np.sort(array)
|
82 |
-
return code, sorted_array
|
83 |
|
84 |
def example15():
|
|
|
85 |
code = "array = np.array([1, 2, 3, 4, 5])\nnp.searchsorted(array, 3)"
|
86 |
array = np.array([1, 2, 3, 4, 5])
|
87 |
index = np.searchsorted(array, 3)
|
88 |
-
return code, index
|
89 |
|
90 |
def example16():
|
91 |
from skimage import data
|
|
|
92 |
code = "from skimage import data\nimage = data.camera()\nnp.mean(image)"
|
93 |
image = data.camera()
|
94 |
mean_value = np.mean(image)
|
95 |
-
return code, mean_value
|
96 |
|
97 |
def example17():
|
|
|
98 |
code = "positions = np.random.random((10, 2))\nvelocities = np.random.random((10, 2))\npositions + velocities"
|
99 |
positions = np.random.random((10, 2))
|
100 |
velocities = np.random.random((10, 2))
|
101 |
result = positions + velocities
|
102 |
-
return code, result
|
103 |
|
104 |
def example18():
|
|
|
105 |
code = "data = np.random.random((100, 4))\nnp.mean(data, axis=0)"
|
106 |
data = np.random.random((100, 4))
|
107 |
mean_values = np.mean(data, axis=0)
|
108 |
-
return code, mean_values
|
109 |
|
110 |
def example19():
|
|
|
111 |
code = "array = np.array([1, 2, 3])\nnp.power(array, 3)"
|
112 |
array = np.array([1, 2, 3])
|
113 |
result = np.power(array, 3)
|
114 |
-
return code, result
|
115 |
|
116 |
def example20():
|
|
|
117 |
code = "array = np.array([1, 2, 3, 4, 5])\nnp.cumsum(array)"
|
118 |
array = np.array([1, 2, 3, 4, 5])
|
119 |
result = np.cumsum(array)
|
120 |
-
return code, result
|
121 |
|
122 |
examples = [
|
123 |
("Example 1: Import NumPy", example1),
|
@@ -146,7 +166,8 @@ st.title("NumPy Course with Streamlit")
|
|
146 |
|
147 |
for title, func in examples:
|
148 |
st.header(title)
|
|
|
|
|
|
|
149 |
if st.button(f"Run {title.split(':')[0]}"):
|
150 |
-
code, result = func()
|
151 |
-
st.code(code)
|
152 |
st.write("Output:", result)
|
|
|
1 |
import streamlit as st
|
2 |
import numpy as np
|
3 |
|
4 |
+
# Example functions with explanations
|
5 |
def example1():
|
6 |
+
explanation = "Importing the NumPy library."
|
7 |
code = "import numpy as np"
|
8 |
exec(code)
|
9 |
+
return explanation, code
|
10 |
|
11 |
def example2():
|
12 |
+
explanation = "Creating a simple NumPy array."
|
13 |
code = "array = np.array([1, 2, 3, 4, 5])\narray"
|
14 |
array = np.array([1, 2, 3, 4, 5])
|
15 |
+
return explanation, code, array
|
16 |
|
17 |
def example3():
|
18 |
+
explanation = "Creating an array with a range of values from 0 to 9."
|
19 |
code = "array = np.arange(10)\narray"
|
20 |
array = np.arange(10)
|
21 |
+
return explanation, code, array
|
22 |
|
23 |
def example4():
|
24 |
+
explanation = "Creating an array with 5 evenly spaced values between 0 and 1."
|
25 |
code = "array = np.linspace(0, 1, 5)\narray"
|
26 |
array = np.linspace(0, 1, 5)
|
27 |
+
return explanation, code, array
|
28 |
|
29 |
def example5():
|
30 |
+
explanation = "Reshaping a 2D array from shape (2, 3) to (3, 2)."
|
31 |
code = "array = np.array([[1, 2, 3], [4, 5, 6]])\nreshaped_array = array.reshape((3, 2))\nreshaped_array"
|
32 |
array = np.array([[1, 2, 3], [4, 5, 6]])
|
33 |
reshaped_array = array.reshape((3, 2))
|
34 |
+
return explanation, code, reshaped_array
|
35 |
|
36 |
def example6():
|
37 |
+
explanation = "Slicing a 1D array to get elements from index 1 to 3."
|
38 |
code = "array = np.array([1, 2, 3, 4, 5])\narray[1:4]"
|
39 |
array = np.array([1, 2, 3, 4, 5])
|
40 |
sliced_array = array[1:4]
|
41 |
+
return explanation, code, sliced_array
|
42 |
|
43 |
def example7():
|
44 |
+
explanation = "Using fancy indexing to select specific elements from a 2D array."
|
45 |
code = "array = np.array([[1, 2], [3, 4], [5, 6]])\nfancy_indexed_array = array[[0, 1], [1, 0]]\nfancy_indexed_array"
|
46 |
array = np.array([[1, 2], [3, 4], [5, 6]])
|
47 |
fancy_indexed_array = array[[0, 1], [1, 0]]
|
48 |
+
return explanation, code, fancy_indexed_array
|
49 |
|
50 |
def example8():
|
51 |
+
explanation = "Using boolean indexing to select elements greater than 3."
|
52 |
code = "array = np.array([1, 2, 3, 4, 5])\nboolean_indexed_array = array[array > 3]\nboolean_indexed_array"
|
53 |
array = np.array([1, 2, 3, 4, 5])
|
54 |
boolean_indexed_array = array[array > 3]
|
55 |
+
return explanation, code, boolean_indexed_array
|
56 |
|
57 |
def example9():
|
58 |
+
explanation = "Performing element-wise multiplication of a 1D array by 2."
|
59 |
code = "array = np.array([1, 2, 3, 4, 5])\narray * 2"
|
60 |
array = np.array([1, 2, 3, 4, 5])
|
61 |
result = array * 2
|
62 |
+
return explanation, code, result
|
63 |
|
64 |
def example10():
|
65 |
+
explanation = "Calculating the sum of all elements in a 2D array."
|
66 |
code = "array = np.array([[1, 2, 3], [4, 5, 6]])\nnp.sum(array)"
|
67 |
array = np.array([[1, 2, 3], [4, 5, 6]])
|
68 |
result = np.sum(array)
|
69 |
+
return explanation, code, result
|
70 |
|
71 |
def example11():
|
72 |
+
explanation = "Calculating the dot product of two matrices."
|
73 |
code = "matrix = np.array([[1, 2], [3, 4]])\nnp.dot(matrix, matrix)"
|
74 |
matrix = np.array([[1, 2], [3, 4]])
|
75 |
result = np.dot(matrix, matrix)
|
76 |
+
return explanation, code, result
|
77 |
|
78 |
def example12():
|
79 |
+
explanation = "Performing broadcasting by adding two 1D arrays element-wise."
|
80 |
code = "array = np.array([1, 2, 3])\narray + np.array([4, 5, 6])"
|
81 |
array = np.array([1, 2, 3])
|
82 |
result = array + np.array([4, 5, 6])
|
83 |
+
return explanation, code, result
|
84 |
|
85 |
def example13():
|
86 |
+
explanation = "Generating a 2x2 array with random values between 0 and 1."
|
87 |
code = "random_array = np.random.random((2, 2))\nrandom_array"
|
88 |
random_array = np.random.random((2, 2))
|
89 |
+
return explanation, code, random_array
|
90 |
|
91 |
def example14():
|
92 |
+
explanation = "Sorting a 1D array in ascending order."
|
93 |
code = "array = np.array([3, 1, 2])\nnp.sort(array)"
|
94 |
array = np.array([3, 1, 2])
|
95 |
sorted_array = np.sort(array)
|
96 |
+
return explanation, code, sorted_array
|
97 |
|
98 |
def example15():
|
99 |
+
explanation = "Finding the index of a value in a sorted array."
|
100 |
code = "array = np.array([1, 2, 3, 4, 5])\nnp.searchsorted(array, 3)"
|
101 |
array = np.array([1, 2, 3, 4, 5])
|
102 |
index = np.searchsorted(array, 3)
|
103 |
+
return explanation, code, index
|
104 |
|
105 |
def example16():
|
106 |
from skimage import data
|
107 |
+
explanation = "Calculating the mean value of an image."
|
108 |
code = "from skimage import data\nimage = data.camera()\nnp.mean(image)"
|
109 |
image = data.camera()
|
110 |
mean_value = np.mean(image)
|
111 |
+
return explanation, code, mean_value
|
112 |
|
113 |
def example17():
|
114 |
+
explanation = "Simulating random positions and velocities in a 2D space."
|
115 |
code = "positions = np.random.random((10, 2))\nvelocities = np.random.random((10, 2))\npositions + velocities"
|
116 |
positions = np.random.random((10, 2))
|
117 |
velocities = np.random.random((10, 2))
|
118 |
result = positions + velocities
|
119 |
+
return explanation, code, result
|
120 |
|
121 |
def example18():
|
122 |
+
explanation = "Calculating the mean of each column in a 2D array."
|
123 |
code = "data = np.random.random((100, 4))\nnp.mean(data, axis=0)"
|
124 |
data = np.random.random((100, 4))
|
125 |
mean_values = np.mean(data, axis=0)
|
126 |
+
return explanation, code, mean_values
|
127 |
|
128 |
def example19():
|
129 |
+
explanation = "Calculating the element-wise power of a 1D array."
|
130 |
code = "array = np.array([1, 2, 3])\nnp.power(array, 3)"
|
131 |
array = np.array([1, 2, 3])
|
132 |
result = np.power(array, 3)
|
133 |
+
return explanation, code, result
|
134 |
|
135 |
def example20():
|
136 |
+
explanation = "Calculating the cumulative sum of a 1D array."
|
137 |
code = "array = np.array([1, 2, 3, 4, 5])\nnp.cumsum(array)"
|
138 |
array = np.array([1, 2, 3, 4, 5])
|
139 |
result = np.cumsum(array)
|
140 |
+
return explanation, code, result
|
141 |
|
142 |
examples = [
|
143 |
("Example 1: Import NumPy", example1),
|
|
|
166 |
|
167 |
for title, func in examples:
|
168 |
st.header(title)
|
169 |
+
explanation, code, result = func()
|
170 |
+
st.write(explanation)
|
171 |
+
st.code(code)
|
172 |
if st.button(f"Run {title.split(':')[0]}"):
|
|
|
|
|
173 |
st.write("Output:", result)
|