๐Ÿ  VisualStudioTutor.com  ยท  Python Tutorial Home  ยท  Python Lesson 31 of 40
Lesson 31 of 40 AI / ML Expert โฑ 35 min

Machine Learning with scikit-learn

Build end-to-end ML pipelines โ€” preprocessing, feature engineering, classification, regression, model evaluation, and joblib serialization.

Part 1: What You Will Learn

  • Separate features (X) from the target (y).
  • Split data into training and testing sets.
  • Build a scikit-learn Pipeline for preprocessing and classification.
  • Evaluate predictions with accuracy and a classification report.

Part 2: Key Concepts

Machine learning finds patterns in examples instead of relying on manually written rules. A good workflow keeps the test data separate from training so that evaluation reflects how the model performs on unseen examples.

  • Features: input values used by the model.
  • Target: the value or class the model learns to predict.
  • Training set: examples used to fit model parameters.
  • Test set: examples reserved for evaluation.
  • Pipeline: preprocessing and modelling steps executed in a fixed order.

Part 3: Topic-Specific Code Example

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report

iris = load_iris()
X = iris.data
y = iris.target

X_train, X_test, y_train, y_test = train_test_split(
    X,
    y,
    test_size=0.25,
    random_state=42,
    stratify=y,
)

model = Pipeline([
    ("scaler", StandardScaler()),
    ("classifier", LogisticRegression(max_iter=500)),
])

model.fit(X_train, y_train)

predictions = model.predict(X_test)

print("Accuracy:", round(accuracy_score(y_test, predictions), 3))
print(classification_report(
    y_test,
    predictions,
    target_names=iris.target_names,
))

new_flower = [[5.9, 3.0, 5.1, 1.8]]
predicted_class = model.predict(new_flower)[0]
print("Prediction:", iris.target_names[predicted_class])
pip install scikit-learn

Part 4: How the Example Works

The Iris dataset supplies four measurements for each flower. train_test_split() reserves 25% of the records for testing. The pipeline standardises the measurements and then trains logistic regression. Calling fit() learns from the training data; predict() applies the learned model to unseen data.

Part 5: Hands-On Practice

Mini project โ€” Student Pass Predictor. Create a small dataset containing study hours, attendance percentage, previous mark, and a pass/fail target. Train a classifier, evaluate it on test data, then predict whether a new student is likely to pass. Add joblib.dump() to save the trained model.

Part 6: Next Steps

Experiment with different features and models, then continue to Lesson 32 for deep learning with PyTorch.

๐Ÿ“˜ Want the complete guide with projects? Get the book โ†’