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

Create app_v1.py

Browse files
Files changed (1) hide show
  1. app_v1.py +75 -0
app_v1.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix, classification_report
2
+ import matplotlib.pyplot as plt
3
+ import seaborn as sns
4
+ import numpy as np
5
+ import pandas as pd
6
+
7
+ from sklearn.model_selection import train_test_split
8
+ from sklearn.preprocessing import StandardScaler, LabelEncoder
9
+ from sklearn.ensemble import RandomForestClassifier
10
+
11
+ # Step 1: Generate synthetic dataset
12
+ np.random.seed(42)
13
+ n_records = 10000
14
+
15
+ data = {
16
+ 'pe_ratio': np.random.uniform(5, 50, n_records),
17
+ 'de_ratio': np.random.uniform(0.1, 3.0, n_records),
18
+ 'roe': np.random.uniform(5, 40, n_records),
19
+ 'market_cap': np.random.uniform(500, 100000, n_records),
20
+ 'dividend_yield': np.random.uniform(0.5, 5.0, n_records),
21
+ 'stock_rating': np.random.choice(['Buy', 'Hold', 'Sell'], n_records, p=[0.4, 0.4, 0.2])
22
+ }
23
+
24
+ df = pd.DataFrame(data)
25
+
26
+ # Step 2: Prepare data
27
+ X = df.drop('stock_rating', axis=1)
28
+ y = df['stock_rating']
29
+
30
+ # Step 3: Encode target
31
+ le = LabelEncoder()
32
+ y_encoded = le.fit_transform(y)
33
+
34
+ # Step 4: Train/test split (stratified)
35
+ X_train, X_test, y_train, y_test = train_test_split(
36
+ X, y_encoded, test_size=0.2, random_state=42, stratify=y_encoded
37
+ )
38
+
39
+ # Step 5: Feature scaling
40
+ scaler = StandardScaler()
41
+ X_train_scaled = scaler.fit_transform(X_train)
42
+ X_test_scaled = scaler.transform(X_test)
43
+
44
+ # Step 6: Train model
45
+ model = RandomForestClassifier(random_state=42)
46
+ model.fit(X_train_scaled, y_train)
47
+
48
+ # Step 7️ Predict using your trained model
49
+ y_pred = model.predict(X_test_scaled)
50
+
51
+ # Step 8️ Inverse transform using correct label encoder
52
+ y_test_labels = le.inverse_transform(y_test)
53
+ y_pred_labels = le.inverse_transform(y_pred)
54
+
55
+ # Step 9️ Print basic metrics
56
+ print("✅ Accuracy:", accuracy_score(y_test_labels, y_pred_labels))
57
+ print("✅ Precision:", precision_score(y_test_labels, y_pred_labels, average='weighted'))
58
+ print("✅ Recall:", recall_score(y_test_labels, y_pred_labels, average='weighted'))
59
+ print("✅ F1 Score:", f1_score(y_test_labels, y_pred_labels, average='weighted'))
60
+
61
+ # Step 10️ Detailed breakdown per class
62
+ print("\n📊 Classification Report:")
63
+ print(classification_report(y_test_labels, y_pred_labels))
64
+
65
+ # Step 11️ Confusion Matrix
66
+ cm = confusion_matrix(y_test_labels, y_pred_labels, labels=le.classes_)
67
+
68
+ plt.figure(figsize=(6, 5))
69
+ sns.heatmap(cm, annot=True, fmt="d", cmap="Blues",
70
+ xticklabels=le.classes_,
71
+ yticklabels=le.classes_)
72
+ plt.xlabel("Predicted Label")
73
+ plt.ylabel("True Label")
74
+ plt.title("📉 Confusion Matrix")
75
+ plt.show()