Skip to content

Main content

Node Classification Demonstrator Source Code

Browse the implementation files for Node Classification Demonstrator.
tool.ts typescript
import { parseFiniteNumberList } from '../../parse/numeric-dataset/tool';
import { parseFiniteNumber } from '../../parse/number/tool';

type NeighborScores = { scores: number[]; weight: number };
export const demonstrateNodeClassification = (
	ownScoresInput: unknown,
	neighborsInput: unknown,
	selfWeightInput: unknown
): string => {
	const ownScores = parseFiniteNumberList(ownScoresInput);
	const selfWeight = parseFiniteNumber(selfWeightInput);
	if (
		!ownScores ||
		ownScores.length < 2 ||
		selfWeight === null ||
		selfWeight < 0 ||
		typeof neighborsInput !== 'string'
	)
		return '';
	let neighbors: unknown;
	try {
		neighbors = JSON.parse(neighborsInput);
	} catch {
		return '';
	}
	if (
		!Array.isArray(neighbors) ||
		neighbors.some(
			(neighbor) =>
				typeof neighbor !== 'object' ||
				neighbor === null ||
				!Array.isArray((neighbor as NeighborScores).scores) ||
				(neighbor as NeighborScores).scores.length !==
					ownScores.length ||
				(neighbor as NeighborScores).scores.some(
					(value) => !Number.isFinite(value)
				) ||
				!Number.isFinite((neighbor as NeighborScores).weight) ||
				(neighbor as NeighborScores).weight < 0
		)
	)
		return '';
	const typedNeighbors = neighbors as NeighborScores[];
	const combined = ownScores.map(
		(score, classIndex) =>
			selfWeight * score +
			typedNeighbors.reduce(
				(sum, neighbor) =>
					sum + neighbor.weight * neighbor.scores[classIndex],
				0
			)
	);
	const maximum = Math.max(...combined);
	const exponentials = combined.map((value) =>
		Math.exp(value - maximum)
	);
	const total = exponentials.reduce(
		(sum, value) => sum + value,
		0
	);
	const probabilities = exponentials.map(
		(value) => value / total
	);
	const predictedClass = probabilities.indexOf(
		Math.max(...probabilities)
	);
	return JSON.stringify(
		{
			classScores: combined.map((value) =>
				Number(value.toPrecision(10))
			),
			predictedClass,
			probabilities: probabilities.map((value) =>
				Number(value.toPrecision(10))
			)
		},
		null,
		2
	);
};

export const Tool: TypeTool = {
	name: 'Node Classification Demonstrator',
	slug: 'graph/node-classification',
	intro:
		'Combine a node’s class scores with weighted neighbor scores and predict a class.',
	tool: demonstrateNodeClassification,
	i: [
		{
			example: '1, 0, 0',
			label: 'Node Scores',
			placeholder: 'One score per class',
			render: 'textarea',
			type: 'text'
		},
		{
			example:
				'[{"scores":[0,1,0],"weight":2},{"scores":[0,0,1],"weight":1}]',
			label: 'Neighbor Scores',
			placeholder: 'Weighted neighbor scores as JSON',
			render: 'textarea',
			type: 'text'
		},
		{
			example: '1',
			label: 'Self Weight',
			placeholder: 'Nonnegative own-score weight',
			type: 'number'
		}
	],
	o: [
		{
			example: '{ "predictedClass": 1 }',
			label: 'Classification',
			placeholder: 'Prediction will appear here...',
			render: 'textarea',
			type: 'text'
		}
	]
};
export default Tool;

Description

Browse the implementation files for Node Classification Demonstrator.

References

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

Similar or alternative tools