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 zero,
    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 some data:
    json load "data" "[]";
    json load "row" "[]";json iarray "row" / 1 NUM;json iarray "row" / 1 NUM;json iarray "data" / (json doc "row") ARRAY;
    json load "row" "[]";json iarray "row" / 3 NUM;json iarray "row" / 2 NUM;json iarray "data" / (json doc "row") ARRAY;
    json load "row" "[]";json iarray "row" / 5 NUM;json iarray "row" / 3 NUM;json iarray "data" / (json doc "row") ARRAY;
    json load "row" "[]";json iarray "row" / 7 NUM;json iarray "row" / 4 NUM;json iarray "data" / (json doc "row") ARRAY;
    json load "row" "[]";json iarray "row" / 9 NUM;json iarray "row" / 7 NUM;json iarray "data" / (json doc "row") ARRAY;
    json load "row" "[]";json iarray "row" / 11 NUM;json iarray "row" / 8 NUM;json iarray "data" / (json doc "row") ARRAY;
    json load "row" "[]";json iarray "row" / 13 NUM;json iarray "row" / 9 NUM;json iarray "data" / (json doc "row") ARRAY;
    json load "row" "[]";json iarray "row" / 15 NUM;json iarray "row" / 10 NUM;json iarray "data" / (json doc "row") ARRAY;
    json doc "data";
    
  • Result:
    "[
      [
        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
      ]
    ]"
    ;
  • Let's see data. To do this, simply execute the command:
    pa xy_scatter (json doc "data") "x, y";
    
  • Here the scatter:
  • Initialize the algorithm:
    pa rl load_empty "reg1";
    
  • Now, let's add data to it:
    json parse_array "data" "/" "row" {
    
    	pa rl add_data "reg1" (json select "row" "/[0]") (json select "row" "/[1]");
    
    };
    
  • Data has been added.

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 adding data as you go.


MentDB © 2012 - 2020 - Legal Notice