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