Skip to content

Latest commit

 

History

History
86 lines (78 loc) · 4.12 KB

README.md

File metadata and controls

86 lines (78 loc) · 4.12 KB

Simple General Regression Neural Network for NodeJS

(With an In-Built CSV Parser)

npm version

Description

This is a NodeJS module to use GRNN to predict given a training data.

The "npm" package can be found here.

Check out the Wikipedia page to find out more about GRNN or the beginner stuffs here.

(This script has no external dependencies)

Input Parameters

Required

double train_x : 2d array of n rows(training size) and m columns(features)
double train_y : 1d array of size n (actual output correspondng to each training input)
double test_x : 2d array of n1 rows(testing size) and m columns(features)
double test_y : 1d array of size n1 (actual output correspondng to each testing input)
double input : 1d array of size n (input data whose Y needs to be predicted)
double sigma : the value of sigma in the Radial Basis Function :: Standard Deviation
boolean normalize : whether to normalize train_x or not (generally normalization of training samples gives better predictions)

Functions

predict(input) - Returns predicted value of given input
mse() - Returns the Mean Squared Error for the given input

Variables

ypred[] - Array which have the predicted values for test input data
optimal_sigma - Value of Optimal Sigma ( Minimum MSE ) -- Must be used after calling mse() function

How to Use ? (Example)

Step 1 : Clone repository

> git clone https://github.com/mannasoumya/grnn.git

Or

Step 1 : Install Via npm

> npm install grnn

Step 2: Import module and use as follows

Or you can use it in your own code

const grnn = require("./grnn"); // assuming cloned repo in cwd; otherwise use appropriate path to grnn.js
// const grnn = require("grnn");  -- > if installed via npm
const train_x = [[1, 2], [5, 6], [10, 11]],
  train_y = [3, 7, 12],
  input = [5.5, 6.5],
  sigma = 2.16,
  normalize = true;
const test_x = [[8.8, 9.8], [13, 14]];
const test_y = [10.8, 15];
const gr = new grnn(train_x, train_y, sigma, normalize, test_x, test_y);
const pred = gr.predict(input);
const mse = gr.mse();
console.log("Prediction:  " + pred);
console.log("MSE:  " + mse);
console.log("Optimal Value of Sigma:  " + gr.optimal_sigma);
console.log("Predicted Values against Test:  "+ gr.ypred);

If you are using CSV parser from file to load data

Here is a snapshot of a sample data.

Note that the Variable to be Predicted must be in the "LAST COLUMN" of the csv file.



Here's a sample code to use the built-in CSV parser

const grnn = require("./grnn"); // assuming cloned repo in cwd; otherwise use appropriate path to grnn.js
// const grnn = require("grnn");  -- > if installed via npm
let gr = new grnn(); // initialize constructor with no parameters
const path="..../data.csv", // path to csv file
  header=true; // if your data contain headers
const data = gr.parseCSV(path, header);
const { train_x, 
        test_x, 
        train_y, 
        test_y } = gr.split_test_train(data); // attribute names are train_x, test_x, train_y, test_y
gr = new grnn(train_x, train_y, 0.1180, false, test_x, test_y); // initialized with actual parameters
const mse = gr.mse();
console.log("MSE:  " + mse);
console.log("Optimal Value of Sigma:  " + gr.optimal_sigma);
console.log("Predicted Values against Test:  "+gr.ypred.map(el=>el.toPrecision(4)));

Please contribute and raise issues. Pull requests are welcome.