0% found this document useful (0 votes)
96 views

Linear Regression Algorithm in PL/SQL

Using data on the heights and weights of 8 students, this PL/SQL script implements a linear regression algorithm to predict the weight of a student who is 71 inches tall. The script declares arrays to hold the height and weight data, calculates the sums and products needed for the linear regression calculation, and outputs the predicted intercept and slope coefficients as well as the resulting predicted weight.

Uploaded by

JP Vijaykumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
96 views

Linear Regression Algorithm in PL/SQL

Using data on the heights and weights of 8 students, this PL/SQL script implements a linear regression algorithm to predict the weight of a student who is 71 inches tall. The script declares arrays to hold the height and weight data, calculates the sums and products needed for the linear regression calculation, and outputs the predicted intercept and slope coefficients as well as the resulting predicted weight.

Uploaded by

JP Vijaykumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 1

********************************************************************************

--LINEAR REGRESSION ALGORITHM


********************************************************************************
--LINEAR REGRESSION ALGORITHM IN PL/SQL
--------------------------------------------------------------------------------
Modified Mar 8th 2019
Using Linear Regression algorithm, find the weight of a student, who is 71 inches
tall.
Given the height and weight of 8 students.

This script is for educational purpose only.


Pls make necessary changes to the script, as may be necessary for use in your
environment.

I generated this script and tested it in Oracle 12C.

connect Veeksha/Saketh

set serverout on size 1000000 timing on


DECLARE
type tallarray IS VARRAY(8) OF float;
type heavyarray IS VARRAY(8) OF float;
height tallarray;
weight heavyarray;
total float;
xy float:=0;
sx float:=0;
sy float:=0;
x2 float:=0;
b1 float;
b0 float;
BEGIN
height := tallarray(65,65,62,67,69,65,61,67);
weight := heavyarray(105,125,110,120,140,135,95,130);
total := height.count;
FOR i in 1 .. total LOOP
xy:=xy + round((height(i)*weight(i)),2);
sx:=sx + height(i);
sy:=sy + weight(i);
x2:=x2 + height(i)**2;
END LOOP;
b1:=round((xy - ((sx * sy)/total))/(x2 - (sx**2/total)),2);
b0:=round((sy - (b1*sx))/total,2);
dbms_output.put_line(sx||' '||sy||' '||x2||' '||xy||' '||b0||' '||b1||' '||(b0 +
b1*71));
END;
/
521 960 33979 62750 -186.74 4.71 147.67

Verified the weightn of the student, who is 71 inches tall using python scripting.

PL/SQL procedure successfully completed

You might also like