npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

alexandrebrillant_perceptron

v1.0.0

Published

A Simple JavaScript Implementation of the Perceptron Algorithm

Readme

Perceptron

A Simple JavaScript Implementation of the Perceptron AI Algorithm

Usage

Add any sample using the addTrainingSample. A Sample is a set of values with a label. You can have only two labels. A label is a free string.

Calling train will update the inner weights of the perceptron. You can specify too a max training size. It return an error rate. 1 (100%) means it can't train the samples and 0 (0%) means this is perfect.

After training the perceptron, you can use the predict method with a set of values. It will return the label for this values.

In the following sample, we have two labels: green and red. Green represents a negative value, and red represents a positive one. We let the perceptron find the correct weights during training to predict them.

Perceptron result


import { Perceptron } from "../dist/perceptron.mjs";

const perceptron = new Perceptron();

const sample1 = {
    sample: [],
    label:"red"
}

for ( let sample = 0; sample < 100; sample++ )
    sample1.sample.push( Math.random() * 100 );

const sample2 = {
    sample : [],
    label:"green"
};

for ( let sample = 0; sample < 100; sample++ )
    sample2.sample.push( -Math.random() * 100 );

perceptron.addTrainingSample( sample1 );
perceptron.addTrainingSample( sample2 );

const errorRate = perceptron.train();

console.log( "Training good result rate " + ( 100 - errorRate ) + "%" );

console.log( perceptron.predict( [-10,-2] ) );  // green

console.log( perceptron.predict( [200,30] ) );  // red

Draw the result separator

In this example, we have two sets of points. We display the points and the separator line found by the perceptron.

<!DOCTYPE html>
<html>
    <head>

        <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>

        <script type="module">

            import { Perceptron } from "../dist/perceptron.mjs";

            const perceptron = new Perceptron();

            const trace1 = {
                x:[],
                y:[],
                mode:'markers',
                type:'scatter'
            };

            const trace2 = {
                x:[],
                y:[],
                mode:'markers',
                type:'scatter'
            };

            for ( let sample = 0; sample < 100; sample++ ) {

                let x1,y1;
                let x2,y2;

                const sample1 = {
                    sample: [ x1 = ( Math.random() * 100 ) + 20, y1 = ( Math.random() * 100 ) + 20 ],
                    label:"red"
                }

                const sample2 = {
                    sample: [ x2 = ( Math.random() * 100 ) - 50, y2 = ( Math.random() * 100 ) - 50 ],
                    label : "green"
                }

                trace1.x.push( x1 );
                trace1.y.push( y1 );

                trace2.x.push( x2 );
                trace2.y.push( y2 );

                perceptron.addTrainingSample( sample1 );
                perceptron.addTrainingSample( sample2 );
            }

            const errorRate = perceptron.train();

            const a1 = -50;
            const a2 = 120;
            const w1 = perceptron.weight( 0 );
            const w2 = perceptron.weight( 1 );

            const b = perceptron.biais();

            const b1 = (-w1 * a1 - b) / w2;
            const b2 = (-w1 * a2 - b) / w2;            

            const trace3 =
            {
                x: [a1, a2],
                y: [b1, b2],
                mode: 'lines',
                type: 'scatter',
                name: 'Separator',
                line: { color: 'blue', dash: 'dash' }
            };

            let data = [ trace1, trace2, trace3 ];

            Plotly.newPlot( 'plots', data );

        </script>
    </head>

    <body>

        <h1>Perceptron result</h1>

        <div id="plots">

        </div>
    </body>

</html>

(c) 2026 Alexandre Brillant