mercredi 9 juin 2021

I have quesion about Design pattern in Java

I was trying to change design patter of KNN.java file, specially backwardsElimination and forwardElimination method.

But it was too complicated and variables and other methods were connected so I just could extract those methods to abstract class StepwiseVariableSelection.

This is the diagram: This is the diagram

I think it is not really strategy pattern. and I didn't even use 'private StepwiseVariableSelection strate;(in knn.java)' well and couldn't make instance ofcourse.

public class kNN implements Classifier{

// data set of training examples
private DataSet dataSet;
// minimum possible value of k
private final int kMin = 1;
// maximum possible value of k
private final int kMax = 15;
// cross-validated, optimized value of k
private int kOpt = 7;
// elimAttr[i] is true if attribute i has been eliminated
private boolean[] isEliminatedAttr;
// learning rate for weight training
private final double learningRate = 0.05;
// instanceWeights for training examples
private double[] instanceWeights;
// use 8 different sets for cross validation
private int numSets = 8;
private Strategy strategy;
private StepwiseVariableSelection strate;

} public class backwardsElimination extends StepwiseVariableSelection {

public void backwardsElimination() {
    
    double[][] distances = distanceSettingForBackward();
    
    int[][] orderedIndices = orderedIndices(distances);
    
    // calculate base error with no attribute elimination
    double baselineError = calcError(orderedIndices);
    
    distances = iterateEachAttribute(distances, orderedIndices, baselineError);
}
public backwardsElimination(DataSet dataSet, Strategy strategy, boolean[] isEliminatedAttr,
        double[] instanceWeights) {
    this.dataSet = dataSet;
    this.strategy = strategy;
    this.isEliminatedAttr = isEliminatedAttr;
    this.instanceWeights = instanceWeights;
}

}

public class forwardsSelection extends StepwiseVariableSelection {

public void forwardsSelection() {
    removeAllAttributes();
    
    double[][] distances = setCrossValidationDistance();
    int[][] orderedIndices = orderedIndices(distances);
    
    // calculate base error with no attribute elimination
    double baselineError = calcError(orderedIndices);
    boolean attributeAdded;
    
    do {
        attributeAdded = false;
        double minError = Double.POSITIVE_INFINITY;
        int minErrorIndex = -1;

}

How can I use strategy pattern here well? or other design pattern for the two method?

Aucun commentaire:

Enregistrer un commentaire