[
  {
    "__docId__": 0,
    "kind": "file",
    "name": "lib/mlr.mjs",
    "content": "import { TensorScriptModelInterface, } from '@tensorscript/core';\r\n\r\n/**\r\n * Mulitple Linear Regression with Tensorflow\r\n * @class MultipleLinearRegression\r\n * @implements {TensorScriptModelInterface}\r\n */\r\nexport class MultipleLinearRegression extends TensorScriptModelInterface {\r\n  /**\r\n   * @param {Object} options - tensorflow model hyperparameters\r\n   * @param {{model:Object,tf:Object,}} properties - extra instance properties\r\n   */\r\n  constructor(options = {}, properties = {}) {\r\n    const config = Object.assign({\r\n      epochs:500,\r\n    }, options);\r\n    super(config, properties);\r\n    return this;\r\n  }\r\n  /**\r\n   * Asynchronously trains tensorflow model\r\n   * @override\r\n   * @param {Array<Array<number>>} x_matrix - independent variables\r\n   * @param {Array<Array<number>>} y_matrix - dependent variables\r\n   * @return {Object} returns trained tensorflow model \r\n   */\r\n  async train(x_matrix, y_matrix) {\r\n    const xShape = this.getInputShape(x_matrix);\r\n    const yShape = this.getInputShape(y_matrix);\r\n    const xs = this.tf.tensor(x_matrix, xShape);\r\n    const ys = this.tf.tensor(y_matrix, yShape);\r\n    this.yShape = yShape;\r\n    this.xShape = xShape;\r\n    this.model = this.tf.sequential();\r\n    this.model.add(this.tf.layers.dense({ units: yShape[1], inputShape: [xShape[1],], }));\r\n    this.model.compile({\r\n      loss: 'meanSquaredError',\r\n      optimizer: 'sgd',\r\n    });\r\n    await this.model.fit(xs, ys, this.settings);\r\n    xs.dispose();\r\n    ys.dispose();\r\n    return this.model;\r\n  }\r\n  /**\r\n   * Predicts new dependent variables\r\n   * @override\r\n   * @param {Array<Array<number>>|Array<number>} matrix - new test independent variables\r\n   * @return {{data: Promise}} returns tensorflow prediction \r\n   */\r\n  calculate(input_matrix) {\r\n    const predictionInput = (Array.isArray(input_matrix[ 0 ]))\r\n      ? input_matrix\r\n      : [ input_matrix, ];\r\n    const predictionTensor = this.tf.tensor(predictionInput);\r\n    const prediction = this.model.predict(predictionTensor);\r\n    predictionTensor.dispose();\r\n    return prediction;\r\n  }\r\n}",
    "static": true,
    "longname": "/Users/yawjosephetse/Developer/github/repetere/ts-mlr/lib/mlr.mjs",
    "access": "public",
    "description": null,
    "lineNumber": 1
  },
  {
    "__docId__": 1,
    "kind": "class",
    "name": "MultipleLinearRegression",
    "memberof": "lib/mlr.mjs",
    "static": true,
    "longname": "lib/mlr.mjs~MultipleLinearRegression",
    "access": "public",
    "export": true,
    "importPath": "@tensorscript/ts-mlr/lib/mlr.mjs",
    "importStyle": "{MultipleLinearRegression}",
    "description": "Mulitple Linear Regression with Tensorflow",
    "lineNumber": 8,
    "unknown": [
      {
        "tagName": "@class",
        "tagValue": "MultipleLinearRegression"
      }
    ],
    "interface": false,
    "extends": [
      "@tensorscript/core~TensorScriptModelInterface"
    ],
    "implements": [
      "TensorScriptModelInterface"
    ]
  },
  {
    "__docId__": 2,
    "kind": "constructor",
    "name": "constructor",
    "memberof": "lib/mlr.mjs~MultipleLinearRegression",
    "generator": false,
    "async": false,
    "static": false,
    "longname": "lib/mlr.mjs~MultipleLinearRegression#constructor",
    "access": "public",
    "description": "",
    "lineNumber": 13,
    "params": [
      {
        "nullable": null,
        "types": [
          "Object"
        ],
        "spread": false,
        "optional": false,
        "name": "options",
        "description": "tensorflow model hyperparameters"
      },
      {
        "nullable": null,
        "types": [
          "{model:Object,tf:Object,}"
        ],
        "spread": false,
        "optional": false,
        "name": "properties",
        "description": "extra instance properties"
      }
    ]
  },
  {
    "__docId__": 3,
    "kind": "method",
    "name": "train",
    "memberof": "lib/mlr.mjs~MultipleLinearRegression",
    "generator": false,
    "async": true,
    "static": false,
    "longname": "lib/mlr.mjs~MultipleLinearRegression#train",
    "access": "public",
    "description": "Asynchronously trains tensorflow model",
    "lineNumber": 27,
    "params": [
      {
        "nullable": null,
        "types": [
          "Array<Array<number>>"
        ],
        "spread": false,
        "optional": false,
        "name": "x_matrix",
        "description": "independent variables"
      },
      {
        "nullable": null,
        "types": [
          "Array<Array<number>>"
        ],
        "spread": false,
        "optional": false,
        "name": "y_matrix",
        "description": "dependent variables"
      }
    ],
    "return": {
      "nullable": null,
      "types": [
        "Object"
      ],
      "spread": false,
      "description": "returns trained tensorflow model"
    },
    "override": true
  },
  {
    "__docId__": 4,
    "kind": "member",
    "name": "yShape",
    "memberof": "lib/mlr.mjs~MultipleLinearRegression",
    "static": false,
    "longname": "lib/mlr.mjs~MultipleLinearRegression#yShape",
    "access": "public",
    "description": null,
    "lineNumber": 32,
    "undocument": true
  },
  {
    "__docId__": 5,
    "kind": "member",
    "name": "xShape",
    "memberof": "lib/mlr.mjs~MultipleLinearRegression",
    "static": false,
    "longname": "lib/mlr.mjs~MultipleLinearRegression#xShape",
    "access": "public",
    "description": null,
    "lineNumber": 33,
    "undocument": true
  },
  {
    "__docId__": 6,
    "kind": "member",
    "name": "model",
    "memberof": "lib/mlr.mjs~MultipleLinearRegression",
    "static": false,
    "longname": "lib/mlr.mjs~MultipleLinearRegression#model",
    "access": "public",
    "description": null,
    "lineNumber": 34,
    "undocument": true
  },
  {
    "__docId__": 7,
    "kind": "method",
    "name": "calculate",
    "memberof": "lib/mlr.mjs~MultipleLinearRegression",
    "generator": false,
    "async": false,
    "static": false,
    "longname": "lib/mlr.mjs~MultipleLinearRegression#calculate",
    "access": "public",
    "description": "Predicts new dependent variables",
    "lineNumber": 51,
    "params": [
      {
        "nullable": null,
        "types": [
          "Array<Array<number>>|Array<number>"
        ],
        "spread": false,
        "optional": false,
        "name": "matrix",
        "description": "new test independent variables"
      }
    ],
    "return": {
      "nullable": null,
      "types": [
        "{data: Promise}"
      ],
      "spread": false,
      "description": "returns tensorflow prediction"
    },
    "override": true
  },
  {
    "kind": "index",
    "content": "# @tensorscript/ts-mlr\n\n[![Coverage Status](https://coveralls.io/repos/github/repetere/ts-mlr/badge.svg?branch=master)](https://coveralls.io/github/repetere/ts-mlr?branch=master) [![Build Status](https://travis-ci.org/repetere/ts-mlr.svg?branch=master)](https://travis-ci.org/repetere/ts-mlr)\n\nMultiple Linear Regression with Tensorflow\n### [Full Documentation](<https://github.com/repetere/ts-mlr/blob/master/docs/API.md>)\n\n### Installation\n\n```sh\n$ npm i @tensorscript/ts-mlr\n```\n\n### Usage\n\nTest against the [Portland housing price dataset](http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=MachineLearning&doc=exercises/ex3/ex3.html)\n\n```javascript\nimport { MultipleLinearRegression, } from '@tensorscript/ts-mlr';\nimport ms from 'modelscript';\n\nfunction scaleColumnMap(columnName) {\n  return {\n    name: columnName,\n    options: {\n      strategy: 'scale',\n      scaleOptions: {\n        strategy:'standard'\n      }\n    }\n  }\n}\n\nasync function main(){\n  const housingdataCSV = await ms.csv.loadCSV('./test/mock/data/portland_housing_data.csv', {\n    colParser: {\n      sqft: 'number',\n      bedrooms: 'number',\n      price: 'number',\n    }\n  });\n  /*\n  housingdataCSV = [ \n    { sqft: 2104, bedrooms: 3, price: 399900 },\n    { sqft: 1600, bedrooms: 3, price: 329900 },\n    ...\n    { sqft: 1203, bedrooms: 3, price: 239500 } \n  ] \n  */\n  const DataSet = new ms.DataSet(housingdataCSV);\n  DataSet.fitColumns({\n    columns: [\n      'sqft',\n      'bedrooms',\n      'price',\n    ].map(scaleColumnMap),\n    returnData:true,\n  });\n  const independentVariables = [ 'sqft', 'bedrooms',];\n  const dependentVariables = [ 'price', ];\n  const x_matrix = DataSet.columnMatrix(independentVariables); \n  const y_matrix = DataSet.columnMatrix(dependentVariables);\n  /* x_matrix = [\n      [2014, 3],\n      [1600, 3],\n    ];\n    y_matrix = [\n      [399900],\n      [329900],\n    ];\n    const y_vector = ms.util.pivotVector(y_matrix)[ 0 ];// not used but just illustrative\n    // y_vector = [ 399900, 329900]\n   */\n  const testSqft = DataSet.scalers.get('sqft').scale(1650);\n  const testBedrooms = DataSet.scalers.get('bedrooms').scale(3);\n  const input_x = [\n    testSqft,\n    testBedrooms,\n  ]; // input_x: [ -0.4412732005944351, -0.2236751871685913 ]\n  const tfMLR = new MultipleLinearRegression();\n  const model = await tfMLR.train(x_matrix, y_matrix);\n  const scaledPrediction = await tfMLR.predict(input_x); // [ -0.3785287367962629 ]\n  const prediction = DataSet.scalers.get('price').descale(scaledPrediction); // prediction: 293081.4643348962\n}\n\nmain();\n\n```\n\nThis MLR module give you a similar ml.js interface for machine learning\n\n```javascript\n// Similarly with ml.js\nimport ms from 'modelscript';\nconst MLR = ms.ml.Regression.MultivariateLinearRegression;\nconst regression = new MLR(x_matrix, y_matrix);\nconst MLJSscaledPrediction = regression.predict(input_x); //[ -0.3785287367962629 ],\nconst MLJSprediction = DataSet.scalers.get('price').descale(MLJSscaledPrediction); // prediction: 293081.4643348962\n```\n\n### Testing\n\n```sh\n$ npm i\n$ npm test\n```\n\n### Contributing\n\nFork, write tests and create a pull request!\n\n### Misc\n\nAs of Node 8, ES modules are still used behind a flag, when running natively as an ES module\n\n```sh\n$ node --experimental-modules my-machine-learning-script.mjs\n# 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\n$ npm i --python=/usr/bin/python\n ```\n\nLicense\n----\n\nMIT",
    "longname": "/Users/yawjosephetse/Developer/github/repetere/ts-mlr/README.md",
    "name": "./README.md",
    "static": true,
    "access": "public"
  }
]