Skip to content

Instantly share code, notes, and snippets.

@initcron
Last active May 26, 2025 09:37
Show Gist options
  • Save initcron/ab926bb37af1008a84fe7575fb57fe32 to your computer and use it in GitHub Desktop.
Save initcron/ab926bb37af1008a84fe7575fb57fe32 to your computer and use it in GitHub Desktop.
Sample Notebook Code
# Step 0 / Cell 0
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report, r2_score, mean_squared_error

# Step 1: Load the Iris dataset
print("πŸ“₯ Loading the Iris dataset...")
data = load_iris()

# Step 2: Explore the dataset structure
print("\nπŸ“ Feature names:", data.feature_names)
print("🎯 Target classes:", data.target_names)
print("πŸ“ Data shape:", data.data.shape)

# Step 3: Create a DataFrame for exploration
df = pd.DataFrame(data.data, columns=data.feature_names)
df['target'] = data.target
print("\nπŸ” First 5 rows of the dataset:")
print(df.head())

# Step 4: Define features (X) and target (y)
X = df[data.feature_names]
y = df['target']

# Step 5: Train a Logistic Regression model
print("\nβš™οΈ Training Logistic Regression model...")
model = LogisticRegression(max_iter=200)
model.fit(X, y)

# Step 6: Make predictions
y_pred = model.predict(X)

# Step 7: Evaluate the model
accuracy = accuracy_score(y, y_pred)
print(f"\nπŸ“Š Accuracy Score: {accuracy:.2f}")

print("\nπŸ“‹ Classification Report:")
print(classification_report(y, y_pred, target_names=data.target_names))

# Step 8 : Confusion Matrix Plot
cm = confusion_matrix(y, y_pred)
plt.figure(figsize=(6, 4))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues',
            xticklabels=data.target_names,
            yticklabels=data.target_names)
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.title("πŸ” Confusion Matrix")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment