Yatheshr commited on
Commit
a3a4cf2
·
verified ·
1 Parent(s): b2b32ab

Create app_with_model_benchmark_img.py

Browse files
Files changed (1) hide show
  1. app_with_model_benchmark_img.py +136 -0
app_with_model_benchmark_img.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 (
8
+ accuracy_score, precision_score, recall_score, f1_score,
9
+ confusion_matrix, classification_report
10
+ )
11
+ import seaborn as sns
12
+ import matplotlib.pyplot as plt
13
+
14
+
15
+ def train_and_evaluate_model():
16
+ # Generate synthetic data
17
+ np.random.seed(42)
18
+ n_records = 10000
19
+ data = {
20
+ 'pe_ratio': np.random.uniform(5, 50, n_records),
21
+ 'de_ratio': np.random.uniform(0.1, 3.0, n_records),
22
+ 'roe': np.random.uniform(5, 40, n_records),
23
+ 'market_cap': np.random.uniform(500, 100000, n_records),
24
+ 'dividend_yield': np.random.uniform(0.5, 5.0, n_records),
25
+ 'stock_rating': np.random.choice(['Buy', 'Hold', 'Sell'], n_records, p=[0.4, 0.4, 0.2])
26
+ }
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
+ le = LabelEncoder()
35
+ y_encoded = le.fit_transform(y)
36
+
37
+ # Split
38
+ X_train, X_test, y_train, y_test = train_test_split(
39
+ X, y_encoded, test_size=0.2, random_state=42, stratify=y_encoded
40
+ )
41
+
42
+ # Scale
43
+ scaler = StandardScaler()
44
+ X_train_scaled = scaler.fit_transform(X_train)
45
+ X_test_scaled = scaler.transform(X_test)
46
+
47
+ # Train
48
+ model = RandomForestClassifier(random_state=42)
49
+ model.fit(X_train_scaled, y_train)
50
+
51
+ # Predict
52
+ y_pred = model.predict(X_test_scaled)
53
+ y_test_labels = le.inverse_transform(y_test)
54
+ y_pred_labels = le.inverse_transform(y_pred)
55
+
56
+ # Metrics
57
+ acc = accuracy_score(y_test_labels, y_pred_labels)
58
+ prec = precision_score(y_test_labels, y_pred_labels, average='weighted', zero_division=0)
59
+ rec = recall_score(y_test_labels, y_pred_labels, average='weighted', zero_division=0)
60
+ f1 = f1_score(y_test_labels, y_pred_labels, average='weighted', zero_division=0)
61
+
62
+ # Classification report image
63
+ report_dict = classification_report(y_test_labels, y_pred_labels, output_dict=True, zero_division=0)
64
+ report_df = pd.DataFrame(report_dict).transpose().round(2)
65
+
66
+ fig, ax = plt.subplots(figsize=(8, 4))
67
+ ax.axis('off')
68
+ tbl = ax.table(
69
+ cellText=report_df.values,
70
+ colLabels=report_df.columns,
71
+ rowLabels=report_df.index,
72
+ cellLoc='center',
73
+ loc='center'
74
+ )
75
+ tbl.auto_set_font_size(False)
76
+ tbl.set_fontsize(10)
77
+ tbl.scale(1.2, 1.2)
78
+ for key, cell in tbl.get_celld().items():
79
+ cell.set_linewidth(0.8)
80
+ cr_path = "classification_report.png"
81
+ plt.savefig(cr_path, bbox_inches='tight')
82
+ plt.close()
83
+
84
+ # Confusion matrix image
85
+ cm = confusion_matrix(y_test_labels, y_pred_labels, labels=le.classes_)
86
+ plt.figure(figsize=(6, 5))
87
+ sns.heatmap(cm, annot=True, fmt="d", cmap="Blues",
88
+ xticklabels=le.classes_, yticklabels=le.classes_)
89
+ plt.xlabel("Predicted")
90
+ plt.ylabel("Actual")
91
+ plt.title("Confusion Matrix")
92
+ cm_path = "confusion_matrix.png"
93
+ plt.savefig(cm_path, bbox_inches='tight')
94
+ plt.close()
95
+
96
+ # Output metrics as Markdown
97
+ output = f"""
98
+ ### ✅ Evaluation Metrics:
99
+ - **Accuracy:** {acc:.2f}
100
+ - **Precision:** {prec:.2f}
101
+ - **Recall:** {rec:.2f}
102
+ - **F1 Score:** {f1:.2f}
103
+ """
104
+
105
+ return output, cr_path, cm_path
106
+
107
+
108
+ # Gradio Interface
109
+ with gr.Blocks() as demo:
110
+ gr.Markdown("## 🧠 Stock Rating Prediction Model Evaluation")
111
+ gr.Markdown("Click the button below to train the model on synthetic stock data and evaluate its performance.")
112
+
113
+ eval_btn = gr.Button("Run Model Evaluation")
114
+
115
+ with gr.Row():
116
+ with gr.Column():
117
+ output_md = gr.Markdown() # Visible Markdown for metrics
118
+
119
+ with gr.Column():
120
+ gr.Markdown("### 📌 Benchmark Guide\n\n")
121
+ benchmark_img = gr.Image(
122
+ value="Model_Benchmark.png",
123
+ type="filepath",
124
+ show_label=False # No label shown
125
+ )
126
+
127
+ with gr.Row():
128
+ report_img = gr.Image(type="filepath", label="📊 Classification Report")
129
+ cm_img = gr.Image(type="filepath", label="📉 Confusion Matrix")
130
+
131
+ eval_btn.click(
132
+ fn=train_and_evaluate_model,
133
+ outputs=[output_md, report_img, cm_img]
134
+ )
135
+
136
+ demo.launch()