Linear regression | Documentation | MentDB Weak
Goal
  • Simple linear regression is useful for finding relationship between two continuous variables.
  • We are going to give to a linear regression algorithm data from a json array,
    and then we are going to make predictions.
  • Reminder, to execute an order, you can click on or or [CTRL]+E or [Apple]+E on OSX.

Let's start by giving data to the algorithm.
  • Here are a json array (loaded in 'data' name):
    json load "data" "[
      [
        1.0,
        1.0
      ],
      [
        3.0,
        2.0
      ],
      [
        5.0,
        3.0
      ],
      [
        7.0,
        4.0
      ],
      [
        9.0,
        7.0
      ],
      [
        11.0,
        8.0
      ],
      [
        13.0,
        9.0
      ],
      [
        15.0,
        10.0
      ]
    ]";
    
  • Initialize the algorithm with the json array:
    pa rl load_from_json "reg1" (json doc "data");
    
  • Data has been added and the algorithm is ready.

Make predictions.
  • To see how much data is saved:
    pa rl count "reg1";
    
  • Result:
    8;
    
  • To get R:
    pa rl r "reg1";
    
  • Result:
    0.9883173560569456;
    
  • Make prediction with data from 1 to 15 (increment 0.1):
    json load "new_data" "[]";
    for (-> "[x]" 1) (< [x] 15) (-> "[x]" (+ [x] 0.1)) {
    
    	json load "new_row" "[]";
    	json iarray "new_row" / [x] NUM;
    	json iarray "new_row" / (pa rl predict "reg1" [x]) NUM;
    	json iarray "new_data" / (json doc "new_row") ARRAY;
    
    };
    pa xy_scatter (json doc "new_data") "X, Y";
    
  • Here the line:
  • Make simple prediction:
    pa rl predict "reg1" 25;
    
  • Result:
    17.23809523809524;
    
  • Now you can do linear regression by directly loading a json array.


MentDB © 2012 - 2020 - Legal Notice