From 268dab0967eaa30c579b4f83eb4a78609f5e2fea Mon Sep 17 00:00:00 2001
From: Mandar Deshpande <mandar061095@gmail.com>
Date: Mon, 27 Mar 2017 00:04:13 +0530
Subject: [PATCH 9/9] Added and modified machine learning features

---
 ...Coverity-fix-1371810-missing-break-switch.patch |  28 +++
 ...-Added-machine-learning-features-required.patch | 253 +++++++++++++++++++++
 .../modules/machine_learning/linear_regression.sci |  36 ++-
 .../machine_learning/logistic_regression.sci       |  21 +-
 .../{data_classification.csv => random.csv}        |   0
 scilab/modules/machine_learning/random_linear.csv  |  97 ++++++++
 6 files changed, 406 insertions(+), 29 deletions(-)
 create mode 100644 scilab/modules/api_scilab/src/cpp/0001-api_scilab-Coverity-fix-1371810-missing-break-switch.patch
 create mode 100644 scilab/modules/machine_learning/0008-Added-machine-learning-features-required.patch
 rename scilab/modules/machine_learning/{data_classification.csv => random.csv} (100%)
 create mode 100644 scilab/modules/machine_learning/random_linear.csv

diff --git a/scilab/modules/api_scilab/src/cpp/0001-api_scilab-Coverity-fix-1371810-missing-break-switch.patch b/scilab/modules/api_scilab/src/cpp/0001-api_scilab-Coverity-fix-1371810-missing-break-switch.patch
new file mode 100644
index 00000000000..f13a2331830
--- /dev/null
+++ b/scilab/modules/api_scilab/src/cpp/0001-api_scilab-Coverity-fix-1371810-missing-break-switch.patch
@@ -0,0 +1,28 @@
+From d71f975854d0cfd834824820c94234a38df1db35 Mon Sep 17 00:00:00 2001
+From: Mandar Deshpande <mandar061095@gmail.com>
+Date: Fri, 24 Mar 2017 22:30:31 +0530
+Subject: [PATCH 1/7] api_scilab: Coverity fix #1371810: missing break switch
+ fixed
+
+---
+ scilab/modules/api_scilab/src/cpp/api_stack_list.cpp | 2 ++
+ 1 file changed, 2 insertions(+)
+
+diff --git a/scilab/modules/api_scilab/src/cpp/api_stack_list.cpp b/scilab/modules/api_scilab/src/cpp/api_stack_list.cpp
+index 6d50ef5aad8..214b281f4af 100644
+--- a/scilab/modules/api_scilab/src/cpp/api_stack_list.cpp
++++ b/scilab/modules/api_scilab/src/cpp/api_stack_list.cpp
+@@ -108,8 +108,10 @@ SciErr getListItemNumber(void* _pvCtx, int* _piAddress, int* _piNbItem)
+     switch (iType)
+     {
+         case sci_list :
++		break;
+         case sci_tlist :
+             *_piNbItem = pL->getSize();
++		break;
+         case sci_mlist :
+             // an mlist can also be a struct in Scilab 5 do not manage it in this API
+             if (pIT->isStruct())
+-- 
+2.11.1.windows.1
+
diff --git a/scilab/modules/machine_learning/0008-Added-machine-learning-features-required.patch b/scilab/modules/machine_learning/0008-Added-machine-learning-features-required.patch
new file mode 100644
index 00000000000..6f992c84761
--- /dev/null
+++ b/scilab/modules/machine_learning/0008-Added-machine-learning-features-required.patch
@@ -0,0 +1,253 @@
+From 0ccd371fc67afa4c6fc75b56bcce23e396de7783 Mon Sep 17 00:00:00 2001
+From: Mandar Deshpande <mandar061095@gmail.com>
+Date: Sat, 25 Mar 2017 00:08:44 +0530
+Subject: [PATCH 8/8] Added machine learning features required
+
+logistic_regression
+linera_regression
+---
+ .../machine_learning/data_classification.csv       | 100 +++++++++++++++++++++
+ .../modules/machine_learning/linear_regression.sci |  58 ++++++++++++
+ .../machine_learning/logistic_regression.sci       |  58 ++++++++++++
+ 3 files changed, 216 insertions(+)
+ create mode 100644 scilab/modules/machine_learning/data_classification.csv
+ create mode 100644 scilab/modules/machine_learning/linear_regression.sci
+ create mode 100644 scilab/modules/machine_learning/logistic_regression.sci
+
+diff --git a/scilab/modules/machine_learning/data_classification.csv b/scilab/modules/machine_learning/data_classification.csv
+new file mode 100644
+index 00000000000..5f23f1c8cfc
+--- /dev/null
++++ b/scilab/modules/machine_learning/data_classification.csv
+@@ -0,0 +1,100 @@
++4.8550642421469092,9.6399615658447146,1
++8.6254397593438625,0.058926530182361603,0
++3.8281915383413434,0.72319923434406519,0
++7.1509548369795084,3.899420415982604,1
++6.4779004408046603,8.1981805479153991,1
++1.9222695007920265,1.3314272649586201,0
++8.9782158890739083,0.99343751091510057,1
++6.6356030758470297,8.5428026784211397,1
++7.6723589515313506,5.4163997946307063,1
++4.8660153336822987,2.0426712930202484,0
++6.8614049674943089,9.655309715308249,1
++8.5404213238507509,2.5903742294758558,1
++3.7178806541487575,5.3816621145233512,0
++9.1812971234321594,0.1714746467769146,1
++9.5601400220766664,0.02494648564606905,0
++5.9713694732636213,4.1883018705993891,1
++9.4382026931270957,1.9438124401494861,1
++4.3575510196387768,9.8879833146929741,1
++4.5403319643810391,6.7138733575120568,1
++1.5491016302257776,9.3751321639865637,0
++8.0819737119600177,9.8422068124637008,1
++9.6204650029540062,2.0993275381624699,1
++8.8347709784284234,3.1522041233256459,1
++1.753448536619544,4.2192426044493914,0
++1.0432128375396132,2.6097651151940227,0
++1.1963831819593906,7.4757448583841324,0
++8.9100698800757527,8.2329279417172074,1
++4.5296187419444323,4.9055115412920713,1
++1.8591124145314097,5.6918675592169166,0
++9.5571788400411606,1.6446719132363796,1
++7.1547012263908982,8.0147901969030499,1
++2.3436185251921415,2.9587068501859903,0
++2.922684489749372,8.2175949169322848,1
++6.333096232265234,7.240304984152317,1
++0.92562817502766848,3.4212671080604196,0
++7.8743905667215586,7.7910933550447226,1
++8.3477510465309024,1.8608125066384673,1
++5.5810611322522163,2.4961292929947376,0
++5.0910290936008096,9.8731340887024999,1
++4.5301713701337576,3.7617589998990297,0
++1.4237779891118407,0.22859792690724134,0
++9.0959601290524006,1.0679170489311218,1
++4.0066159190610051,9.792127856053412,1
++8.9765674341470003,3.9351597707718611,1
++0.098052877001464367,7.2145125409588218,0
++0.45238867402076721,2.7746942453086376,0
++3.8630462670698762,3.9132022904232144,0
++7.863850174471736,7.7263833675533533,1
++8.9227064093574882,7.7542167110368609,1
++7.4643678776919842,9.9451762065291405,1
++1.3419292913749814,2.3428780445829034,0
++5.9409695956856012,4.6206316258758307,1
++0.90407765936106443,9.4209287827834487,0
++7.750530056655407,9.0571718849241734,1
++9.5179252931848168,1.3011859031394124,0
++7.7437867131084204,1.1544216889888048,0
++7.691923058591783,8.2982278196141124,1
++7.0922730304300785,2.3574569076299667,0
++6.9444390805438161,6.4847038919106126,1
++0.045024724677205086,3.346005929633975,0
++1.5459691314026713,7.5677000870928168,1
++5.2723831683397293,9.1496153734624386,1
++0.86040707770735025,8.9881881373003125,0
++7.2534389328211546,1.762510621920228,1
++7.5173089792951941,2.2489292873069644,0
++9.0816271863877773,1.4373503997921944,1
++0.45567818451672792,4.7222974756732583,0
++6.9497054163366556,1.411293363198638,0
++9.2821425152942538,8.5293305432423949,1
++7.180812694132328,3.6107634194195271,1
++1.1324883857741952,3.2649118127301335,0
++7.7465284522622824,3.6430192459374666,1
++7.0653604483231902,1.1213281331583858,0
++6.5058174915611744,0.86310222744941711,0
++5.7005291106179357,7.0835442328825593,1
++6.6604666877537966,2.2539557795971632,0
++1.0914720175787807,7.0843769749626517,0
++4.9030876159667969,6.0254777781665325,0
++3.4431093418970704,7.0663468586280942,0
++8.1829780619591475,0.97498656250536442,1
++9.00037647690624,9.5493278605863452,1
++9.6831041388213634,9.5070497319102287,1
++2.991911475546658,5.2992104599252343,0
++2.2381834778934717,4.5348437037318945,0
++0.66547832917422056,9.782636440359056,1
++6.7793187126517296,2.0281807519495487,0
++9.9478409299626946,1.0264578135684133,1
++3.2148492243140936,0.48505899496376514,0
++8.8516463106498122,1.0279159573838115,1
++0.20005786791443825,4.834059551358223,0
++5.1854695053771138,0.73263081256300211,0
++7.2600881475955248,3.9741338323801756,1
++9.1505161253735423,2.5623337319120765,1
++6.4608960598707199,7.0762926898896694,1
++4.7785724932327867,8.2828713255003095,1
++0.022279573604464531,2.6584278885275126,0
++7.6306369295343757,7.4053513957187533,1
++3.6849974654614925,5.0499651208519936,0
++7.4842595355585217,6.0593958059325814,1
++2.0307079795747995,3.9372665341943502,0
+diff --git a/scilab/modules/machine_learning/linear_regression.sci b/scilab/modules/machine_learning/linear_regression.sci
+new file mode 100644
+index 00000000000..2716fdfa57c
+--- /dev/null
++++ b/scilab/modules/machine_learning/linear_regression.sci
+@@ -0,0 +1,58 @@
++t=csvRead("data_classification.csv");
++
++
++b0 = 10;
++t = b0 * rand(100,2);
++t = [t 0.5+0.5*sign(t(:,2)+t(:,1)-b0)];
++
++b = 1;
++flip = find(abs(t(:,2)+t(:,1)-b0)<b);
++t(flip,$)=grand(length(t(flip,$)),1,"uin",0,1);
++
++
++t0 = t(find(t(:,$)==0),:);
++t1 = t(find(t(:,$)==1),:);
++
++clf(0);scf(0);
++plot(t0(:,1),t0(:,2),'bo')
++plot(t1(:,1),t1(:,2),'rx')
++
++
++
++
++x = t(:, 1:$-1); y = t(:, $);
++
++[m, n] = size(x);
++
++
++
++
++// Add intercept term to x
++x = [ones(m, 1) x];
++
++
++
++
++// Initialize fitting parameters
++theta = zeros(n + 1, 1);
++
++// Learning rate and number of iterations
++
++a = 0.01;
++n_iter = 10000;
++
++for iter = 1:n_iter do
++    z = x * theta;
++    h = ones(z) ./ (1+exp(-z));
++    theta = theta - a * x' *(h-y) / m;
++    J(iter) = 1/(2*m) * sum((x*theta -y).^2)
++end
++
++disp(theta)
++
++u = linspace(min(x(:,2)),max(x(:,2)));
++
++clf(1);scf(1);
++plot(t0(:,1),t0(:,2),'bo')
++plot(t1(:,1),t1(:,2),'rx')
++plot(u,-(theta(1)+theta(2)*u)/theta(3),'-g')
+diff --git a/scilab/modules/machine_learning/logistic_regression.sci b/scilab/modules/machine_learning/logistic_regression.sci
+new file mode 100644
+index 00000000000..d4b10fbbac6
+--- /dev/null
++++ b/scilab/modules/machine_learning/logistic_regression.sci
+@@ -0,0 +1,58 @@
++t=csvRead("data_classification.csv");
++
++
++b0 = 10;
++t = b0 * rand(100,2);
++t = [t 0.5+0.5*sign(t(:,2)+t(:,1)-b0)];
++
++b = 1;
++flip = find(abs(t(:,2)+t(:,1)-b0)<b);
++t(flip,$)=grand(length(t(flip,$)),1,"uin",0,1);
++
++
++t0 = t(find(t(:,$)==0),:);
++t1 = t(find(t(:,$)==1),:);
++
++clf(0);scf(0);
++plot(t0(:,1),t0(:,2),'bo')
++plot(t1(:,1),t1(:,2),'rx')
++
++
++
++
++x = t(:, 1:$-1); y = t(:, $);
++
++[m, n] = size(x);
++
++
++
++
++// Add intercept term to x
++x = [ones(m, 1) x];
++
++
++
++
++// Initialize fitting parameters
++theta = zeros(n + 1, 1);
++
++// Learning rate and number of iterations
++
++a = 0.01;
++n_iter = 10000;
++
++for iter = 1:n_iter do
++    z = x * theta;
++    h = ones(z) ./ (1+exp(-z));
++    theta = theta - a * x' *(h-y) / m;
++    J(iter) = (-y' * log(h) - (1-y)' * log(1-h))/m;
++end
++
++disp(theta)
++
++u = linspace(min(x(:,2)),max(x(:,2)));
++
++clf(1);scf(1);
++plot(t0(:,1),t0(:,2),'bo')
++plot(t1(:,1),t1(:,2),'rx')
++plot(u,-(theta(1)+theta(2)*u)/theta(3),'-g')
+-- 
+2.11.1.windows.1
+
diff --git a/scilab/modules/machine_learning/linear_regression.sci b/scilab/modules/machine_learning/linear_regression.sci
index 2716fdfa57c..71c3de2d602 100644
--- a/scilab/modules/machine_learning/linear_regression.sci
+++ b/scilab/modules/machine_learning/linear_regression.sci
@@ -1,26 +1,18 @@
-t=csvRead("data_classification.csv");
+t=csvRead("random_linear.csv");
 
 
-b0 = 10;
-t = b0 * rand(100,2);
-t = [t 0.5+0.5*sign(t(:,2)+t(:,1)-b0)];
 
-b = 1;
-flip = find(abs(t(:,2)+t(:,1)-b0)<b);
-t(flip,$)=grand(length(t(flip,$)),1,"uin",0,1);
-
-
-t0 = t(find(t(:,$)==0),:);
-t1 = t(find(t(:,$)==1),:);
 
+t1= t(:,1);
+t2= t(:,2);
 clf(0);scf(0);
-plot(t0(:,1),t0(:,2),'bo')
-plot(t1(:,1),t1(:,2),'rx')
+plot(t1,t2,'bx');
+
 
 
+x = t1; y = t2;
 
 
-x = t(:, 1:$-1); y = t(:, $);
 
 [m, n] = size(x);
 
@@ -42,17 +34,19 @@ a = 0.01;
 n_iter = 10000;
 
 for iter = 1:n_iter do
-    z = x * theta;
-    h = ones(z) ./ (1+exp(-z));
-    theta = theta - a * x' *(h-y) / m;
+    theta = theta - a * (x' *(x*theta-y)) / m;
     J(iter) = 1/(2*m) * sum((x*theta -y).^2)
 end
 
 disp(theta)
 
-u = linspace(min(x(:,2)),max(x(:,2)));
+
 
 clf(1);scf(1);
-plot(t0(:,1),t0(:,2),'bo')
-plot(t1(:,1),t1(:,2),'rx')
-plot(u,-(theta(1)+theta(2)*u)/theta(3),'-g')
+plot(t1,t2,'rx');
+plot(x(:,2), x*theta, '-')
+
+//cost function plot to check convergence
+clf(2);scf(2);
+plot(1:n_iter, J');
+xtitle('Convergence','Iterations','Cost');
diff --git a/scilab/modules/machine_learning/logistic_regression.sci b/scilab/modules/machine_learning/logistic_regression.sci
index d4b10fbbac6..f62c5567d03 100644
--- a/scilab/modules/machine_learning/logistic_regression.sci
+++ b/scilab/modules/machine_learning/logistic_regression.sci
@@ -1,7 +1,7 @@
-t=csvRead("data_classification.csv");
+t=csvRead("random.csv");
 
 
-b0 = 10;
+b0 = 5;
 t = b0 * rand(100,2);
 t = [t 0.5+0.5*sign(t(:,2)+t(:,1)-b0)];
 
@@ -10,12 +10,12 @@ flip = find(abs(t(:,2)+t(:,1)-b0)<b);
 t(flip,$)=grand(length(t(flip,$)),1,"uin",0,1);
 
 
-t0 = t(find(t(:,$)==0),:);
-t1 = t(find(t(:,$)==1),:);
+c0 = t(find(t(:,$)==0),:);
+c1 = t(find(t(:,$)==1),:);
 
 clf(0);scf(0);
-plot(t0(:,1),t0(:,2),'bo')
-plot(t1(:,1),t1(:,2),'rx')
+plot(c0(:,1),c0(:,2),'bo')
+plot(c1(:,1),c1(:,2),'rx')
 
 
 
@@ -53,6 +53,11 @@ disp(theta)
 u = linspace(min(x(:,2)),max(x(:,2)));
 
 clf(1);scf(1);
-plot(t0(:,1),t0(:,2),'bo')
-plot(t1(:,1),t1(:,2),'rx')
+plot(c0(:,1),c0(:,2),'bo')
+plot(c1(:,1),c1(:,2),'rx')
 plot(u,-(theta(1)+theta(2)*u)/theta(3),'-g')
+
+//cost function plot to check convergence
+clf(2);scf(2);
+plot(1:n_iter, J');
+xtitle('Convergence','Iterations','Cost')
diff --git a/scilab/modules/machine_learning/data_classification.csv b/scilab/modules/machine_learning/random.csv
similarity index 100%
rename from scilab/modules/machine_learning/data_classification.csv
rename to scilab/modules/machine_learning/random.csv
diff --git a/scilab/modules/machine_learning/random_linear.csv b/scilab/modules/machine_learning/random_linear.csv
new file mode 100644
index 00000000000..0f88ccb611f
--- /dev/null
+++ b/scilab/modules/machine_learning/random_linear.csv
@@ -0,0 +1,97 @@
+6.1101,17.592
+5.5277,9.1302
+8.5186,13.662
+7.0032,11.854
+5.8598,6.8233
+8.3829,11.886
+7.4764,4.3483
+8.5781,12
+6.4862,6.5987
+5.0546,3.8166
+5.7107,3.2522
+14.164,15.505
+5.734,3.1551
+8.4084,7.2258
+5.6407,0.71618
+5.3794,3.5129
+6.3654,5.3048
+5.1301,0.56077
+6.4296,3.6518
+7.0708,5.3893
+6.1891,3.1386
+20.27,21.767
+5.4901,4.263
+6.3261,5.1875
+5.5649,3.0825
+18.945,22.638
+12.828,13.501
+10.957,7.0467
+13.176,14.692
+22.203,24.147
+5.2524,-1.22
+6.5894,5.9966
+9.2482,12.134
+5.8918,1.8495
+8.2111,6.5426
+7.9334,4.5623
+8.0959,4.1164
+5.6063,3.3928
+12.836,10.117
+6.3534,5.4974
+5.4069,0.55657
+6.8825,3.9115
+11.708,5.3854
+5.7737,2.4406
+7.8247,6.7318
+7.0931,1.0463
+5.0702,5.1337
+5.8014,1.844
+11.7,8.0043
+5.5416,1.0179
+7.5402,6.7504
+5.3077,1.8396
+7.4239,4.2885
+7.6031,4.9981
+6.3328,1.4233
+6.3589,-1.4211
+6.2742,2.4756
+5.6397,4.6042
+9.3102,3.9624
+9.4536,5.4141
+8.8254,5.1694
+5.1793,-0.74279
+21.279,17.929
+14.908,12.054
+18.959,17.054
+7.2182,4.8852
+8.2951,5.7442
+10.236,7.7754
+5.4994,1.0173
+20.341,20.992
+10.136,6.6799
+7.3345,4.0259
+6.0062,1.2784
+7.2259,3.3411
+5.0269,-2.6807
+6.5479,0.29678
+7.5386,3.8845
+5.0365,5.7014
+10.274,6.7526
+5.1077,2.0576
+5.7292,0.47953
+5.1884,0.20421
+6.3557,0.67861
+9.7687,7.5435
+6.5159,5.3436
+8.5172,4.2415
+9.1802,6.7981
+6.002,0.92695
+5.5204,0.152
+5.0594,2.8214
+5.7077,1.8451
+7.6366,4.2959
+5.8707,7.2029
+5.3054,1.9869
+8.2934,0.14454
+13.394,9.0551
+5.4369,0.61705
-- 
2.11.1.windows.1

