Neural Network
Machine Learning
Data Classification
Neuralnet Package
Predictive Modeling

predicting class for new data using neuralnet

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Predicting classes for new samples with a neural network is straightforward once you separate three steps: preprocessing, model training, and postprocessing of outputs. Most mistakes come from applying different transforms at prediction time than at training time. A robust workflow keeps both paths identical and converts probabilities to class labels in one place.

Build a Repeatable Training Pipeline

The example below uses R with the neuralnet package, which returns raw network outputs that you map back to class labels. The key is to normalize input features once and reuse the same scaling values for all future predictions.

r
1set.seed(42)
2library(neuralnet)
3
4# Binary classification sample data
5train_df <- data.frame(
6  x1 = c(1.0, 1.5, 2.0, 3.0, 3.5, 4.0),
7  x2 = c(1.0, 1.2, 1.8, 2.7, 3.0, 3.5),
8  y  = c(0, 0, 0, 1, 1, 1)
9)
10
11# Min max scaling parameters from training data only
12mins <- sapply(train_df[, c("x1", "x2")], min)
13maxs <- sapply(train_df[, c("x1", "x2")], max)
14
15scale_minmax <- function(df, mins, maxs) {
16  as.data.frame(mapply(function(col, lo, hi) (col - lo) / (hi - lo),
17                       df, mins, maxs, SIMPLIFY = FALSE))
18}
19
20x_train <- scale_minmax(train_df[, c("x1", "x2")], mins, maxs)
21train_scaled <- cbind(x_train, y = train_df$y)
22
23nn <- neuralnet(y ~ x1 + x2, data = train_scaled, hidden = c(4), linear.output = FALSE)

This creates a binary classifier that outputs values close to zero or one.

Predict Classes for New Data Correctly

At inference time, scale new inputs with the same mins and maxs from training. Do not recompute scaling from the new batch, because that shifts the feature space and changes decision boundaries.

r
1new_df <- data.frame(
2  x1 = c(1.4, 3.2, 3.9),
3  x2 = c(1.1, 2.8, 3.2)
4)
5
6new_scaled <- scale_minmax(new_df, mins, maxs)
7raw_pred <- compute(nn, new_scaled)$net.result
8
9# Convert probability style output to hard class labels
10pred_class <- ifelse(raw_pred >= 0.5, 1, 0)
11
12result <- cbind(new_df, probability = as.numeric(raw_pred), class = as.integer(pred_class))
13print(result)

For multiclass models, your network usually has one output neuron per class. In that case, choose the class index with the highest score.

r
1# Example helper for multiclass score matrix
2scores <- matrix(c(
3  0.1, 0.8, 0.1,
4  0.6, 0.2, 0.2,
5  0.2, 0.3, 0.5
6), nrow = 3, byrow = TRUE)
7
8pred_idx <- max.col(scores, ties.method = "first")
9class_names <- c("A", "B", "C")
10pred_labels <- class_names[pred_idx]
11print(pred_labels)

Validate Before Deployment

Do not rely only on training accuracy. Create a validation split and track confusion matrix metrics so you understand class-specific behavior. For imbalanced data, precision and recall are usually more informative than global accuracy.

A simple deployment checklist:

  • Save model object and scaling parameters together.
  • Validate incoming data schema before scoring.
  • Log prediction probabilities for monitoring drift.
  • Re-evaluate threshold values for business tradeoffs.

These operational details often matter more than architecture tweaks when your goal is stable production predictions.

Production Readiness Checklist

A model that works in a notebook may still fail in production if input quality changes. Validate numeric ranges before scoring, reject malformed rows explicitly, and log both prediction labels and confidence values. Keep a sample of scored requests for periodic review with domain experts. This gives you a feedback loop for recalibration and retraining, which is critical when real world data slowly drifts away from the training distribution.

Common Pitfalls

  • Recomputing normalization values from new data instead of reusing training statistics.
  • Using a threshold of 0.5 blindly without testing precision and recall impact.
  • Treating multiclass scores as binary outputs and assigning invalid labels.
  • Training with one feature order and predicting with another feature order.
  • Saving only model weights but not the preprocessing configuration needed for inference.

Summary

  • Keep preprocessing and prediction steps tightly coupled to the training setup.
  • Reuse the same scaling parameters from training for all new samples.
  • Convert network outputs to labels using thresholding for binary or argmax for multiclass.
  • Evaluate performance on validation data, not only training data.
  • Package model and preprocessing artifacts together for reliable deployment.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.