Skip to content

Main content

AdaBoost Classifier Demonstrator Source Code

Browse the implementation files for AdaBoost Classifier Demonstrator.
tool.ts typescript
// Tool: AdaBoost Classifier Demonstrator

const NUMBER =
	/^[+-]?(?:(?:\d+(?:\.\d*)?)|(?:\.\d+))(?:[eE][+-]?\d+)?$/;
const MAX_ESTIMATORS = 100;

const parseNumberList = (input: unknown): number[] | null => {
	if (typeof input !== 'string' || input.trim() === '') return null;

	const tokens = input
		.split(/[,\n]/)
		.map((token) => token.trim())
		.filter((token) => token !== '');

	if (
		tokens.length === 0 ||
		tokens.some((token) => !NUMBER.test(token))
	) {
		return null;
	}

	const values = tokens.map(Number);

	return values.every(Number.isFinite) ? values : null;
};

const parsePositiveInteger = (input: unknown): number | null => {
	if (
		(typeof input !== 'string' && typeof input !== 'number') ||
		(typeof input === 'string' && input.trim() === '')
	) {
		return null;
	}

	const value = Number(input);

	return Number.isSafeInteger(value) &&
		value >= 1 &&
		value <= MAX_ESTIMATORS
		? value
		: null;
};

const parsePositiveNumber = (input: unknown): number | null => {
	if (
		(typeof input !== 'string' && typeof input !== 'number') ||
		(typeof input === 'string' && input.trim() === '')
	) {
		return null;
	}

	const value = Number(input);

	return Number.isFinite(value) && value > 0 ? value : null;
};

const classify = (
	value: number,
	threshold: number,
	polarity: number
): number => (value < threshold ? -polarity : polarity);

const round = (value: number): number =>
	Number(value.toPrecision(12));

export const trainAdaBoostClassifier = (
	featuresInput: unknown,
	labelsInput: unknown,
	estimatorCountInput: unknown,
	learningRateInput: unknown
): string => {
	const features = parseNumberList(featuresInput);
	const labels = parseNumberList(labelsInput);
	const estimatorCount = parsePositiveInteger(estimatorCountInput);
	const learningRate = parsePositiveNumber(learningRateInput);

	if (
		features === null ||
		labels === null ||
		estimatorCount === null ||
		learningRate === null ||
		features.length < 2 ||
		features.length !== labels.length ||
		labels.some((label) => label !== -1 && label !== 1)
	) {
		return '';
	}

	const uniqueFeatures = [...new Set(features)].sort(
		(first, second) => first - second
	);
	const edge = Math.max(
		1,
		Math.abs(uniqueFeatures[0]) * Number.EPSILON
	);
	const thresholds = [
		uniqueFeatures[0] - edge,
		...uniqueFeatures
			.slice(0, -1)
			.map(
				(value, index) =>
					value +
					(uniqueFeatures[index + 1] - value) / 2
			),
		uniqueFeatures.at(-1)! + edge
	].filter(Number.isFinite);
	const weights = new Array(features.length).fill(
		1 / features.length
	);
	const estimators: Array<{
		alpha: number;
		polarity: number;
		threshold: number;
		weightedError: number;
	}> = [];

	for (
		let estimatorIndex = 0;
		estimatorIndex < estimatorCount;
		estimatorIndex += 1
	) {
		let bestError = Infinity;
		let bestPolarity = 1;
		let bestThreshold = thresholds[0];

		for (const threshold of thresholds) {
			for (const polarity of [1, -1]) {
				let error = 0;

				for (let index = 0; index < features.length; index += 1) {
					if (
						classify(
							features[index],
							threshold,
							polarity
						) !== labels[index]
					) {
						error += weights[index];
					}
				}

				if (error < bestError) {
					bestError = error;
					bestPolarity = polarity;
					bestThreshold = threshold;
				}
			}
		}

		if (!Number.isFinite(bestError) || bestError >= 0.5) break;

		const boundedError = Math.max(
			Number.EPSILON,
			Math.min(1 - Number.EPSILON, bestError)
		);
		const alpha =
			learningRate *
			0.5 *
			Math.log((1 - boundedError) / boundedError);

		if (!Number.isFinite(alpha)) return '';

		let weightTotal = 0;

		for (let index = 0; index < features.length; index += 1) {
			const prediction = classify(
				features[index],
				bestThreshold,
				bestPolarity
			);
			weights[index] *= Math.exp(
				-alpha * labels[index] * prediction
			);
			weightTotal += weights[index];
		}

		if (!Number.isFinite(weightTotal) || weightTotal <= 0) return '';

		for (let index = 0; index < weights.length; index += 1) {
			weights[index] /= weightTotal;
		}

		estimators.push({
			alpha: round(alpha),
			polarity: bestPolarity,
			threshold: round(bestThreshold),
			weightedError: round(bestError)
		});

		if (bestError <= Number.EPSILON) break;
	}

	if (estimators.length === 0) return '';

	const scores = features.map((feature) =>
		estimators.reduce(
			(score, estimator) =>
				score +
				estimator.alpha *
					classify(
						feature,
						estimator.threshold,
						estimator.polarity
					),
			0
		)
	);
	const predictions = scores.map((score) => (score >= 0 ? 1 : -1));
	const correct = predictions.filter(
		(prediction, index) => prediction === labels[index]
	).length;

	return JSON.stringify(
		{
			estimators,
			predictions,
			scores: scores.map(round),
			trainingAccuracy: round(correct / labels.length)
		},
		null,
		2
	);
};

export const Tool: TypeTool = {
	name: 'AdaBoost Classifier Demonstrator',
	slug: 'machine-learning/adaboost-classifier',
	intro:
		'Train an AdaBoost ensemble of decision stumps on one-dimensional labeled data.',
	tool: trainAdaBoostClassifier,
	i: [
		{
			example: '1, 2, 3, 7, 8, 9',
			label: 'Feature Values',
			placeholder: 'Comma- or line-separated values',
			render: 'textarea',
			type: 'text',
			value: '1, 2, 3, 7, 8, 9'
		},
		{
			example: '-1, -1, -1, 1, 1, 1',
			help: 'Provide one -1 or 1 class label per feature value.',
			label: 'Class Labels',
			placeholder: 'Comma- or line-separated -1 and 1 labels',
			render: 'textarea',
			type: 'text',
			value: '-1, -1, -1, 1, 1, 1'
		},
		{
			example: '10',
			label: 'Maximum Estimators',
			placeholder: 'Maximum number of decision stumps',
			type: 'number',
			value: 10
		},
		{
			example: '1',
			label: 'Learning Rate',
			placeholder: 'Positive learning rate',
			type: 'number',
			value: 1
		}
	],
	o: [
		{
			example: '{ "estimators": [], "predictions": [] }',
			label: 'AdaBoost Model',
			placeholder: 'Trained model JSON will appear here...',
			render: 'textarea',
			type: 'text'
		}
	],
	cite: [
		{
			title: 'AdaBoost - Wikipedia',
			url: 'https://en.wikipedia.org/wiki/AdaBoost'
		}
	]
};

export default Tool;

Description

Browse the implementation files for AdaBoost Classifier Demonstrator.

References

  1. Tool.io on GitHub
  2. Tool.io TypeScript tools library

Similar or alternative tools