-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict_iris.py
More file actions
54 lines (43 loc) · 1.45 KB
/
predict_iris.py
File metadata and controls
54 lines (43 loc) · 1.45 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
# iris_project.py
# Step 1: Import libraries
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix, ConfusionMatrixDisplay
# Step 2: Load the dataset
df = sns.load_dataset("iris")
print("\n📊 First few rows of the dataset:")
print(df.head())
# Step 3: Visualize the dataset
sns.pairplot(df, hue="species")
plt.suptitle("Iris Feature Pairs", y=1.02)
plt.show()
sns.heatmap(df.corr(numeric_only=True), annot=True, cmap="Blues")
plt.title("Feature Correlation")
plt.show()
# Step 4: Prepare data for training
X = df.drop("species", axis=1)
y = pd.factorize(df["species"])[0] # Encode species as integers
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Step 5: Train a model
model = LogisticRegression()
model.fit(X_train_scaled, y_train)
# Step 6: Evaluate the model
y_pred = model.predict(X_test_scaled)
accuracy = accuracy_score(y_test, y_pred)
print(f"\n✅ Model Accuracy: {accuracy:.2%}")
# Show confusion matrix
cm = confusion_matrix(y_test, y_pred)
disp = ConfusionMatrixDisplay(confusion_matrix=cm)
disp.plot()
plt.title("Confusion Matrix")
plt.show()