⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 checkkernel.java

📁 代码是一个分类器的实现,其中使用了部分weka的源代码。可以将项目导入eclipse运行
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
   */  protected boolean[] correctBuildInitialisation(      boolean nominalPredictor,      boolean numericPredictor,       boolean stringPredictor,       boolean datePredictor,      boolean relationalPredictor,      boolean multiInstance,      int classType) {    boolean[] result = new boolean[2];        print("correct initialisation during buildKernel");    printAttributeSummary(        nominalPredictor, numericPredictor, stringPredictor, datePredictor, relationalPredictor, multiInstance, classType);    print("...");    int numTrain = getNumInstances(),     numClasses = 2, missingLevel = 0;    boolean predictorMissing = false, classMissing = false;        Instances train1 = null;    Instances train2 = null;    Kernel kernel = null;    KernelEvaluation evaluation1A = null;    KernelEvaluation evaluation1B = null;    KernelEvaluation evaluation2 = null;    int stage = 0;    try {            // Make two sets of train/test splits with different       // numbers of attributes      train1 = makeTestDataset(42, numTrain,                                nominalPredictor    ? getNumNominal()    : 0,                               numericPredictor    ? getNumNumeric()    : 0,                                stringPredictor     ? getNumString()     : 0,                                datePredictor       ? getNumDate()       : 0,                                relationalPredictor ? getNumRelational() : 0,                                numClasses,                                classType,                               multiInstance);      train2 = makeTestDataset(84, numTrain,                                nominalPredictor    ? getNumNominal() + 1    : 0,                               numericPredictor    ? getNumNumeric() + 1    : 0,                                stringPredictor     ? getNumString() + 1     : 0,                                datePredictor       ? getNumDate() + 1       : 0,                                relationalPredictor ? getNumRelational() + 1 : 0,                                numClasses,                                classType,                               multiInstance);      if (missingLevel > 0) {        addMissing(train1, missingLevel, predictorMissing, classMissing);        addMissing(train2, missingLevel, predictorMissing, classMissing);      }            kernel = Kernel.makeCopy(getKernel());      evaluation1A = new KernelEvaluation();      evaluation1B = new KernelEvaluation();      evaluation2 = new KernelEvaluation();    } catch (Exception ex) {      throw new Error("Error setting up for tests: " + ex.getMessage());    }    try {      stage = 0;      evaluation1A.evaluate(kernel, train1);            stage = 1;      evaluation2.evaluate(kernel, train2);            stage = 2;      evaluation1B.evaluate(kernel, train1);            stage = 3;      if (!evaluation1A.equals(evaluation1B)) {        if (m_Debug) {          println("\n=== Full report ===\n"              + evaluation1A.toSummaryString("\nFirst buildKernel()")                  + "\n\n");          println(              evaluation1B.toSummaryString("\nSecond buildKernel()")                  + "\n\n");        }        throw new Exception("Results differ between buildKernel calls");      }      println("yes");      result[0] = true;            if (false && m_Debug) {        println("\n=== Full report ===\n"            + evaluation1A.toSummaryString("\nFirst buildKernel()")                + "\n\n");        println(            evaluation1B.toSummaryString("\nSecond buildKernel()")                + "\n\n");      }    }     catch (Exception ex) {      println("no");      result[0] = false;            if (m_Debug) {        println("\n=== Full Report ===");        print("Problem during building");        switch (stage) {          case 0:            print(" of dataset 1");            break;          case 1:            print(" of dataset 2");            break;          case 2:            print(" of dataset 1 (2nd build)");            break;          case 3:            print(", comparing results from builds of dataset 1");            break;	          }        println(": " + ex.getMessage() + "\n");        println("here are the datasets:\n");        println("=== Train1 Dataset ===\n"            + train1.toString() + "\n");        println("=== Train2 Dataset ===\n"            + train2.toString() + "\n");      }    }        return result;  }    /**   * Checks basic missing value handling of the scheme. If the missing   * values cause an exception to be thrown by the scheme, this will be   * recorded.   *   * @param nominalPredictor if true use nominal predictor attributes   * @param numericPredictor if true use numeric predictor attributes   * @param stringPredictor if true use string predictor attributes   * @param datePredictor if true use date predictor attributes   * @param relationalPredictor if true use relational predictor attributes   * @param multiInstance whether multi-instance is needed   * @param classType the class type (NUMERIC, NOMINAL, etc.)   * @param predictorMissing true if the missing values may be in    * the predictors   * @param classMissing true if the missing values may be in the class   * @param missingLevel the percentage of missing values   * @return index 0 is true if the test was passed, index 1 is true if test    *         was acceptable   */  protected boolean[] canHandleMissing(      boolean nominalPredictor,      boolean numericPredictor,       boolean stringPredictor,       boolean datePredictor,      boolean relationalPredictor,      boolean multiInstance,      int classType,      boolean predictorMissing,      boolean classMissing,      int missingLevel) {        if (missingLevel == 100)      print("100% ");    print("missing");    if (predictorMissing) {      print(" predictor");      if (classMissing)        print(" and");    }    if (classMissing)      print(" class");    print(" values");    printAttributeSummary(        nominalPredictor, numericPredictor, stringPredictor, datePredictor, relationalPredictor, multiInstance, classType);    print("...");    FastVector accepts = new FastVector();    accepts.addElement("missing");    accepts.addElement("value");    accepts.addElement("train");    int numTrain = getNumInstances(), numClasses = 2;        return runBasicTest(nominalPredictor, numericPredictor, stringPredictor,         datePredictor, relationalPredictor,         multiInstance,        classType,         missingLevel, predictorMissing, classMissing,        numTrain, numClasses,         accepts);  }    /**   * Checks whether the kernel can handle instance weights.   * This test compares the kernel performance on two datasets   * that are identical except for the training weights. If the    * results change, then the kernel must be using the weights. It   * may be possible to get a false positive from this test if the    * weight changes aren't significant enough to induce a change   * in kernel performance (but the weights are chosen to minimize   * the likelihood of this).   *   * @param nominalPredictor if true use nominal predictor attributes   * @param numericPredictor if true use numeric predictor attributes   * @param stringPredictor if true use string predictor attributes   * @param datePredictor if true use date predictor attributes   * @param relationalPredictor if true use relational predictor attributes   * @param multiInstance whether multi-instance is needed   * @param classType the class type (NUMERIC, NOMINAL, etc.)   * @return index 0 true if the test was passed   */  protected boolean[] instanceWeights(      boolean nominalPredictor,      boolean numericPredictor,       boolean stringPredictor,       boolean datePredictor,      boolean relationalPredictor,      boolean multiInstance,      int classType) {        print("kernel uses instance weights");    printAttributeSummary(        nominalPredictor, numericPredictor, stringPredictor, datePredictor, relationalPredictor, multiInstance, classType);    print("...");    int numTrain = 2*getNumInstances(),     numClasses = 2, missingLevel = 0;    boolean predictorMissing = false, classMissing = false;        boolean[] result = new boolean[2];    Instances train = null;    Kernel[] kernels = null;    KernelEvaluation evaluationB = null;    KernelEvaluation evaluationI = null;    boolean evalFail = false;    try {      train = makeTestDataset(42, numTrain,                               nominalPredictor    ? getNumNominal() + 1 : 0,                              numericPredictor    ? getNumNumeric() + 1 : 0,                               stringPredictor     ? getNumString()      : 0,                               datePredictor       ? getNumDate()        : 0,                               relationalPredictor ? getNumRelational()  : 0,                               numClasses,                               classType,                              multiInstance);      if (missingLevel > 0)        addMissing(train, missingLevel, predictorMissing, classMissing);      kernels = Kernel.makeCopies(getKernel(), 2);      evaluationB = new KernelEvaluation();      evaluationI = new KernelEvaluation();      evaluationB.evaluate(kernels[0], train);    } catch (Exception ex) {      throw new Error("Error setting up for tests: " + ex.getMessage());    }    try {            // Now modify instance weights and re-built/test      for (int i = 0; i < train.numInstances(); i++) {        train.instance(i).setWeight(0);      }      Random random = new Random(1);      for (int i = 0; i < train.numInstances() / 2; i++) {        int inst = Math.abs(random.nextInt()) % train.numInstances();        int weight = Math.abs(random.nextInt()) % 10 + 1;        train.instance(inst).setWeight(weight);      }      evaluationI.evaluate(kernels[1], train);      if (evaluationB.equals(evaluationI)) {        //	println("no");        evalFail = true;        throw new Exception("evalFail");      }            println("yes");      result[0] = true;    } catch (Exception ex) {      println("no");      result[0] = false;            if (m_Debug) {        println("\n=== Full Report ===");                if (evalFail) {          println("Results don't differ between non-weighted and "              + "weighted instance models.");          println("Here are the results:\n");          println(evaluationB.toSummaryString("\nboth methods\n"));        } else {          print("Problem during building");          println(": " + ex.getMessage() + "\n");        }        println("Here is the dataset:\n");        println("=== Train Dataset ===\n"            + train.toString() + "\n");        println("=== Train Weights ===\n");        for (int i = 0; i < train.numInstances(); i++) {          println(" " + (i + 1)               + "    " + train.instance(i).weight());        }      }    }        return result;  }    /**   * Checks whether the scheme alters the training dataset during   * building. If the scheme needs to modify the data it should take    * a copy of the training data. Currently checks for changes to header    * structure, number of instances, order of instances, instance weights.   *   * @param nominalPredictor if true use nominal predictor attributes   * @param numericPredictor if true use numeric predictor attributes   * @param stringPredictor if true use string predictor attributes   * @param datePredictor if true use date predictor attributes   * @param relationalPredictor if true use relational predictor attributes   * @param multiInstance whether multi-instance is needed   * @param classType the class type (NUMERIC, NOMINAL, etc.)   * @param predictorMissing true if we know the kernel can handle   * (at least) moderate missing predictor values   * @param classMissing true if we know the kernel can handle   * (at least) moderate missing class values   * @return index 0 is true if the test was passed   */  protected boolean[] datasetIntegrity(      boolean nominalPredictor,      boolean numericPredictor,       boolean stringPredictor,       boolean datePredictor,      boolean relationalPredictor,      boolean multiInstance,      int classType,      boolean predictorMissing,      boolean classMissing) {        print("kernel doesn't alter original datasets");    printAttributeSummary(        nominalPredictor, numericPredictor, stringPredictor, datePredictor, relationalPredictor, multiInstance, classType);    print("...");    int numTrain = getNumInstances(),     numClasses = 2, missingLevel = 20;        boolean[] result = new boolean[2];    Instances train = null;    Kernel kernel = null;    try {      train = makeTestDataset(42, numTrain,                               nominalPredictor    ? getNumNominal()    : 0,                              numericPredictor    ? getNumNumeric()    : 0,                               stringPredictor     ? getNumString()     : 0,                               datePredictor       ? getNumDate()       : 0,                               relationalPredictor ? getNumRelational() : 0,                               numClasses,                               classType,                              multiInstance);      if (missingLevel > 0)        addMissing(train, missingLevel, predictorMissing, classMissing);      kernel = Kernel.makeCopies(getKernel(), 1)[0];    } catch (Exception ex) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -