packagemachineLearning.adaboosting;importweka.core.Instances;importjava.io.FileReader;importjava.util.Arrays;/*** ClassName: WeightedInstances* Package: machineLearning.adaboosting* Description:Weighted instances.** @Author: luv_x_c* @Create: 2023/8/13 15:12*/publicclassWeightedInstancesextendsInstances{/*** Just the requirement of some classes, any number is ok.*/privatestaticfinallong serialVersionUID =11087456L;/*** Weights.*/privatedouble[] weights;/*** The first constructor.** @param paraFileReader The given reader to read data from file.*/publicWeightedInstances(FileReader paraFileReader)throwsException{super(paraFileReader);setClassIndex(numAttributes()-1);//Initialize weights.weights =newdouble[numInstances()];double tempAverage =1.0/numInstances();Arrays.fill(weights, tempAverage);System.out.println("Instances weights are: "+Arrays.toString(weights));}//Of the first constructor/*** The second constructor.** @param paraInstances The given instances.*/publicWeightedInstances(Instances paraInstances){super(paraInstances);setClassIndex(numAttributes()-1);// Initialize weights.weights =newdouble[numInstances()];double tempAverage =1.0/numInstances();Arrays.fill(weights, tempAverage);System.out.println("Instances weights are: "+Arrays.toString(weights));}//Of the second constructor/*** Getter.** @param paraIndex The given index.* @return The weight of the given index.*/publicdoublegetWeight(int paraIndex){return weights[paraIndex];}//Of getWeight/*** Adjust the weights.** @param paraCorrectArray Indicate which instance have been correctly classify.* @param paraAlpha The weight of the last classifier.*/publicvoidadjustWeights(boolean[] paraCorrectArray,double paraAlpha){//Step1. Calculate alpha.double tempIncrease =Math.exp(paraAlpha);// Step2. Adjust.double tempWeightsSum =0;for(int i =0; i < weights.length; i++){if(paraCorrectArray[i]){weights[i]/= tempIncrease;}else{weights[i]*= tempIncrease;}//Of iftempWeightsSum += weights[i];}//Of for i// Step3. Normalize.for(int i =0; i < weights.length; i++){weights[i]/= tempWeightsSum;}//Of for iSystem.out.println("After adjusting, instances weights are: "+Arrays.toString(weights));}//Of adjustWeights/*** Test the method.*/publicvoidadjustWeightsTest(){boolean[] tempCorrectArray =newboolean[numInstances()];Arrays.fill(tempCorrectArray,true);double tempWeightError =0.3;adjustWeights(tempCorrectArray, tempWeightError);System.out.println("After adjusting: ");System.out.println(toString());}// Of adjustWeightsTest@OverridepublicStringtoString(){String resultString ="I am a weighted Instances object.\r\n"+" I have "+numInstances()+" "+"instances and "+(numAttributes()-1)+" conditional attributes.\r\n"+" My weights are: "+Arrays.toString(weights)+"\r\n"+" My data are:\r\n"+super.toString();return resultString;}//Of toString/*** @param args Not provided.*/publicstaticvoidmain(String[] args){WeightedInstances tempWeightInstances =null;String tempFileName ="E:\\java_code\\data\\sampledata\\iris.arff";try{FileReader fileReader =newFileReader(tempFileName);tempWeightInstances =newWeightedInstances(fileReader);fileReader.close();}catch(Exception ee){System.out.println("Cannot read the file: "+ tempFileName +"\r\n"+ ee);System.exit(0);}//Of trySystem.out.println(tempWeightInstances.toString());tempWeightInstances.adjustWeightsTest();}//Of main}//OF class WeightedInstances
packagemachineLearning.adaboosting;importweka.core.Instance;importjava.io.FileReader;importjava.util.Arrays;/*** ClassName: StumpClassifier* Package: machineLearning.adaboosting* Description:The stump classifier.** @Author: luv_x_c* @Create: 2023/8/17 20:37*/publicclassStumpClassifierextendsSimpleClassifier{/*** The best cut for the current attribute on weightInstances.*/double bestCut;/*** The class label for attribute value less than bestCut.*/int leftLeafLabel;/*** The class label for attribute value no less than bestCut.*/int rightLeafLabel;/*** The only constructor.** @param paraWeightedInstances The given instances.*/publicStumpClassifier(WeightedInstances paraWeightedInstances){super(paraWeightedInstances);}//Of the only constructorpublicvoidtrain(){//Step1. Randomly choose an attribute.selectedAttribute = random.nextInt(numConditions);//Step2. Find all attributes values and sort.double[] tempValuesArray =newdouble[numInstances];for(int i =0; i < tempValuesArray.length; i++){tempValuesArray[i]= weightedInstances.instance(i).value(selectedAttribute);}//Of for iArrays.sort(tempValuesArray);//Step3. Initialize, classify all instances as the same with the original cut.int tempNumLabels = numClasses;double[] tempLabelCountArray =newdouble[tempNumLabels];int tempCurrentLabel;//Step3.1 Scan all labels to obtain their counts.for(int i =0; i < numInstances; i++){// The label of the ith instancetempCurrentLabel =(int) weightedInstances.instance(i).classValue();tempLabelCountArray[tempCurrentLabel]+= weightedInstances.getWeight(i);}//Of for i//Step3.2 Find the label with the maximal count.double tempMaxCorrect =0;int tempBestLabel =-1;for(int i =0; i < tempLabelCountArray.length; i++){if(tempMaxCorrect < tempLabelCountArray[i]){tempMaxCorrect = tempLabelCountArray[i];tempBestLabel = i;}//Of if}//Of for i//Steep3.3 The cut is a little smaller than the minimal value.bestCut = tempValuesArray[0]-0.1;leftLeafLabel = tempBestLabel;rightLeafLabel = tempBestLabel;// Step4. Check candidate cuts one by one.// Step4.1 To handle multi-class data, left and right.double tempCut;double[][] tempLabelCountMatrix =newdouble[2][tempNumLabels];for(int i =0; i < tempValuesArray.length -1; i++){// Step4.1 Some attribute values are identical, ignore them.if(tempValuesArray[i]== tempValuesArray[i +1]){continue;}//Of iftempCut =(tempValuesArray[i]+ tempValuesArray[i +1])/2;// Step4.2 Scan all labels to obtain their counts wrt, the cut .// Initialize again since it is used many times.for(int j =0; j <2; j++){for(int k =0; k < tempNumLabels; k++){tempLabelCountMatrix[j][k]=0;}//Of for k}//Of for jfor(int j =0; j < numInstances; j++){// The label of the jth instance.tempCurrentLabel =(int) weightedInstances.instance(j).classValue();if(weightedInstances.instance(j).value(selectedAttribute)< tempCut){tempLabelCountMatrix[0][tempCurrentLabel]+= weightedInstances.getWeight(j);}else{tempLabelCountMatrix[1][tempCurrentLabel]+= weightedInstances.getWeight(j);}//Of if}//Of for j// Step4.3 Left leafdouble tempLeftMaxCorrect =0;int tempLeftBestLabel =0;for(int j =0; j < tempLabelCountMatrix[0].length; j++){if(tempLeftMaxCorrect < tempLabelCountMatrix[0][j]){tempLeftMaxCorrect = tempLabelCountMatrix[0][j];tempLeftBestLabel = j;}//Of if}//Of for j// Step 4.4 Right leafdouble tempRightMaxCorrect =0;int tempRightBestLabel =0;for(int j =0; j < tempLabelCountMatrix[0].length; j++){if(tempRightMaxCorrect < tempLabelCountMatrix[1][j]){tempRightMaxCorrect = tempLabelCountMatrix[1][j];tempRightBestLabel = j;}//Of if}//Of for j// Step 4.5 Compare with the current bestif(tempMaxCorrect < tempLeftMaxCorrect + tempRightMaxCorrect){tempMaxCorrect = tempLeftMaxCorrect + tempRightMaxCorrect;bestCut = tempCut;leftLeafLabel = tempLeftBestLabel;rightLeafLabel = tempRightBestLabel;}//Of if}//Of for iSystem.out.println("Attribute = "+ selectedAttribute +", cut ="+ bestCut +", "+"leftLeafLabel = "+ leftLeafLabel +", rightLeafLabel"+ rightLeafLabel);}//Of train@Overridepublicintclassify(Instance paraInstance){int resultLabel =-1;if(paraInstance.value(selectedAttribute)< bestCut){resultLabel = leftLeafLabel;}else{resultLabel = rightLeafLabel;}//Of ifreturn resultLabel;}//Of classify@OverridepublicStringtoString(){return"I am a stump classifier.\r\n"+"I choose attribute #"+ selectedAttribute+" with cut value "+ bestCut +".\r\n"+"The left and right leaf labels are "+ leftLeafLabel+" and "+ rightLeafLabel +", respectively.\r\n"+"My weighted error is: "+computeWeightedError()+".\r\n"+"My weighted accuracy is : "+computeTrainingAccuracy()+".";}//Of toString/*** For unit test.** @param args Not used.*/publicstaticvoidmain(String[] args){WeightedInstances tempWeightedInstance =null;String tempFileName ="E:\\java_code\\data\\sampledata\\iris.arff";try{FileReader fileReader =newFileReader(tempFileName);tempWeightedInstance =newWeightedInstances(fileReader);fileReader.close();}catch(Exception e){System.out.println("Cannot read the file: "+ tempFileName +"\r\n"+ e);System.exit(0);}//OF tryStumpClassifier tempClassifier =newStumpClassifier(tempWeightedInstance);tempClassifier.train();System.out.println(tempClassifier);System.out.println(Arrays.toString(tempClassifier.computeCorrectnessArray()));}//OF main}//Of class StumpClassifier
packagemachineLearning.adaboosting;importweka.core.Instance;importjava.util.Random;/*** ClassName: SimpleClassifier* Package: machineLearning.adaboosting* Description:The super class of any simple classifier.** @Author: luv_x_c* @Create: 2023/8/14 13:43*/publicabstractclassSimpleClassifier{/*** The index of the current attribute.*/int selectedAttribute;/*** Weighted data.*/WeightedInstances weightedInstances;/*** The accuracy on the training set.*/double trainingAccuracy;/*** The number of instances.*/int numInstances;/*** The number of instances.*/int numClasses;/*** The number of conditional attributes.*/int numConditions;Random random =newRandom();/*** The first constructor.** @param paraWeightedInstances The given instances.*/publicSimpleClassifier(WeightedInstances paraWeightedInstances){weightedInstances = paraWeightedInstances;numConditions = weightedInstances.numAttributes()-1;numInstances = weightedInstances.numInstances();numClasses = weightedInstances.classAttribute().numValues();}// Of the first constructor/*** Train the classifier.*/publicabstractvoidtrain();/*** Classify an instance.** @param paraInstance The given instance.* @return Predicted label.*/publicabstractintclassify(Instance paraInstance);/*** Which instance in the training set are correctly classified.** @return The correctness array.*/publicboolean[]computeCorrectnessArray(){boolean[] resultCorrectnessArray =newboolean[weightedInstances.numInstances()];for(int i =0; i < resultCorrectnessArray.length; i++){Instance tempInstance = weightedInstances.instance(i);if((int)(tempInstance.classValue())==classify(tempInstance)){resultCorrectnessArray[i]=true;}// OF if}//Of for ireturn resultCorrectnessArray;}//Of computeCorrectnessArray/*** Compute the accuracy on the training set.** @return The training accuracy.*/publicdoublecomputeTrainingAccuracy(){double tempCorrect =0;boolean[] tempCorrectnessArray =computeCorrectnessArray();for(int i =0; i < tempCorrectnessArray.length; i++){if(tempCorrectnessArray[i]){tempCorrect++;}//Of if}//Of for idouble resultAccuracy = tempCorrect / tempCorrectnessArray.length;return resultAccuracy;}//Of computeTrainingAccuracypublicdoublecomputeWeightedError(){double resultError=0;boolean[]tempCorrectnessArray=computeCorrectnessArray();for(int i =0; i < tempCorrectnessArray.length; i++){if(!tempCorrectnessArray[i]){resultError+=weightedInstances.getWeight(i);}//Of if}//Of for iif(resultError<1e-6){resultError=1e-6;}//Of ifreturn resultError;}//Of computeWeightedError}//Of class SimpleClassifier
packagemachineLearning.adaboosting;importweka.core.Instance;importweka.core.Instances;importjava.io.FileReader;importjava.io.IOException;/*** ClassName: Booster* Package: machineLearning.adaboosting* Description: The booster which ensembles base classifiers.** @Author: luv_x_c* @Create: 2023/8/18 14:34*/publicclassBooster{/*** Classifiers.*/SimpleClassifier[] classifiers;/*** Number of classifiers.*/int numClassifiers;/*** Whether stop after the training error is 0.*/boolean stopAfterConverge =false;/*** The weights of classifier.*/double[] classifierWeights;/*** The training data.*/Instances trainingData;/*** The testing data.*/Instances testingData;/*** The first constructor. The testing set is the same as the training data.** @param paraTrainingFileName The data file name.*/publicBooster(String paraTrainingFileName){// Step1. Read the training set.try{FileReader fileReader =newFileReader(paraTrainingFileName);trainingData =newInstances(fileReader);fileReader.close();}catch(IOException e){System.out.println("Cannot read the file: "+ paraTrainingFileName +"\r\n"+ e);System.exit(0);}//Of try// Step2. Set the last attribute as the class indextrainingData.setClassIndex(trainingData.numAttributes()-1);// Step3. The testing data is the same as the training datatestingData = trainingData;stopAfterConverge =true;System.out.println("****************Data**********\r\n"+ trainingData);}//Of the first constructor/*** Set the number of base classifier, and allocate space for them.** @param paraNumBaseClassifiers The number of base classifier.*/publicvoidsetNumBaseClassifiers(int paraNumBaseClassifiers){numClassifiers = paraNumBaseClassifiers;// Step1. Allocate space for classifiersclassifiers =newSimpleClassifier[numClassifiers];// Step2. Initialize classifier weightsclassifierWeights =newdouble[numClassifiers];}//Of setNumBaseClassifiers/*** Train the booster.*/publicvoidtrain(){// Step1. Initialize.WeightedInstances tempWeightedInstances =null;double tempError;numClassifiers =0;// Step2. Build other classifier.for(int i =0; i < classifiers.length; i++){// Step2.1 Key code: Construct or adjust the weightedInstancesif(i ==0){tempWeightedInstances =newWeightedInstances(trainingData);}else{// Adjust the weights of the datatempWeightedInstances.adjustWeights(classifiers[i -1].computeCorrectnessArray(),classifierWeights[i -1]);}//Of if// Step 2.2 Train the next classifier.classifiers[i]=newStumpClassifier(tempWeightedInstances);classifiers[i].train();tempError = classifiers[i].computeWeightedError();// Key code: Set the classifier weight.classifierWeights[i]=0.5*Math.log(1/ tempError -1);if(classifierWeights[i]<1e-6){classifierWeights[i]=0;}//Of ifSystem.out.println("Classifier #"+ i +" , weighted error = "+ tempError +" , "+"weight = "+ classifierWeights[i]+"\r\n");numClassifiers++;// The accuracy is enough.if(stopAfterConverge){double tempTrainAccuracy =computeTrainingAccuracy();System.out.println("The accuracy of the booster is: "+ tempTrainAccuracy +"\r\n");if(tempTrainAccuracy >0.99999){System.out.println("Stop at the round: "+ i +" due to converge.\r\n");break;}//Of if}//of if}//Of for i}//Of for train/*** Classify an instance** @param paraInstance The given instance* @return The predicted label*/publicintclassify(Instance paraInstance){double[] tempLabelCountArray =newdouble[testingData.classAttribute().numValues()];for(int i =0; i < numClassifiers; i++){int tempLabel = classifiers[i].classify(paraInstance);tempLabelCountArray[tempLabel]+= classifierWeights[i];}//Of for iint resultLabel =-1;double tempMax =-1;for(int i =0; i < tempLabelCountArray.length; i++){if(tempMax < tempLabelCountArray[i]){tempMax = tempLabelCountArray[i];resultLabel = i;}//Of if}//Of for ireturn resultLabel;}//Of classify/*** Test the booster on the training data.** @return The classification accuracy.*/publicdoubletest(){System.out.println("Testing on "+ testingData.numInstances()+" instances\r\n");returntest(testingData);}//Of test/*** Test the booster.** @param paraInstances The testing set.* @return The classification accuracy.*/publicdoubletest(Instances paraInstances){double tempCorrect =0;paraInstances.setClassIndex(paraInstances.numAttributes()-1);for(int i =0; i < paraInstances.numInstances(); i++){Instance tempInstance = paraInstances.instance(i);if(classify(tempInstance)==(int) tempInstance.classValue()){tempCorrect++;}//Of if}//Of for idouble resultAccuracy = tempCorrect / paraInstances.numInstances();System.out.println("The accuracy is: "+ resultAccuracy);return resultAccuracy;}//Of test/*** Compute the training accuracy of the booster. It is not weighted.** @return The training accuracy.*/publicdoublecomputeTrainingAccuracy(){double tempCorrect =0;for(int i =0; i < trainingData.numInstances(); i++){if(classify(trainingData.instance(i))==(int) trainingData.instance(i).classValue()){tempCorrect++;}//Of if}//Of for idouble tempAccuracy = tempCorrect / trainingData.numInstances();return tempAccuracy;}//Of computeTrainingAccuracy/*** The entrance of the program.** @param args Not used now.*/publicstaticvoidmain(String[] args){System.out.println("Starting Adaboosting ..");Booster tempBooster =newBooster("E:\\java_code\\data\\sampledata\\iris.arff");tempBooster.setNumBaseClassifiers(5);tempBooster.train();System.out.println("The training accuracy is: "+ tempBooster.computeTrainingAccuracy());tempBooster.test();}//Of main}//Of class Booster
一. 测试服务端 //测试服务端public function testServer(){$server new Server(192.168.0.144, 9501, SWOOLE_BASE, SWOOLE_SOCK_TCP);$server->on(request, function ($request, $response) {$response->header(Content-Type, text/plain);$response->end("He…
SELECTCOUNT(*) AS 数量,subquery.total_count AS 总数,COUNT(*) * 100 / subquery.total_count AS 百分比,CASEWHEN park_long < 900 THEN 15分钟以内WHEN park_long > 900 AND park_long < 3600 THEN 15-60分钟WHEN park_long > 3600 AND park_long < 10800 T…