/** * performs initialization of members */ @Override protected void initializeMembers() { super.initializeMembers(); m_Classifier = new weka.classifiers.trees.J48(); m_NNSearch = new KDTree(); m_WeightingFactor = 1.0; m_NoWeights = false; m_KNN = 10; m_KNNDetermined = 10; m_UseCV = false; m_Data = null; m_DataIsLabeled = false; m_TestsetNew = null; m_ClearCutDistribution = 0; m_WeightFlips = 0; m_AdditionalMeasures.add("measureClearCutDistribution"); m_AdditionalMeasures.add("measureWeightFlips"); }
/** * Assigns instances to centers. * * @param tree KDTree on all instances * @param centers all the input centers * @param instOfCent the instances to each center * @param allInstList list of all instances * @param assignments assignments of instances to centers * @param iterationCount the number of iteration * @return true if converged * @throws Exception is something goes wrong */ protected boolean assignToCenters(KDTree tree, Instances centers, int[][] instOfCent, int[] allInstList, int[] assignments, int iterationCount) throws Exception { boolean converged = true; if (tree != null) { // using KDTree structure for assigning converged = assignToCenters(tree, centers, instOfCent, assignments, iterationCount); } else { converged = assignToCenters(centers, instOfCent, allInstList, assignments); } return converged; }
/** * Constructor. * @param dataset WEKA Instances object. * @throws Exception */ public LocallyWeightedLinearRegression(Instances dataset) throws Exception { // set the method for local regression lwl.setClassifier(new LinearRegression()); // set number of nearest neighbours to be used for local prediction lwl.setKNN(10); // 10 by default // set weighting kernel method (see comments on constants) lwl.setWeightingKernel(LINEAR); // set KDTree as nearest neighbour search method lwl.setNearestNeighbourSearchAlgorithm(new KDTree()); // build the classifier lwl.buildClassifier(dataset); // store instance reference this.dataset = dataset; }
/** * Gets the KDTree specification string, which contains the class name of * the KDTree class and any options to the KDTree. * * @return the KDTree string. */ protected String getKDTreeSpec() { KDTree c = getKDTree(); if (c instanceof OptionHandler) { return c.getClass().getName() + " " + Utils.joinOptions(((OptionHandler)c).getOptions()); } return c.getClass().getName(); }
/** * Assign instances to centers using KDtree. * First part of conventionell K-Means, returns true if new assignment * is the same as the last one. * * @param kdtree KDTree on all instances * @param centers all the input centers * @param instOfCent the instances to each center * @param assignments assignments of instances to centers * @param iterationCount the number of iteration * @return true if converged * @throws Exception in case instances are not assigned to cluster */ protected boolean assignToCenters(KDTree kdtree, Instances centers, int[][] instOfCent, int[] assignments, int iterationCount) throws Exception { int numCent = centers.numInstances(); int numInst = m_Instances.numInstances(); int[] oldAssignments = new int[numInst]; // WARNING: assignments is "input/output-parameter" // should not be null if (assignments == null) { assignments = new int[numInst]; for (int i = 0; i < numInst; i++) { assignments[0] = -1; } } // WARNING: instOfCent is "input/output-parameter" // should not be null if (instOfCent == null) { instOfCent = new int [numCent][]; } // save old assignments for (int i = 0; i < assignments.length; i++) { oldAssignments[i] = assignments[i]; } // use tree to get new assignments kdtree.centerInstances(centers, assignments, Math.pow(.8, iterationCount)); boolean converged = true; // compare with previous assignment for (int i = 0; converged && (i < assignments.length); i++) { converged = (oldAssignments[i] == assignments[i]); if (assignments[i] == -1) throw new Exception("Instance " + i + " has not been assigned to cluster."); } if (!converged) { int[] numInstOfCent = new int[numCent]; for (int i = 0; i < numCent; i++) numInstOfCent[i] = 0; // count num of assignments per center for (int i = 0; i < numInst; i++) numInstOfCent[assignments[i]]++; // prepare instancelists per center for (int i = 0; i < numCent; i++){ instOfCent[i] = new int[numInstOfCent[i]]; } // write instance lists per center for (int i = 0; i < numCent; i++) { int index = -1; for (int j = 0; j < numInstOfCent[i]; j++) { index = nextAssignedOne(i, index, assignments); instOfCent[i][j] = index; } } } return converged; }
/** * Sets the KDTree class. * @param k a KDTree object with all options set */ public void setKDTree(KDTree k) { m_KDTree = k; }
/** * Gets the KDTree class. * * @return the configured KDTree */ public KDTree getKDTree() { return m_KDTree; }