Security News
JavaScript Leaders Demand Oracle Release the JavaScript Trademark
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
@kobra-dev/js-regression
Advanced tools
Package implements linear regression and logistic regression
Package provides javascript implementation of linear regression and logistic regression
yarn add @kobra-dev/js-regression
The sample code below illustrates how to run the multiple linear regression (polynomial in this case):
var jsregression = require('js-regression');
// === training data generated from y = 2.0 + 5.0 * x + 2.0 * x^2 === //
var data = [];
for(var x = 1.0; x < 100.0; x += 1.0) {
var y = 2.0 + 5.0 * x + 2.0 * x * x + Math.random() * 1.0;
data.push([x, x * x, y]); // Note that the last column should be y the output
}
// === Create the linear regression === //
var regression = new jsregression.LinearRegression({
alpha: 0.001, //
iterations: 300,
lambda: 0.0
});
// can also use default configuration: var regression = new jsregression.LinearRegression();
// === Train the linear regression === //
var model = regression.fit(data);
// === Print the trained model === //
console.log(model);
// === Testing the trained linear regression === //
var testingData = [];
for(var x = 1.0; x < 100.0; x += 1.0) {
var actual_y = 2.0 + 5.0 * x + 2.0 * x * x + Math.random() * 1.0;
var predicted_y = regression.transform([x, x * x]);
console.log("actual: " + actual_y + " predicted: " + predicted_y);
}
The sample code below illustrates how to run the logistic regression on the iris datsets to classify whether a data row belong to species Iris-virginica:
var jsregression = require('js-regression');
var iris = require('js-datasets-iris');
// === Create the linear regression === //
var logistic = new jsregression.LogisticRegression({
alpha: 0.001,
iterations: 1000,
lambda: 0.0
});
// can also use default configuration: var logistic = new jsregression.LogisticRegression();
// === Create training data and testing data ===//
iris.shuffle();
var trainingDataSize = Math.round(iris.rowCount * 0.8);
var trainingData = [];
var testingData = [];
for(var i=0; i < iris.rowCount ; ++i) {
var row = [];
row.push(iris.data[i][0]); // sepalLength;
row.push(iris.data[i][1]); // sepalWidth;
row.push(iris.data[i][2]); // petalLength;
row.push(iris.data[i][3]); // petalWidth;
row.push(iris.data[i][4] == "Iris-virginica" ? 1.0 : 0.0); // output which is 1 if species is Iris-virginica; 0 otherwise
if(i < trainingDataSize) {
trainingData.push(row);
} else {
testingData.push(row);
}
}
// === Train the logistic regression === //
var model = logistic.fit(trainingData);
// === Print the trained model === //
console.log(model);
// === Testing the trained logistic regression === //
for(var i=0; i < testingData.length; ++i){
var probabilityOfSpeciesBeingIrisVirginica = logistic.transform(testingData[i]);
var predicted = logistic.transform(testingData[i]) >= logistic.threshold ? 1 : 0;
console.log("actual: " + testingData[i][4] + " probability of being Iris-virginica: " + probabilityOfSpeciesBeingIrisVirginica);
console.log("actual: " + testingData[i][4] + " predicted: " + predicted);
}
The sample code below illustrates how to run the multi-class classifier on the iris datasets to classifiy the species of each data row:
var classifier = new jsregression.MultiClassLogistic({
alpha: 0.001,
iterations: 1000,
lambda: 0.0
});
iris.shuffle();
var trainingDataSize = Math.round(iris.rowCount * 0.9);
var trainingData = [];
var testingData = [];
for(var i=0; i < iris.rowCount ; ++i) {
var row = [];
row.push(iris.data[i][0]); // sepalLength;
row.push(iris.data[i][1]); // sepalWidth;
row.push(iris.data[i][2]); // petalLength;
row.push(iris.data[i][3]); // petalWidth;
row.push(iris.data[i][4]); // output is species
if(i < trainingDataSize){
trainingData.push(row);
} else {
testingData.push(row);
}
}
var result = classifier.fit(trainingData);
console.log(result);
for(var i=0; i < testingData.length; ++i){
var predicted = classifier.transform(testingData[i]);
console.log("actual: " + testingData[i][4] + " predicted: " + predicted);
}
Include the "node_modules/js-regression/build/jsregression.min.js" (or "node_modules/js-regression/src/jsregression.js") in your HTML <script> tag
The codes in the following html files illustrates how to use them in html pages:
FAQs
Package implements linear regression and logistic regression
The npm package @kobra-dev/js-regression receives a total of 25 weekly downloads. As such, @kobra-dev/js-regression popularity was classified as not popular.
We found that @kobra-dev/js-regression demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.