-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
88 lines (74 loc) · 3.36 KB
/
Copy pathmodel.py
File metadata and controls
88 lines (74 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns
def main():
print("Loading the Iris dataset...")
# 1. Load and understand a dataset
iris = load_iris()
X = pd.DataFrame(iris.data, columns=iris.feature_names)
y = pd.Series(iris.target, name="target")
print("\nDataset Features Head:")
print(X.head())
print(f"\nTotal samples: {len(X)}")
# 2. Split data into training and testing sets
print("\nSplitting the dataset into 80% training and 20% testing sets...")
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
print(f"Training samples: {len(X_train)}")
print(f"Testing samples: {len(X_test)}")
# 3. Apply classification algorithms and compare
print("\nTraining and comparing multiple algorithms...")
models = {
"Random Forest": RandomForestClassifier(n_estimators=100, random_state=42),
"Logistic Regression": LogisticRegression(max_iter=200, random_state=42),
"Support Vector Machine": SVC(random_state=42)
}
best_model_name = ""
best_accuracy = 0
best_y_pred = None
accuracies = {}
for name, model in models.items():
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
acc = accuracy_score(y_test, y_pred)
accuracies[name] = acc * 100
print(f" - {name} Accuracy: {acc * 100:.2f}%")
if acc > best_accuracy:
best_accuracy = acc
best_model_name = name
best_y_pred = y_pred
print(f"\nBest Model: {best_model_name} with {best_accuracy * 100:.2f}% accuracy")
# Plotting algorithm comparison
print("Generating Algorithm Comparison bar chart...")
plt.figure(figsize=(10, 6))
sns.barplot(x=list(accuracies.keys()), y=list(accuracies.values()), palette='viridis')
plt.title('Algorithm Comparison - Testing Accuracy')
plt.ylabel('Accuracy (%)')
plt.ylim(0, 110) # Set y-axis to a bit above 100 for visual headroom
for index, value in enumerate(accuracies.values()):
plt.text(index, value + 2, f'{value:.2f}%', ha='center')
comp_filename = 'algorithm_comparison.png'
plt.savefig(comp_filename)
print(f"Algorithm comparison chart saved as {comp_filename}")
# Evaluate the best model in detail
print(f"\nDetailed Classification Report for {best_model_name}:")
print(classification_report(y_test, best_y_pred, target_names=iris.target_names))
# Plotting confusion matrix for the best model
print(f"Generating Confusion Matrix plot for {best_model_name}...")
cm = confusion_matrix(y_test, best_y_pred)
plt.figure(figsize=(8, 6))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=iris.target_names, yticklabels=iris.target_names)
plt.title(f'Confusion Matrix - {best_model_name}')
plt.xlabel('Predicted')
plt.ylabel('Actual')
# Save the plot
cm_filename = 'confusion_matrix.png'
plt.savefig(cm_filename)
print(f"Confusion matrix saved as {cm_filename}")
if __name__ == "__main__":
main()