Skip to content

Instantly share code, notes, and snippets.

@initcron
Last active May 26, 2025 09:37

Revisions

  1. initcron renamed this gist May 26, 2025. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion basic-ml.ipynb β†’ basic-ml.ipynb.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    ```
    # Step 0 / Cell 0
    import pandas as pd
    import numpy as np
    @@ -50,4 +51,6 @@ sns.heatmap(cm, annot=True, fmt='d', cmap='Blues',
    plt.xlabel("Predicted")
    plt.ylabel("Actual")
    plt.title("πŸ” Confusion Matrix")
    plt.show()
    plt.show()
    ```
  2. initcron created this gist May 26, 2025.
    53 changes: 53 additions & 0 deletions basic-ml.ipynb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    # 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()