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

Create app_XGBOOST_v2.py

Browse files
Files changed (1) hide show
  1. app_XGBOOST_v2.py +123 -0
app_XGBOOST_v2.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ import xgboost as xgb
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
+ # Step 1: Generate synthetic dataset
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
+ # Step 2: Prepare data
31
+ X = df.drop('stock_rating', axis=1)
32
+ y = df['stock_rating']
33
+
34
+ # Step 3: Encode target
35
+ le = LabelEncoder()
36
+ y_encoded = le.fit_transform(y)
37
+
38
+ # Step 4: 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
+ # Step 5: Feature scaling
44
+ scaler = StandardScaler()
45
+ X_train_scaled = scaler.fit_transform(X_train)
46
+ X_test_scaled = scaler.transform(X_test)
47
+
48
+ # Step 6: Train model using XGBoost
49
+ model = xgb.XGBClassifier(random_state=42, use_label_encoder=False, eval_metric='mlogloss')
50
+ model.fit(X_train_scaled, y_train)
51
+
52
+ # Step 7: Predict
53
+ y_pred = model.predict(X_test_scaled)
54
+
55
+ # Step 8: Decode labels
56
+ y_test_labels = le.inverse_transform(y_test)
57
+ y_pred_labels = le.inverse_transform(y_pred)
58
+
59
+ # Step 9: Metrics
60
+ acc = accuracy_score(y_test_labels, y_pred_labels)
61
+ prec = precision_score(y_test_labels, y_pred_labels, average='weighted', zero_division=0)
62
+ rec = recall_score(y_test_labels, y_pred_labels, average='weighted', zero_division=0)
63
+ f1 = f1_score(y_test_labels, y_pred_labels, average='weighted', zero_division=0)
64
+
65
+ # Step 10: Create Classification Report as DataFrame (with zero_division fix)
66
+ report_dict = classification_report(y_test_labels, y_pred_labels, output_dict=True, zero_division=0)
67
+ report_df = pd.DataFrame(report_dict).transpose().round(2)
68
+
69
+ # Step 11: Plot classification report as table with grid
70
+ fig, ax = plt.subplots(figsize=(8, 4))
71
+ ax.axis('off')
72
+ tbl = ax.table(
73
+ cellText=report_df.values,
74
+ colLabels=report_df.columns,
75
+ rowLabels=report_df.index,
76
+ cellLoc='center',
77
+ loc='center'
78
+ )
79
+ tbl.auto_set_font_size(False)
80
+ tbl.set_fontsize(10)
81
+ tbl.scale(1.2, 1.2)
82
+ for key, cell in tbl.get_celld().items():
83
+ cell.set_linewidth(0.8)
84
+ cr_path = "classification_report.png"
85
+ plt.savefig(cr_path, bbox_inches='tight')
86
+ plt.close()
87
+
88
+ # Step 12: Confusion matrix
89
+ cm = confusion_matrix(y_test_labels, y_pred_labels, labels=le.classes_)
90
+ plt.figure(figsize=(6, 5))
91
+ sns.heatmap(cm, annot=True, fmt="d", cmap="Blues",
92
+ xticklabels=le.classes_, yticklabels=le.classes_)
93
+ plt.xlabel("Predicted")
94
+ plt.ylabel("Actual")
95
+ plt.title("Confusion Matrix")
96
+ cm_path = "confusion_matrix.png"
97
+ plt.savefig(cm_path, bbox_inches='tight')
98
+ plt.close()
99
+
100
+ # Step 13: Return outputs
101
+ output = f"""
102
+ ### ✅ Evaluation Metrics:
103
+ - **Accuracy:** {acc:.2f}
104
+ - **Precision:** {prec:.2f}
105
+ - **Recall:** {rec:.2f}
106
+ - **F1 Score:** {f1:.2f}
107
+ """
108
+ return output, cr_path, cm_path
109
+
110
+ # Gradio Interface
111
+ with gr.Blocks() as demo:
112
+ gr.Markdown("## 🧠 Stock Rating Prediction Model Evaluation")
113
+ gr.Markdown("Click the button below to train the model on synthetic stock data and evaluate its performance.")
114
+
115
+ eval_btn = gr.Button("Run Model Evaluation")
116
+ output_md = gr.Markdown()
117
+ report_img = gr.Image(type="filepath", label="📊 Classification Report")
118
+ cm_img = gr.Image(type="filepath", label="📉 Confusion Matrix")
119
+
120
+ eval_btn.click(fn=train_and_evaluate_model,
121
+ outputs=[output_md, report_img, cm_img])
122
+
123
+ demo.launch()