Sphere Partners

Learn with Sphere: Machine Learning Walkthrough using LabelBinarize and Keras

3 min read
Learn with Sphere: Machine Learning Walkthrough using LabelBinarize and Keras
In this article

My name is Anton Kazakov. I'm a Java developer with 7+ years of backend experience. In this article I walk through one of my pet projects using machine learning — the goal was to get familiar with model training, data preparation, and the prediction phase. Below is the process and result, hopefully useful to anyone starting out with Keras (opens in new tab).

The goal

Collect gyroscope data from a smartwatch during a workout, then use a smartphone to create a workout journal where you can search and find all the gym activities undertaken — pull-ups, sit-ups, abs — with details like reps.

Data preparation

The training set is a dummy CSV with gyroscope coordinates labelled with the type of activity. Three people performed three exercises (sit-ups, abs, pull-ups), so the dataset is small but realistic enough for a tutorial. The same file becomes both training and test data after the split.

To use the data with the Keras (opens in new tab) model I did some pre-processing to match Keras's expected input: map features to numbers, shuffle, then split into a test set and a model-fit set.

There are several types of machine-learning models — I consider two here: supervised (learning with a teacher, where we already have labelled samples) and unsupervised (no labels, and we want the model to discover the structure on its own). For this project the labels exist, so it's a supervised classification problem.

I used LabelBinarizer (opens in new tab) from scikit-learn (opens in new tab) to convert the activity labels into binary targets. LabelBinarizer accepts categorical data as input and returns a NumPy array suitable for the model's output layer.

Building the model

I used the Keras (opens in new tab) framework to build a custom model. Keras is a wrapper around TensorFlow (opens in new tab). The sequential API lets you stack layers one after another and is enough for most problems, but it doesn't allow models with shared layers or multiple inputs/outputs. The functional API gives more flexibility for that — for this project the sequential API is enough.

python
model = models.Sequential()
model.add(layers.Conv1D(32, 3, activation='relu',
input_shape=(None, train_data.shape[-1])))
model.add(layers.MaxPooling1D(2))
model.add(layers.Conv1D(64, 3, activation='relu'))
model.add(layers.GlobalMaxPooling1D())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(targets.shape[-1], activation='softmax'))

The architecture, layer by layer:

  • Conv1D — the first layer is a 1-D convolutional (opens in new tab) layer with a ReLU (opens in new tab) activation. A CNN is a specialised neural network for working with grid-structured data; the convolution slides a window of weights over the input.
  • MaxPooling1D(2) — downsamples by a factor of 2, halving the temporal axis.
  • Conv1D + GlobalMaxPooling1D — a second convolution feeds into global max pooling, which collapses the temporal axis to a fixed-size feature vector.
  • Dense — a fully-connected layer that wires every output from the previous layer to every neuron.
  • Dropout(0.5) — prevents overfitting (opens in new tab) by randomly zeroing half the activations during training.
  • Dense + softmax — the final layer outputs a softmax (opens in new tab) probability distribution over the workout activities.

Training

Compile defines the loss function, the optimiser, and the metrics — it has nothing to do with the weights, so you can compile a model as many times as you want without losing pretrained weights.

The loss function for this run is Mean Square Error (MSE) (opens in new tab) — a method of evaluating how well a specific algorithm fits the data:

python
loss = square(y_true - y_pred)

Accuracy is the metric for evaluating the classification model — informally, the fraction of predictions the model got right.

Evaluation and prediction

After fitting, the success rate is almost 1 — about 0.996. That's high, so the model is making good guesses; the predicted probabilities are slightly under 1, as expected. The final step is to predict on a few held-out test rows to sanity-check the probabilities and confirm the model generalises.

If you'd like our support with a machine-learning project — assessment, model architecture, productionisation, or MLOpsget in touch.

More to read

How to Choose an AI Software Development Company (And What to Watch Out For) — hero image
Consulting & Advisory,  Tech Executive Advisory,  Data & AI,  IT Strategy Consulting,  Software Development,  ChatGPT,  Trends

Not all AI software development companies are equal. Learn what separates firms that truly build with AI from those that just use the word. Includes real questions to ask and red flags to avoid.

Agentic RAG vs Traditional RAG vs ChatGPT — hero image
Data & AI,  ChatGPT,  Trends

Agentic RAG costs 3-10× more than traditional RAG and adds 2-5× latency. Here's when each approach wins in 2026 — with the numbers Progress and others leave out.

We'd love to hear from you!

Please provide your contact details, and our team will get back to you promptly.