Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -47,8 +47,53 @@ except Exception as e:
|
|
| 47 |
print(f"Firebase initialization failed: {e}")
|
| 48 |
firebase_initialized = False
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
# --- AQI breakpoints and calculation functions ---
|
| 54 |
aqi_breakpoints = {
|
|
|
|
| 47 |
print(f"Firebase initialization failed: {e}")
|
| 48 |
firebase_initialized = False
|
| 49 |
|
| 50 |
+
class MinMaxScaler:
|
| 51 |
+
def __init__(self, feature_axis=None, minmax_range=(0, 1)):
|
| 52 |
+
self.feature_axis = feature_axis
|
| 53 |
+
self.min_ = None
|
| 54 |
+
self.max_ = None
|
| 55 |
+
self.scale_ = None
|
| 56 |
+
self.minmax_range = minmax_range
|
| 57 |
+
|
| 58 |
+
def load_attributes(self, attributes):
|
| 59 |
+
self.min_ = np.array(attributes['min_']) if isinstance(attributes['min_'], list) else attributes['min_']
|
| 60 |
+
self.max_ = np.array(attributes['max_']) if isinstance(attributes['max_'], list) else attributes['max_']
|
| 61 |
+
self.scale_ = np.array(attributes['scale_']) if isinstance(attributes['scale_'], list) else attributes['scale_']
|
| 62 |
+
self.minmax_range = tuple(attributes['minmax_range']) if isinstance(attributes['minmax_range'], list) else attributes['minmax_range']
|
| 63 |
+
|
| 64 |
+
def fit(self, X):
|
| 65 |
+
if X.ndim == 3 and self.feature_axis is not None:
|
| 66 |
+
axis = tuple(i for i in range(X.ndim) if i != self.feature_axis)
|
| 67 |
+
self.min_ = np.min(X, axis=axis)
|
| 68 |
+
self.max_ = np.max(X, axis=axis)
|
| 69 |
+
elif X.ndim == 2:
|
| 70 |
+
self.min_ = np.min(X, axis=0)
|
| 71 |
+
self.max_ = np.max(X, axis=0)
|
| 72 |
+
elif X.ndim == 1:
|
| 73 |
+
self.min_ = np.min(X)
|
| 74 |
+
self.max_ = np.max(X)
|
| 75 |
+
else:
|
| 76 |
+
raise ValueError("Data must be 1D, 2D, or 3D.")
|
| 77 |
+
|
| 78 |
+
self.scale_ = self.max_ - self.min_
|
| 79 |
+
return self
|
| 80 |
+
|
| 81 |
+
def transform(self, X):
|
| 82 |
+
if self.min_ is None or self.max_ is None or self.scale_ is None:
|
| 83 |
+
# Handle the case where scaler wasn't fitted (though it should be if attributes loaded)
|
| 84 |
+
# Or raise an error
|
| 85 |
+
raise ValueError("Scaler attributes not loaded or scaler not fitted.")
|
| 86 |
+
X_scaled = (X - self.min_) / self.scale_
|
| 87 |
+
X_scaled = X_scaled * (self.minmax_range[1] - self.minmax_range[0]) + self.minmax_range[0]
|
| 88 |
+
return X_scaled
|
| 89 |
+
|
| 90 |
+
def inverse_transform(self, X_scaled):
|
| 91 |
+
if self.min_ is None or self.max_ is None or self.scale_ is None:
|
| 92 |
+
# Handle the case where scaler wasn't fitted
|
| 93 |
+
raise ValueError("Scaler attributes not loaded or scaler not fitted.")
|
| 94 |
+
X = (X_scaled - self.minmax_range[0]) / (self.minmax_range[1] - self.minmax_range[0])
|
| 95 |
+
X = X * self.scale_ + self.min_
|
| 96 |
+
return X
|
| 97 |
|
| 98 |
# --- AQI breakpoints and calculation functions ---
|
| 99 |
aqi_breakpoints = {
|