项目作者: repetere

项目描述 :
Multiple Linear Regression with TensorFlow
高级语言: JavaScript
项目地址: git://github.com/repetere/ts-mlr.git
创建时间: 2018-07-26T20:14:53Z
项目社区:https://github.com/repetere/ts-mlr

开源协议:MIT License

下载


@tensorscript/ts-mlr

Coverage Status Build Status

Multiple Linear Regression with Tensorflow

Full Documentation

Installation

  1. $ npm i @tensorscript/ts-mlr

Usage

Test against the Portland housing price dataset

  1. import { MultipleLinearRegression, } from '@tensorscript/ts-mlr';
  2. import ms from 'modelscript';
  3. function scaleColumnMap(columnName) {
  4. return {
  5. name: columnName,
  6. options: {
  7. strategy: 'scale',
  8. scaleOptions: {
  9. strategy:'standard'
  10. }
  11. }
  12. }
  13. }
  14. async function main(){
  15. const housingdataCSV = await ms.csv.loadCSV('./test/mock/data/portland_housing_data.csv', {
  16. colParser: {
  17. sqft: 'number',
  18. bedrooms: 'number',
  19. price: 'number',
  20. }
  21. });
  22. /*
  23. housingdataCSV = [
  24. { sqft: 2104, bedrooms: 3, price: 399900 },
  25. { sqft: 1600, bedrooms: 3, price: 329900 },
  26. ...
  27. { sqft: 1203, bedrooms: 3, price: 239500 }
  28. ]
  29. */
  30. const DataSet = new ms.DataSet(housingdataCSV);
  31. DataSet.fitColumns({
  32. columns: [
  33. 'sqft',
  34. 'bedrooms',
  35. 'price',
  36. ].map(scaleColumnMap),
  37. returnData:true,
  38. });
  39. const independentVariables = [ 'sqft', 'bedrooms',];
  40. const dependentVariables = [ 'price', ];
  41. const x_matrix = DataSet.columnMatrix(independentVariables);
  42. const y_matrix = DataSet.columnMatrix(dependentVariables);
  43. /* x_matrix = [
  44. [2014, 3],
  45. [1600, 3],
  46. ];
  47. y_matrix = [
  48. [399900],
  49. [329900],
  50. ];
  51. const y_vector = ms.util.pivotVector(y_matrix)[ 0 ];// not used but just illustrative
  52. // y_vector = [ 399900, 329900]
  53. */
  54. const testSqft = DataSet.scalers.get('sqft').scale(1650);
  55. const testBedrooms = DataSet.scalers.get('bedrooms').scale(3);
  56. const input_x = [
  57. testSqft,
  58. testBedrooms,
  59. ]; // input_x: [ -0.4412732005944351, -0.2236751871685913 ]
  60. const tfMLR = new MultipleLinearRegression();
  61. const model = await tfMLR.train(x_matrix, y_matrix);
  62. const scaledPrediction = await tfMLR.predict(input_x); // [ -0.3785287367962629 ]
  63. const prediction = DataSet.scalers.get('price').descale(scaledPrediction); // prediction: 293081.4643348962
  64. }
  65. main();

This MLR module give you a similar ml.js interface for machine learning

  1. // Similarly with ml.js
  2. import ms from 'modelscript';
  3. const MLR = ms.ml.Regression.MultivariateLinearRegression;
  4. const regression = new MLR(x_matrix, y_matrix);
  5. const MLJSscaledPrediction = regression.predict(input_x); //[ -0.3785287367962629 ],
  6. const MLJSprediction = DataSet.scalers.get('price').descale(MLJSscaledPrediction); // prediction: 293081.4643348962

Testing

  1. $ npm i
  2. $ npm test

Contributing

Fork, write tests and create a pull request!

Misc

As of Node 8, ES modules are still used behind a flag, when running natively as an ES module

  1. $ node --experimental-modules my-machine-learning-script.mjs
  2. # Also there are native bindings that require Python 2.x, make sure if you're using Andaconda, you build with your Python 2.x bin
  3. $ npm i --python=/usr/bin/python

License

MIT