Skip to content
Laviyx Learn

Try

Machine Learning course menu

The Machine Learning Workflow: From Problem to Deployed Model

The end-to-end machine learning workflow: framing the problem, collecting and exploring data, preparing features, choosing a baseline, training and evaluating models, and deploying and monitoring them, with the scikit-learn fit/predict API.

Beginner4 min readUpdated
On this page

Building a useful model is much more than calling fit. Real projects follow a cycle, and skipping steps is the most common reason models disappoint.

The steps#

  1. Frame the problem. What decision will the model support? What exactly is predicted, and how will success be measured (fewer fraud losses, fewer missed deliveries)?
  2. Get the data. Collect examples with the right features (inputs) and labels (answers).
  3. Explore and clean. Understand the data with exploratory analysis; fix missing values, errors and duplicates.
  4. Split the data into training and test sets before going further, so the test set stays truly unseen.
  5. Prepare features. Encode categories, scale numbers, create useful new features.
  6. Start with a baseline. A trivially simple prediction gives a number every real model must beat.
  7. Train and compare models, using cross-validation on the training data.
  8. Evaluate once on the test set with the chosen metric.
  9. Deploy: save the model and serve its predictions.
  10. Monitor and retrain as new data arrives and the world changes.

The scikit-learn API#

Every scikit-learn model ("estimator") follows the same pattern, so once you know it you can use any algorithm:

MethodDoes
model = SomeModel(settings)create the model with its hyperparameters
model.fit(X, y)learn from training features X and labels y
model.predict(X_new)predict labels for new rows
model.score(X, y)a default metric (accuracy for classifiers, R² for regressors)
model.predict_proba(X_new)class probabilities (most classifiers)

X is a 2D table (rows × features), usually a NumPy array or DataFrame. y is a 1D array of labels, one per row.

Always start with a baseline#

A baseline is the score of the simplest possible prediction. Without one, "85% accuracy" means nothing: if 85% of emails are not spam, predicting "not spam" every time already scores 85%. scikit-learn's DummyClassifier and DummyRegressor give baselines:

Python
from sklearn.datasets import load_breast_cancer
from sklearn.dummy import DummyClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0, stratify=y)

baseline = DummyClassifier(strategy="most_frequent").fit(X_train, y_train)
model = make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000)).fit(X_train, y_train)

print("rows:", X.shape[0], "features:", X.shape[1])
print("baseline accuracy:", round(baseline.score(X_test, y_test), 3))
print("model accuracy:   ", round(model.score(X_test, y_test), 3))
Output
rows: 569 features: 30
baseline accuracy: 0.629
model accuracy:    0.958

The baseline simply predicts the most common class for every tumour. The real model must, and does, do far better. make_pipeline chains preparation and the model; you will learn it properly in Pipelines.

Data leakage#

Leakage happens when information about the answers sneaks into training, so the model looks great in testing but fails in real use. Common causes:

  • Preparing data (for example, scaling or filling missing values) using statistics computed on the whole dataset, including the test set.
  • Features that would not be available at prediction time ("refund issued" when predicting fraud at checkout).
  • Duplicate rows that end up in both training and test sets.

The defence: split first, fit every preparation step on the training data only (pipelines make this automatic), and ask of each feature, "would I know this when making the prediction?"

Choosing a metric#

The metric must match the business goal. Accuracy is misleading when classes are imbalanced; for fraud detection you may care most about catching fraud (recall) without too many false alarms (precision). For prices, an average error in rupees is easier to explain than R². The metrics lessons cover the options.

Common mistakes#

Key takeaway#

Next: Features, Labels and Train/Test Splits.

Finished this lesson?