|
| 1 | +/*############################################################################## |
| 2 | +
|
| 3 | + HPCC SYSTEMS software Copyright (C) 2022 HPCC Systems®. |
| 4 | +
|
| 5 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + you may not use this file except in compliance with the License. |
| 7 | + You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | + Unless required by applicable law or agreed to in writing, software |
| 12 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + See the License for the specific language governing permissions and |
| 15 | + limitations under the License. |
| 16 | +############################################################################## */ |
| 17 | + |
| 18 | +#ONWARNING(4550, ignore); |
| 19 | + |
| 20 | +// Modified version of the testCovTypeReg test file that works with the |
| 21 | +// OBT test system |
| 22 | + |
| 23 | +IMPORT $.^.test.datasets.CovTypeDS; |
| 24 | +IMPORT $.^ AS LT; |
| 25 | +IMPORT LT.LT_Types; |
| 26 | +IMPORT ML_Core; |
| 27 | +IMPORT ML_Core.Types; |
| 28 | + |
| 29 | +numTrees := 400; |
| 30 | +maxDepth := 255; |
| 31 | +numFeatures := 0; // Zero is automatic choice |
| 32 | +nonSequentialIds := TRUE; // True to renumber ids, numbers and work-items to test |
| 33 | + // support for non-sequentiality |
| 34 | +numWIs := 1; // The number of independent work-items to create |
| 35 | +maxRecs := 500; // Note that this has to be less than or equal to the number of records |
| 36 | + // in CovTypeDS (currently 500) |
| 37 | + |
| 38 | +maxTestRecs := 100; |
| 39 | +NumericField := Types.NumericField; |
| 40 | +trainDat := CovTypeDS.trainRecs; |
| 41 | +testDat := CovTypeDS.testRecs; |
| 42 | +nominalFields := CovTypeDS.nominalCols; |
| 43 | +DependentVar := 1; // Dependent Variable meant for this function |
| 44 | + |
| 45 | +RegressTest() := FUNCTION |
| 46 | + |
| 47 | + ML_Core.ToField(trainDat, trainNF); // Get training data as a field |
| 48 | + ML_Core.ToField(testDat, testNF); // Get test data as a field |
| 49 | + |
| 50 | + // Take out the first field from training set (Elevation) to use as the target value. Re-number the other fields |
| 51 | + // to fill the gap |
| 52 | + |
| 53 | + //Ind = independent, Dep = dependent |
| 54 | + Ind1 := PROJECT(trainNF(number != DependentVar AND id <= maxRecs), TRANSFORM(NumericField, |
| 55 | + SELF.number := IF(nonSequentialIds, (5*LEFT.number -1), LEFT.number -1), |
| 56 | + SELF.id := IF(nonSequentialIds, 5*LEFT.id, LEFT.id), |
| 57 | + SELF := LEFT)); |
| 58 | + Dep1 := PROJECT(trainNF(number = DependentVar AND id <= maxRecs), TRANSFORM(NumericField, |
| 59 | + SELF.number := DependentVar, |
| 60 | + SELF.id := IF(nonSequentialIds, 5*LEFT.id, LEFT.id), |
| 61 | + SELF := LEFT)); |
| 62 | + |
| 63 | + // Generate multiple work items |
| 64 | + Ind2 := NORMALIZE(Ind1, numWIs, TRANSFORM(RECORDOF(LEFT), |
| 65 | + SELF.wi := IF(nonSequentialIds, 5*COUNTER, COUNTER), |
| 66 | + SELF := LEFT)); |
| 67 | + Dep2 := NORMALIZE(Dep1, numWIs, TRANSFORM(RECORDOF(LEFT), |
| 68 | + SELF.wi := IF(nonSequentialIds, 5*COUNTER, COUNTER), |
| 69 | + SELF := LEFT)); |
| 70 | + |
| 71 | + Forest := LT.RegressionForest(numTrees:=numTrees, featuresPerNode:=numFeatures, maxDepth:=maxDepth, nominalFields:=nominalFields); |
| 72 | + model := Forest.GetModel(Ind2, Dep2); |
| 73 | + |
| 74 | + maxTestId := MIN(testNF, id) + maxTestRecs; |
| 75 | + testNF2 := testNF(id < maxTestId); |
| 76 | + |
| 77 | + Indtest1 := PROJECT(testNF2(number != DependentVar), TRANSFORM(NumericField, |
| 78 | + SELF.number := IF(nonSequentialIds, (5*LEFT.number -1), LEFT.number -1), |
| 79 | + SELF.id := IF(nonSequentialIds, 5*LEFT.id, LEFT.id), |
| 80 | + SELF := LEFT)); |
| 81 | + DepCmp1 := PROJECT(testNF2(number = DependentVar), TRANSFORM(NumericField, |
| 82 | + SELF.number := DependentVar, |
| 83 | + SELF.id := IF(nonSequentialIds, 5*LEFT.id, LEFT.id), |
| 84 | + SELF := LEFT)); |
| 85 | + |
| 86 | + // Generate multiple work items |
| 87 | + IndTest2 := NORMALIZE(IndTest1, numWIs, TRANSFORM(RECORDOF(LEFT), |
| 88 | + SELF.wi := IF(nonSequentialIds, 5*COUNTER, COUNTER), |
| 89 | + SELF := LEFT)); |
| 90 | + DepCmp2 := NORMALIZE(DepCmp1, numWIs, TRANSFORM(RECORDOF(LEFT), |
| 91 | + SELF.wi := IF(nonSequentialIds, 5*COUNTER, COUNTER), |
| 92 | + SELF := LEFT)); |
| 93 | + |
| 94 | + // Determine accuracy |
| 95 | + RETURN Forest.Accuracy(model, DepCmp2, IndTest2); |
| 96 | +END; |
| 97 | + |
| 98 | +accuracy := RegressTest(); |
| 99 | + |
| 100 | +// Result should be at least 70% accurate |
| 101 | +OUTPUT(accuracy, {passing := IF(r2 > 0.70, 'Pass', 'Fail, ' + r2)}, NAMED('Result')); |
0 commit comments