Created
June 16, 2017 04:36
-
-
Save curist/38d6f7f081a14ec61f0eb1e0468e8b34 to your computer and use it in GitHub Desktop.
Using shaman.js, dump trained model, and use it to do prediction.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { LinearRegression } = require('shaman'); | |
require('sylvester'); | |
LinearRegression.prototype.dump = function() { | |
return { | |
means: this.means, | |
ranges: this.ranges, | |
theta: this.theta.elements, | |
} | |
}; | |
function Predictor({means, ranges, theta}) { | |
theta = $M(theta); | |
return function predict(input) { | |
if (!Array.isArray(input)) { | |
input = [input]; | |
} | |
input = input.map((val, i) => (val - means[i]) / ranges[i]); | |
const xInput = $V([1]).augment(input); | |
const output = theta.transpose().x(xInput); | |
return output.e(1,1); | |
} | |
} | |
// usage | |
var lr = new LinearRegression(X,Y, { | |
algorithm: 'GradientDescent', | |
numberOfIterations: 1000, | |
learningRate: 0.5, | |
}); | |
lr.train(err => { | |
if(err) { | |
throw err; | |
} | |
const model = lr.dump(); | |
const predict = Predictor(model); | |
predict(1) === 10; // etc | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment