eaglelandsonce commited on
Commit
5394934
·
verified ·
1 Parent(s): d4e5a7c

Update pages/3_NumpyBasics.py

Browse files
Files changed (1) hide show
  1. pages/3_NumpyBasics.py +120 -33
pages/3_NumpyBasics.py CHANGED
@@ -1,65 +1,152 @@
1
  import streamlit as st
2
  import numpy as np
3
 
 
4
  def example1():
5
- array = np.array([1, 2, 3, 4, 5])
6
- return array
 
7
 
8
  def example2():
9
- array = np.arange(0, 10, 2)
10
- return array
 
11
 
12
  def example3():
13
- array = np.linspace(0, 1, 5)
14
- return array
 
15
 
16
  def example4():
17
- array = np.zeros((3, 3))
18
- return array
 
19
 
20
  def example5():
21
- array = np.ones((2, 3))
22
- return array
 
 
23
 
24
  def example6():
25
- array = np.eye(4)
26
- return array
 
 
27
 
28
  def example7():
29
- array = np.random.random((2, 3))
30
- return array
 
 
31
 
32
  def example8():
33
- array = np.random.randint(1, 10, size=(3, 3))
34
- return array
 
 
35
 
36
  def example9():
 
37
  array = np.array([1, 2, 3, 4, 5])
38
- reshaped_array = array.reshape((5, 1))
39
- return reshaped_array
40
 
41
  def example10():
42
- array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
43
- transposed_array = np.transpose(array)
44
- return transposed_array
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  examples = [
47
- ("Example 1: Create a simple array", example1),
48
- ("Example 2: Create an array with a range of values using arange", example2),
49
- ("Example 3: Create an array with evenly spaced values using linspace", example3),
50
- ("Example 4: Create a 3x3 matrix of zeros", example4),
51
- ("Example 5: Create a 2x3 matrix of ones", example5),
52
- ("Example 6: Create an identity matrix", example6),
53
- ("Example 7: Create a 2x3 matrix with random values", example7),
54
- ("Example 8: Create a 3x3 matrix with random integers", example8),
55
- ("Example 9: Reshape a 1D array to a 2D array", example9),
56
- ("Example 10: Transpose a 2D array", example10),
 
 
 
 
 
 
 
 
 
 
57
  ]
58
 
59
- st.title("NumPy Essentials with Streamlit")
60
 
61
  for title, func in examples:
62
  st.header(title)
63
  if st.button(f"Run {title.split(':')[0]}"):
64
- result = func()
 
65
  st.write("Output:", result)
 
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),
124
+ ("Example 2: Create a simple array", example2),
125
+ ("Example 3: Create an array with a range of values", example3),
126
+ ("Example 4: Create an array with evenly spaced values using linspace", example4),
127
+ ("Example 5: Reshape a 2D array", example5),
128
+ ("Example 6: Slice a 1D array", example6),
129
+ ("Example 7: Fancy indexing on a 2D array", example7),
130
+ ("Example 8: Boolean indexing on a 1D array", example8),
131
+ ("Example 9: Element-wise multiplication", example9),
132
+ ("Example 10: Sum of all elements in a 2D array", example10),
133
+ ("Example 11: Dot product of two matrices", example11),
134
+ ("Example 12: Broadcasting example", example12),
135
+ ("Example 13: Generate random numbers", example13),
136
+ ("Example 14: Sort an array", example14),
137
+ ("Example 15: Search for a value in a sorted array", example15),
138
+ ("Example 16: Mean value of an image", example16),
139
+ ("Example 17: Numerical simulation with random positions and velocities", example17),
140
+ ("Example 18: Mean of each column in a 2D array", example18),
141
+ ("Example 19: Element-wise power", example19),
142
+ ("Example 20: Cumulative sum of an array", example20),
143
  ]
144
 
145
+ 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)