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 🙏

© 2024 – Pkg Stats / Ryan Hefner

averaged-perceptron

v3.0.0

Published

A linear classifier with the averaged perceptron algorithm

Downloads

24

Readme

averaged-perceptron

A linear classifier with the averaged perceptron algorithm

Installing

npm install averaged-perceptron

Using

A simple (and unrealistic) example:

import averagedPerceptron from "averaged-perceptron";

const { predict, update } = averagedPerceptron();
const trainingDataset = [
  [{ height: 4, width: 2 }, "slim"],
  [{ height: 2, width: 4 }, "fat"],
  [{ height: 1, width: 4 }, "fat"],
  [{ height: 2, width: 2.1 }, "fat"],
  [{ height: 2.1, width: 2 }, "slim"],
  [{ height: 2, width: 1 }, "slim"],
  [{ height: 1, width: 2 }, "fat"],
  [{ height: 1, width: 1.1 }, "fat"],
  [{ height: 1.1, width: 1 }, "slim"],
  [{ height: 4, width: 1 }, "slim"],
];
const epochs = 1000;
for (let epoch = 0; epoch < epochs; epoch += 1) {
  const shuffledDataset = shuffle(trainingDataset); // Any Fisher–Yates shuffle
  shuffledDataset.forEach(([features, label]) => update(features, label));
}

predict({ height: 8, width: 2 }); // => "slim"
predict({ height: 2.1, width: 2 }); // => "slim"
predict({ height: 2, width: 2.1 }); // => "fat"
predict({ height: 2, width: 8 }); // => "fat"

A slightly more realistic example using the Iris dataset can be found in the tests.

API

averagedPerceptron([weights [, iterations]])

Returns a perceptron object. It may be initialized with weights, an object of objects with the weight of each feature-label pair. When initialized with weights, the number of iterations used to obtain them are iterations, or 0 by default.

import averagedPerceptron from "averaged-perceptron";

// Create a new perceptron
const { predict, update, weights } = averagedPerceptron();

If you want to train the model in multiple sessions, you may resume training by specifying the iterations, which is the number of times update() was called to obtain the weights. That way new update() calls are properly averaged against the pretrained weights.

import averagedPerceptron from "averaged-perceptron";

// Create a perceptron from pretrained weights to do further training
const weightsJSON = '{"x":{"a":0.4,"b":0.6},"y":{"a":0.8,"b":-0.4}}';
const weights = JSON.parse(weightsJSON);
const iterations = 1000; // weights obtained with 1000 update() calls
const { predict, update, weights } = averagedPerceptron(weights, iterations);
// Keep training by calling update()

predict(features)

Returns the label predicted from the values in features, or "" if none exists.

import averagedPerceptron from "averaged-perceptron";

const { predict } = averagedPerceptron({
  x: { a: 0.4, b: 0.6 },
  y: { a: 0.8, b: -0.4 },
});
predict({ x: 1, y: 1 }); // => "a"

update(features, label [, guess])

Returns the perceptron, updating its weights with the respective values in features if label does not equal guess. If guess is not given, it defaults to the output of predict(features).

import averagedPerceptron from "averaged-perceptron";

const { update } = averagedPerceptron();
update({ x: 1, y: 1 }, "a");

Note that update() may be given feature-label pairs whose weights have not been preinitialized, so the model may be used for online learning when the features or labels are unknown a priori.

weights()

Returns an object of objects with the weight of each feature-label pair.

import averagedPerceptron from "averaged-perceptron";

const { weights } = averagedPerceptron({
  x: { a: 0.4, b: 0.6 },
  y: { a: 0.8, b: -0.4 },
});
weights(); // => { x: { a: 0.4, b: 0.6 }, y: { a: 0.8, b: -0.4 } }

Note that the weights are stored as an object of objects, because this perceptron is optimized for sparse features.