Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

Pattern Recognition in Machine Learning: A Simple Classification Example

Classify points above or below a line with JavaScript, distinguish target labels from learned predictions, and see how the example relates to a perceptron.

Table of Contents

Pattern recognition in machine learning means identifying useful regularities in data. A simple example is deciding whether a point belongs above or below a straight line. This is a binary classification task because there are two possible labels.

A perceptron is a linear classifier that can learn this kind of boundary from labeled examples. Complex tasks such as facial recognition generally require much more than this simple model.

Define the classes precisely

Suppose the boundary is y = 1.2x + 50. Give a point label 1 when its y-coordinate is greater than the boundary value at its x-coordinate. Give it label 0 otherwise.

Pattern Recognition in Machine Learning Picture 1

At x = 100, the boundary is y = 170. The point (100, 200) is above it, while (100, 100) is below it. A point exactly on the line receives label 0 in this example. That boundary convention is a choice and must be consistent.

Pattern Recognition in Machine Learning Picture 2

Create labeled examples in JavaScript

Run this example in a browser’s developer console. It generates 500 points, labels them, displays the first ten, and counts the two classes. It uses ordinary JavaScript and does not require an undefined plotting library.

const count = 500;
const limit = 500;
const boundary = x => 1.2 * x + 50;

// The label is 1 above the line and 0 on or below it.
const label = (x, y) => y > boundary(x) ? 1 : 0;

const points = Array.from({ length: count }, () => {
  const x = Math.random() * limit;
  const y = Math.random() * limit;
  return { x, y, target: label(x, y) };
});

console.table(points.slice(0, 10));
const above = points.filter(p => p.target === 1).length;
console.log({ above, onOrBelow: count - above });

// Check the rule with examples on both sides and on the line.
console.assert(label(100, 200) === 1);
console.assert(label(100, 100) === 0);
console.assert(label(100, 170) === 0);

The counts vary because the points are random. They still add up to 500. If you draw the points, use one color for label 1 and another for label 0, and draw the same boundary used to produce the labels.

Labels are not predictions

The code above applies a rule we already know. It creates target labels; it does not train a neural network.

A perceptron instead calculates a weighted score, such as w1 × x + w2 × y + bias, and predicts a class based on that score. Training adjusts the weights and bias when its prediction differs from a target label. The scikit-learn documentation describes the perceptron as a linear classifier.

What to test next

Keep separate examples for evaluating a trained model. Compare its predictions with the known rule rather than judging accuracy from the appearance of a plot. TipsMake’s perceptron testing example discusses counting those errors.

A single straight decision boundary cannot solve every classification problem. If the classes require a curved or more complex boundary, the model or features need to change. The machine learning fundamentals course covers problem framing, data preparation, and evaluation beyond this introductory example.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.