Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,6 +11,7 @@ from sklearn.feature_selection import f_classif
|
|
| 11 |
import matplotlib.pyplot as plt
|
| 12 |
import seaborn as sns
|
| 13 |
import io
|
|
|
|
| 14 |
|
| 15 |
# Constants for reproducibility and configuration
|
| 16 |
RANDOM_STATE = 42
|
|
@@ -76,6 +77,10 @@ def analyze_file(file, label_col, n_clusters):
|
|
| 76 |
|
| 77 |
results_text = ""
|
| 78 |
model_img = None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
# Prediction: Regression or Classification
|
| 81 |
try:
|
|
@@ -102,7 +107,7 @@ def analyze_file(file, label_col, n_clusters):
|
|
| 102 |
plt.savefig(buf, format="png", bbox_inches="tight")
|
| 103 |
plt.close()
|
| 104 |
buf.seek(0)
|
| 105 |
-
model_img = buf
|
| 106 |
else:
|
| 107 |
# Classification
|
| 108 |
if len(y.unique()) < 2:
|
|
@@ -124,9 +129,9 @@ def analyze_file(file, label_col, n_clusters):
|
|
| 124 |
plt.savefig(buf, format="png", bbox_inches="tight")
|
| 125 |
plt.close()
|
| 126 |
buf.seek(0)
|
| 127 |
-
model_img = buf
|
| 128 |
except Exception as e:
|
| 129 |
-
|
| 130 |
|
| 131 |
# Feature Importance
|
| 132 |
try:
|
|
@@ -140,9 +145,8 @@ def analyze_file(file, label_col, n_clusters):
|
|
| 140 |
plt.savefig(buf, format="png", bbox_inches="tight")
|
| 141 |
plt.close()
|
| 142 |
buf.seek(0)
|
| 143 |
-
fi_img = buf
|
| 144 |
except Exception as e:
|
| 145 |
-
fi_img = None
|
| 146 |
results_text += f"\nWarning: Could not compute feature importance: {e}"
|
| 147 |
|
| 148 |
# KMeans Clustering
|
|
@@ -162,9 +166,8 @@ def analyze_file(file, label_col, n_clusters):
|
|
| 162 |
plt.savefig(buf, format="png", bbox_inches="tight")
|
| 163 |
plt.close()
|
| 164 |
buf.seek(0)
|
| 165 |
-
kmeans_img = buf
|
| 166 |
except Exception as e:
|
| 167 |
-
kmeans_img = None
|
| 168 |
results_text += f"\nWarning: KMeans clustering failed: {e}"
|
| 169 |
|
| 170 |
# Agglomerative Clustering
|
|
@@ -181,9 +184,8 @@ def analyze_file(file, label_col, n_clusters):
|
|
| 181 |
plt.savefig(buf, format="png", bbox_inches="tight")
|
| 182 |
plt.close()
|
| 183 |
buf.seek(0)
|
| 184 |
-
agg_img = buf
|
| 185 |
except Exception as e:
|
| 186 |
-
agg_img = None
|
| 187 |
results_text += f"\nWarning: Agglomerative clustering failed: {e}"
|
| 188 |
|
| 189 |
# Differentiating Features
|
|
@@ -199,9 +201,8 @@ def analyze_file(file, label_col, n_clusters):
|
|
| 199 |
plt.savefig(buf, format="png", bbox_inches="tight")
|
| 200 |
plt.close()
|
| 201 |
buf.seek(0)
|
| 202 |
-
diff_img = buf
|
| 203 |
except Exception as e:
|
| 204 |
-
diff_img = None
|
| 205 |
results_text += f"\nWarning: Could not compute differentiating features: {e}"
|
| 206 |
|
| 207 |
return results_text, model_img, fi_img, kmeans_img, agg_img, diff_img
|
|
|
|
| 11 |
import matplotlib.pyplot as plt
|
| 12 |
import seaborn as sns
|
| 13 |
import io
|
| 14 |
+
from PIL import Image # For converting BytesIO to PIL Image
|
| 15 |
|
| 16 |
# Constants for reproducibility and configuration
|
| 17 |
RANDOM_STATE = 42
|
|
|
|
| 77 |
|
| 78 |
results_text = ""
|
| 79 |
model_img = None
|
| 80 |
+
fi_img = None
|
| 81 |
+
kmeans_img = None
|
| 82 |
+
agg_img = None
|
| 83 |
+
diff_img = None
|
| 84 |
|
| 85 |
# Prediction: Regression or Classification
|
| 86 |
try:
|
|
|
|
| 107 |
plt.savefig(buf, format="png", bbox_inches="tight")
|
| 108 |
plt.close()
|
| 109 |
buf.seek(0)
|
| 110 |
+
model_img = Image.open(buf) # Convert to PIL Image
|
| 111 |
else:
|
| 112 |
# Classification
|
| 113 |
if len(y.unique()) < 2:
|
|
|
|
| 129 |
plt.savefig(buf, format="png", bbox_inches="tight")
|
| 130 |
plt.close()
|
| 131 |
buf.seek(0)
|
| 132 |
+
model_img = Image.open(buf) # Convert to PIL Image
|
| 133 |
except Exception as e:
|
| 134 |
+
results_text += f"\nError during model training: {e}"
|
| 135 |
|
| 136 |
# Feature Importance
|
| 137 |
try:
|
|
|
|
| 145 |
plt.savefig(buf, format="png", bbox_inches="tight")
|
| 146 |
plt.close()
|
| 147 |
buf.seek(0)
|
| 148 |
+
fi_img = Image.open(buf) # Convert to PIL Image
|
| 149 |
except Exception as e:
|
|
|
|
| 150 |
results_text += f"\nWarning: Could not compute feature importance: {e}"
|
| 151 |
|
| 152 |
# KMeans Clustering
|
|
|
|
| 166 |
plt.savefig(buf, format="png", bbox_inches="tight")
|
| 167 |
plt.close()
|
| 168 |
buf.seek(0)
|
| 169 |
+
kmeans_img = Image.open(buf) # Convert to PIL Image
|
| 170 |
except Exception as e:
|
|
|
|
| 171 |
results_text += f"\nWarning: KMeans clustering failed: {e}"
|
| 172 |
|
| 173 |
# Agglomerative Clustering
|
|
|
|
| 184 |
plt.savefig(buf, format="png", bbox_inches="tight")
|
| 185 |
plt.close()
|
| 186 |
buf.seek(0)
|
| 187 |
+
agg_img = Image.open(buf) # Convert to PIL Image
|
| 188 |
except Exception as e:
|
|
|
|
| 189 |
results_text += f"\nWarning: Agglomerative clustering failed: {e}"
|
| 190 |
|
| 191 |
# Differentiating Features
|
|
|
|
| 201 |
plt.savefig(buf, format="png", bbox_inches="tight")
|
| 202 |
plt.close()
|
| 203 |
buf.seek(0)
|
| 204 |
+
diff_img = Image.open(buf) # Convert to PIL Image
|
| 205 |
except Exception as e:
|
|
|
|
| 206 |
results_text += f"\nWarning: Could not compute differentiating features: {e}"
|
| 207 |
|
| 208 |
return results_text, model_img, fi_img, kmeans_img, agg_img, diff_img
|