File size: 5,152 Bytes
d4e5a7c
 
 
5394934
d4e5a7c
5394934
 
 
d4e5a7c
 
5394934
 
 
d4e5a7c
 
5394934
 
 
d4e5a7c
 
5394934
 
 
d4e5a7c
 
5394934
 
 
 
d4e5a7c
 
5394934
 
 
 
d4e5a7c
 
5394934
 
 
 
d4e5a7c
 
5394934
 
 
 
d4e5a7c
 
5394934
d4e5a7c
5394934
 
d4e5a7c
 
5394934
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4e5a7c
 
5394934
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4e5a7c
 
5394934
d4e5a7c
 
 
 
5394934
 
d4e5a7c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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)