lee-ite commited on
Commit
8060008
·
verified ·
1 Parent(s): d286ff3

Upload 4_Compare.py

Browse files
Files changed (1) hide show
  1. pages/4_Compare.py +231 -0
pages/4_Compare.py ADDED
@@ -0,0 +1,231 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ import pandas as pd
4
+ import matplotlib.pyplot as plt
5
+ from sklearn.metrics import r2_score
6
+
7
+ st.title("Compare Your Algorithm")
8
+
9
+ default_dataOne = {
10
+ "X": [1, 2, 3, 4, 5],
11
+ "Y": [2.2, 4.4, 6.5, 8.0, 10.1],
12
+ "Select": [True, True, True, True, True]
13
+ }
14
+ default_dataTwo = {
15
+ "X": [1, 10, 100, 1000, 10000],
16
+ "Y": [3.3, 6.6, 12.21, 24.84, 48.25],
17
+ "Select": [True, True, True, True, True]
18
+ }
19
+ dataOne = pd.DataFrame(default_dataOne)
20
+ dataTwo = pd.DataFrame(default_dataTwo)
21
+
22
+ xlabel = st.text_input("X-axis", "X")
23
+ ylabel = st.text_input("Y-axis", "Y")
24
+
25
+ col1, col2 = st.columns(2)
26
+
27
+ with col1:
28
+ st.subheader("Enter Your Data One")
29
+ cola, colb = st.columns(2)
30
+ with cola:
31
+ user_dataOne = st.data_editor(dataOne, num_rows="dynamic", key="data_editor_one")
32
+ with colb:
33
+ fit_typeOne = st.radio(
34
+ "Choose the Type of Fit",
35
+ options=["Logarithmic", "Linear", "Linearithmic", "Quadratic", "Cubic", "Exponential"],
36
+ index=1,
37
+ key="one"
38
+ )
39
+
40
+ with col2:
41
+ st.subheader("Enter Your Data Two")
42
+ colc, cold = st.columns(2)
43
+ with colc:
44
+ user_dataTwo = st.data_editor(dataTwo, num_rows="dynamic", key="data_editor_two")
45
+ with cold:
46
+ fit_typeTwo = st.radio(
47
+ "Choose the Type of Fit",
48
+ options=["Logarithmic", "Linear", "Linearithmic", "Quadratic", "Cubic", "Exponential"],
49
+ index=0,
50
+ key="two"
51
+ )
52
+
53
+ try:
54
+ selected_dataOne = user_dataOne[user_dataOne["Select"]]
55
+ x = np.array(selected_dataOne["X"], dtype=float)
56
+ y = np.array(selected_dataOne["Y"], dtype=float)
57
+
58
+ if len(x) < 2 and len(y) < 2:
59
+ st.warning("Please enter at least 2 data points.")
60
+ st.stop()
61
+ except ValueError:
62
+ st.error("Invalid data entered. Please ensure all values are numeric.")
63
+ st.stop()
64
+
65
+ try:
66
+ selected_dataTwo = user_dataTwo[user_dataTwo["Select"]]
67
+ u = np.array(selected_dataTwo["X"], dtype=float)
68
+ v = np.array(selected_dataTwo["Y"], dtype=float)
69
+
70
+ if len(u) < 2 and len(v) < 2:
71
+ st.warning("Please enter at least 2 data points.")
72
+ st.stop()
73
+ except ValueError:
74
+ st.error("Invalid data entered. Please ensure all values are numeric.")
75
+ st.stop()
76
+
77
+ if fit_typeOne == "Logarithmic":
78
+ try:
79
+ log_x = np.log(x)
80
+ coefficients = np.polyfit(log_x, y , 1)
81
+ y_fit = coefficients[0] * log_x + coefficients[1]
82
+ r2 = r2_score(y, y_fit)
83
+ equation = f"y = {coefficients[0]:.4f}*log(x) + {coefficients[1]:.4f}"
84
+ except ValueError:
85
+ st.error("Logarithmic fit failed. Ensure all X values are positive.")
86
+ st.stop()
87
+
88
+ elif fit_typeOne == "Linear":
89
+ degree = 1
90
+ coefficients = np.polyfit(x, y, degree)
91
+ y_fit = np.polyval(coefficients, x)
92
+ r2 = r2_score(y, y_fit)
93
+ equation = f"y = {coefficients[0]:.4f}*x + {coefficients[1]:.4f}"
94
+
95
+ elif fit_typeOne == "Linearithmic":
96
+ try:
97
+ x_log_x = x * np.log(x)
98
+ A = np.column_stack((x_log_x, x, np.ones_like(x)))
99
+ coefficients, _, _, _ = np.linalg.lstsq(A, y, rcond=None)
100
+ a, b, c = coefficients
101
+ y_fit = a * x_log_x + b * x + c
102
+ r2 = r2_score(y, y_fit)
103
+ equation = f"y = {a:.4f}*x*log(x) + {b:.4f}*x + {c:.4f}"
104
+ except ValueError:
105
+ st.error("Linearithmic fir failed. Ensure all X values are positive.")
106
+ st.stop()
107
+
108
+ elif fit_typeOne == "Quadratic":
109
+ degree = 2
110
+ coefficients = np.polyfit(x, y, degree)
111
+ y_fit = np.polyval(coefficients, x)
112
+ r2 = r2_score(y, y_fit)
113
+ equation = f"y = {coefficients[0]:.4f}*x² + {coefficients[1]:.4f}*x + {coefficients[2]:.4f}"
114
+
115
+ elif fit_typeOne == "Cubic":
116
+ degree = 3
117
+ coefficients = np.polyfit(x, y, degree)
118
+ y_fit = np.polyval(coefficients, x)
119
+ r2 = r2_score(y, y_fit)
120
+ equation = f"y = {coefficients[0]:.4f}*x³ + {coefficients[1]:.4f}*x² + {coefficients[2]:.4f}*x + {coefficients[3]:.4f}"
121
+
122
+ elif fit_typeOne == "Exponential":
123
+ try:
124
+ log_y = np.log(y)
125
+ coefficients = np.polyfit(x, log_y, 1)
126
+ a = np.exp(coefficients[1])
127
+ b = coefficients[0]
128
+ y_fit = a * np.exp(b * x)
129
+ r2 = r2_score(y, y_fit)
130
+ equation = f"y = {a:.4f}*exp({b:.4f}*x)"
131
+ except ValueError:
132
+ st.error("Exponential fit failed. Ensure all Y values are positive.")
133
+ st.stop()
134
+
135
+ if fit_typeTwo == "Logarithmic":
136
+ try:
137
+ log_u = np.log(u)
138
+ coefficients_Two = np.polyfit(log_u, v , 1)
139
+ v_fit = coefficients_Two[0] * log_u + coefficients_Two[1]
140
+ r2_Two = r2_score(v, v_fit)
141
+ equation_Two = f"y = {coefficients_Two[0]:.4f}*log(x) + {coefficients_Two[1]:.4f}"
142
+ except ValueError:
143
+ st.error("Logarithmic fit failed. Ensure all X values are positive.")
144
+ st.stop()
145
+
146
+ elif fit_typeTwo == "Linear":
147
+ degree_Two = 1
148
+ coefficients_Two = np.polyfit(u, v, degree_Two)
149
+ v_fit = np.polyval(coefficients_Two, u)
150
+ r2_Two = r2_score(v, v_fit)
151
+ equation_Two = f"y = {coefficients_Two[0]:.4f}*x + {coefficients_Two[1]:.4f}"
152
+
153
+ elif fit_typeTwo == "Linearithmic":
154
+ try:
155
+ u_log_u = u * np.log(u)
156
+ B = np.column_stack((u_log_u, u, np.ones_like(u)))
157
+ coefficients_Two, _, _, _ = np.linalg.lstsq(B, v, rcond=None)
158
+ d, e, f = coefficients_Two
159
+ v_fit = d * u_log_u + e * u + f
160
+ r2_Two = r2_score(v, v_fit)
161
+ equation_Two = f"y = {d:.4f}*x*log(x) + {e:.4f}*x + {f:.4f}"
162
+ except ValueError:
163
+ st.error("Linearithmic fir failed. Ensure all X values are positive.")
164
+ st.stop()
165
+
166
+ elif fit_typeTwo == "Quadratic":
167
+ degree_Two = 2
168
+ coefficients_Two = np.polyfit(u, v, degree_Two)
169
+ v_fit = np.polyval(coefficients_Two, u)
170
+ r2_Two = r2_score(v, v_fit)
171
+ equation_Two = f"y = {coefficients_Two[0]:.4f}*x² + {coefficients_Two[1]:.4f}*x + {coefficients_Two[2]:.4f}"
172
+
173
+ elif fit_typeTwo == "Cubic":
174
+ degree_Two = 3
175
+ coefficients_Two = np.polyfit(u, v, degree_Two)
176
+ v_fit = np.polyval(coefficients_Two, u)
177
+ r2_Two = r2_score(v, v_fit)
178
+ equation_Two = f"y = {coefficients_Two[0]:.4f}*x³ + {coefficients_Two[1]:.4f}*x² + {coefficients_Two[2]:.4f}*x + {coefficients_Two[3]:.4f}"
179
+
180
+ elif fit_typeTwo == "Exponential":
181
+ try:
182
+ log_v = np.log(v)
183
+ coefficients_Two = np.polyfit(u, log_v, 1)
184
+ d = np.exp(coefficients_Two[1])
185
+ e = coefficients_Two[0]
186
+ v_fit = d * np.exp(e * u)
187
+ r2_Two = r2_score(v, v_fit)
188
+ equation_Two = f"y = {d:.4f}*exp({e:.4f}*x)"
189
+ except ValueError:
190
+ st.error("Exponential fit failed. Ensure all Y values are positive.")
191
+ st.stop()
192
+
193
+ minimum = min(min(x), min(u))
194
+ maximum = max(max(x), max(u))
195
+ x_smooth = np.linspace(minimum, maximum, 500)
196
+ if fit_typeOne == "Logarithmic":
197
+ y_smooth = coefficients[0] * np.log(x_smooth) + coefficients[1]
198
+ elif fit_typeOne == "Linearithmic":
199
+ y_smooth = a * x_smooth * np.log(x_smooth) + b * x_smooth + c
200
+ elif fit_typeOne == "Exponential":
201
+ y_smooth = a * np.exp(b * x_smooth)
202
+ else:
203
+ y_smooth = np.polyval(coefficients, x_smooth)
204
+
205
+ u_smooth = np.linspace(minimum, maximum, 500)
206
+ if fit_typeTwo == "Logarithmic":
207
+ v_smooth = coefficients_Two[0] * np.log(u_smooth) + coefficients_Two[1]
208
+ elif fit_typeTwo == "Linearithmic":
209
+ v_smooth = d * u_smooth * np.log(u_smooth) + e * u_smooth + f
210
+ elif fit_typeTwo == "Exponential":
211
+ v_smooth = d * np.exp(e * u_smooth)
212
+ else:
213
+ v_smooth = np.polyval(coefficients_Two, u_smooth)
214
+
215
+ fig, ax = plt.subplots()
216
+ ax.scatter(x, y, color="red", label="Original Data One")
217
+ ax.scatter(u, v, color="blue", label="Original Data Two")
218
+ ax.plot(x_smooth, y_smooth, color="pink", label=f"{fit_typeOne} Fit (R²={r2:.4f})")
219
+ ax.plot(u_smooth, v_smooth, color="purple", label=f"{fit_typeTwo} Fit (R²={r2_Two:.4f})")
220
+ ax.set_xlabel(xlabel)
221
+ ax.set_ylabel(ylabel)
222
+ ax.legend()
223
+ ax.set_title("fit")
224
+
225
+ st.pyplot(fig)
226
+
227
+ st.write(f"**Fitted Equation One**: {equation}")
228
+ st.write(f"**R² Value One**: {r2:.6f}")
229
+
230
+ st.write(f"**Fitted Equation Two**: {equation_Two}")
231
+ st.write(f"**R² Value Two**: {r2_Two:.6f}")