Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,16 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import numpy as np
|
| 3 |
import pandas as pd
|
| 4 |
-
import
|
| 5 |
-
import seaborn as sns
|
| 6 |
-
|
| 7 |
from sklearn.model_selection import train_test_split
|
| 8 |
from sklearn.preprocessing import StandardScaler, LabelEncoder
|
| 9 |
from sklearn.ensemble import RandomForestClassifier
|
| 10 |
-
from sklearn.metrics import
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
| 15 |
-
# Step 1: Generate synthetic dataset
|
| 16 |
def train_and_evaluate_model():
|
|
|
|
| 17 |
np.random.seed(42)
|
| 18 |
n_records = 10000
|
| 19 |
data = {
|
|
@@ -27,45 +24,43 @@ def train_and_evaluate_model():
|
|
| 27 |
|
| 28 |
df = pd.DataFrame(data)
|
| 29 |
|
| 30 |
-
# Prepare data
|
| 31 |
X = df.drop('stock_rating', axis=1)
|
| 32 |
y = df['stock_rating']
|
| 33 |
|
| 34 |
-
# Encode target
|
| 35 |
le = LabelEncoder()
|
| 36 |
y_encoded = le.fit_transform(y)
|
| 37 |
|
| 38 |
-
# Train/test split
|
| 39 |
X_train, X_test, y_train, y_test = train_test_split(
|
| 40 |
X, y_encoded, test_size=0.2, random_state=42, stratify=y_encoded
|
| 41 |
)
|
| 42 |
|
| 43 |
-
# Feature scaling
|
| 44 |
scaler = StandardScaler()
|
| 45 |
X_train_scaled = scaler.fit_transform(X_train)
|
| 46 |
X_test_scaled = scaler.transform(X_test)
|
| 47 |
|
| 48 |
-
# Train model
|
| 49 |
model = RandomForestClassifier(random_state=42)
|
| 50 |
model.fit(X_train_scaled, y_train)
|
| 51 |
|
| 52 |
-
# Predict
|
| 53 |
y_pred = model.predict(X_test_scaled)
|
| 54 |
|
| 55 |
-
# Decode labels
|
| 56 |
y_test_labels = le.inverse_transform(y_test)
|
| 57 |
y_pred_labels = le.inverse_transform(y_pred)
|
| 58 |
|
| 59 |
-
# Metrics
|
| 60 |
acc = accuracy_score(y_test_labels, y_pred_labels)
|
| 61 |
prec = precision_score(y_test_labels, y_pred_labels, average='weighted')
|
| 62 |
rec = recall_score(y_test_labels, y_pred_labels, average='weighted')
|
| 63 |
f1 = f1_score(y_test_labels, y_pred_labels, average='weighted')
|
| 64 |
-
|
| 65 |
-
# Classification report
|
| 66 |
clf_report = classification_report(y_test_labels, y_pred_labels)
|
| 67 |
|
| 68 |
-
# Confusion matrix
|
| 69 |
cm = confusion_matrix(y_test_labels, y_pred_labels, labels=le.classes_)
|
| 70 |
plt.figure(figsize=(6, 5))
|
| 71 |
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues",
|
|
@@ -74,12 +69,13 @@ def train_and_evaluate_model():
|
|
| 74 |
plt.ylabel("Actual")
|
| 75 |
plt.title("Confusion Matrix")
|
| 76 |
|
| 77 |
-
# Save plot
|
| 78 |
-
|
| 79 |
-
plt.savefig(
|
| 80 |
plt.close()
|
| 81 |
|
| 82 |
-
|
|
|
|
| 83 |
### β
Evaluation Metrics:
|
| 84 |
- **Accuracy:** {acc:.2f}
|
| 85 |
- **Precision:** {prec:.2f}
|
|
@@ -89,18 +85,19 @@ def train_and_evaluate_model():
|
|
| 89 |
---
|
| 90 |
|
| 91 |
### π Classification Report:
|
| 92 |
-
|
| 93 |
"""
|
| 94 |
-
return
|
| 95 |
|
| 96 |
# Gradio Interface
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
)
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
+
import numpy as np
|
|
|
|
|
|
|
| 4 |
from sklearn.model_selection import train_test_split
|
| 5 |
from sklearn.preprocessing import StandardScaler, LabelEncoder
|
| 6 |
from sklearn.ensemble import RandomForestClassifier
|
| 7 |
+
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix, classification_report
|
| 8 |
+
import seaborn as sns
|
| 9 |
+
import matplotlib.pyplot as plt
|
| 10 |
+
|
| 11 |
|
|
|
|
| 12 |
def train_and_evaluate_model():
|
| 13 |
+
# Step 1: Generate synthetic dataset
|
| 14 |
np.random.seed(42)
|
| 15 |
n_records = 10000
|
| 16 |
data = {
|
|
|
|
| 24 |
|
| 25 |
df = pd.DataFrame(data)
|
| 26 |
|
| 27 |
+
# Step 2: Prepare data
|
| 28 |
X = df.drop('stock_rating', axis=1)
|
| 29 |
y = df['stock_rating']
|
| 30 |
|
| 31 |
+
# Step 3: Encode target
|
| 32 |
le = LabelEncoder()
|
| 33 |
y_encoded = le.fit_transform(y)
|
| 34 |
|
| 35 |
+
# Step 4: Train/test split
|
| 36 |
X_train, X_test, y_train, y_test = train_test_split(
|
| 37 |
X, y_encoded, test_size=0.2, random_state=42, stratify=y_encoded
|
| 38 |
)
|
| 39 |
|
| 40 |
+
# Step 5: Feature scaling
|
| 41 |
scaler = StandardScaler()
|
| 42 |
X_train_scaled = scaler.fit_transform(X_train)
|
| 43 |
X_test_scaled = scaler.transform(X_test)
|
| 44 |
|
| 45 |
+
# Step 6: Train model
|
| 46 |
model = RandomForestClassifier(random_state=42)
|
| 47 |
model.fit(X_train_scaled, y_train)
|
| 48 |
|
| 49 |
+
# Step 7: Predict
|
| 50 |
y_pred = model.predict(X_test_scaled)
|
| 51 |
|
| 52 |
+
# Step 8: Decode labels
|
| 53 |
y_test_labels = le.inverse_transform(y_test)
|
| 54 |
y_pred_labels = le.inverse_transform(y_pred)
|
| 55 |
|
| 56 |
+
# Step 9: Metrics
|
| 57 |
acc = accuracy_score(y_test_labels, y_pred_labels)
|
| 58 |
prec = precision_score(y_test_labels, y_pred_labels, average='weighted')
|
| 59 |
rec = recall_score(y_test_labels, y_pred_labels, average='weighted')
|
| 60 |
f1 = f1_score(y_test_labels, y_pred_labels, average='weighted')
|
|
|
|
|
|
|
| 61 |
clf_report = classification_report(y_test_labels, y_pred_labels)
|
| 62 |
|
| 63 |
+
# Step 10: Confusion matrix
|
| 64 |
cm = confusion_matrix(y_test_labels, y_pred_labels, labels=le.classes_)
|
| 65 |
plt.figure(figsize=(6, 5))
|
| 66 |
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues",
|
|
|
|
| 69 |
plt.ylabel("Actual")
|
| 70 |
plt.title("Confusion Matrix")
|
| 71 |
|
| 72 |
+
# Save the confusion matrix plot
|
| 73 |
+
cm_path = "confusion_matrix.png"
|
| 74 |
+
plt.savefig(cm_path)
|
| 75 |
plt.close()
|
| 76 |
|
| 77 |
+
# Combine output (correct Markdown formatting)
|
| 78 |
+
output = f"""
|
| 79 |
### β
Evaluation Metrics:
|
| 80 |
- **Accuracy:** {acc:.2f}
|
| 81 |
- **Precision:** {prec:.2f}
|
|
|
|
| 85 |
---
|
| 86 |
|
| 87 |
### π Classification Report:
|
|
|
|
| 88 |
"""
|
| 89 |
+
return output, cm_path
|
| 90 |
|
| 91 |
# Gradio Interface
|
| 92 |
+
with gr.Blocks() as demo:
|
| 93 |
+
gr.Markdown("## π§ Stock Rating Prediction Model Evaluation")
|
| 94 |
+
gr.Markdown("Click the button below to train the model on synthetic stock data and evaluate its performance.")
|
| 95 |
+
|
| 96 |
+
eval_btn = gr.Button("Run Model Evaluation")
|
| 97 |
+
output_md = gr.Markdown()
|
| 98 |
+
output_img = gr.Image(type="filepath")
|
| 99 |
+
|
| 100 |
+
eval_btn.click(fn=train_and_evaluate_model, outputs=[output_md, output_img])
|
| 101 |
+
|
| 102 |
+
# Launch app
|
| 103 |
+
demo.launch()
|