weka.classifiers.functions.SMO Java Examples

The following examples show how to use weka.classifiers.functions.SMO. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExtrapolatedSaturationPointEvaluationTester.java    From AILibs with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testClassifierEvaluationAtSaturationPoint() throws Exception {
	// Load dataset from OpenML and create stratified split
	Instances dataset = null;
	OpenmlConnector client = new OpenmlConnector();
	DataSetDescription description = client.dataGet(42);
	File file = client.datasetGet(description);
	DataSource source = new DataSource(file.getCanonicalPath());
	dataset = source.getDataSet();
	dataset.setClassIndex(dataset.numAttributes() - 1);
	Attribute targetAttribute = dataset.attribute(description.getDefault_target_attribute());
	dataset.setClassIndex(targetAttribute.index());
	this.createSplit(new WekaInstances(dataset), 0.8, 123l);

	// Test classifier evaluation at saturation point
	ExtrapolatedSaturationPointEvaluator evaluator = new ExtrapolatedSaturationPointEvaluator(new int[] { 8, 16, 64, 128 }, new SystematicSamplingFactory<>(), this.train, 0.7,
			new InversePowerLawExtrapolationMethod(), 123l, this.test, EClassificationPerformanceMeasure.ERRORRATE);
	evaluator.setEpsilon(0.0005d);
	double evaluationResult = evaluator.evaluate(new WekaClassifier(new SMO()));
	Assert.assertTrue(evaluationResult > 0 && evaluationResult <= 100);
}
 
Example #2
Source File: LearningCurveExtrapolationEvaluationTester.java    From AILibs with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testClassifierEvaluationWithLearningCurveExtrapolation() throws Exception {
	// Load dataset from OpenML
	Instances dataset = null;
	OpenmlConnector client = new OpenmlConnector();
	DataSetDescription description = client.dataGet(42);
	File file = client.datasetGet(description);
	DataSource source = new DataSource(file.getCanonicalPath());
	dataset = source.getDataSet();
	dataset.setClassIndex(dataset.numAttributes() - 1);
	Attribute targetAttribute = dataset.attribute(description.getDefault_target_attribute());
	dataset.setClassIndex(targetAttribute.index());

	// Test classifier evaluation by learning curve extrapolation
	LearningCurveExtrapolationEvaluator evaluator = new LearningCurveExtrapolationEvaluator(new int[] { 8, 16, 64, 128 }, new SystematicSamplingFactory<>(), new WekaInstances(dataset),
			0.8d, new InversePowerLawExtrapolationMethod(), 123l);
	double evaluationResult = evaluator.evaluate(new WekaClassifier(new SMO()));
	Assert.assertTrue(evaluationResult > 0 && evaluationResult <= 100);
}
 
Example #3
Source File: WekaUtil.java    From AILibs with GNU Affero General Public License v3.0 6 votes vote down vote up
public static String printNestedWekaClassifier(final Classifier c) {
	StringBuilder sb = new StringBuilder();
	sb.append(c.getClass().getName());
	sb.append("(");

	if (c instanceof SingleClassifierEnhancer) {
		sb.append(printNestedWekaClassifier(((SingleClassifierEnhancer) c).getClassifier()));
	} else if (c instanceof SMO) {
		sb.append(((SMO) c).getKernel().getClass().getName());
	} else if (c instanceof MultipleClassifiersCombiner) {
		sb.append(printNestedWekaClassifier(((MultipleClassifiersCombiner) c).getClassifier(0)));
	}

	sb.append(")");

	return sb.toString();
}
 
Example #4
Source File: LLGC.java    From collective-classification-weka-package with GNU General Public License v3.0 6 votes vote down vote up
/**
 * performs initialization of members
 */
@Override
protected void initializeMembers() {
  super.initializeMembers();
  
  m_TrainsetNew          = null;
  m_TestsetNew           = null;
  m_Alpha                = 0.99;
  m_Sigma                = 1.0;
  m_Repeats              = 0;
  m_SequenceLimit        = SEQ_LIMIT_GRAPHKERNEL;
  m_filterType           = SMO.FILTER_NORMALIZE;
  m_IncludeNumAttributes = true;
  m_MatrixY              = null;
  m_MatrixW              = null;
  m_MatrixD              = null;
  m_MatrixS              = null;
  m_MatrixFStar          = null;
  m_Data                 = null;
  m_DistanceFunction     = new EuclideanDistance();
}
 
Example #5
Source File: StatUtils.java    From meka with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Main - do some tests.
 */
public static void main(String args[]) throws Exception {
	Instances D = Evaluation.loadDataset(args);
	MLUtils.prepareData(D);
	int L = D.classIndex();

	double CD[][] = null;

	if (args[2].equals("L")) {
		String I = "I";
		if (args.length >= 3) 
			I = args[3];
		CD = StatUtils.LEAD(D, new SMO(), new Random(), I);
	}
	else {
		CD = StatUtils.margDepMatrix(D,args[2]);
	}
	System.out.println(MatrixUtils.toString(CD, "M" + args[2]));
}
 
Example #6
Source File: SVMAttributeEval.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns the capabilities of this evaluator.
 *
 * @return            the capabilities of this evaluator
 * @see               Capabilities
 */
public Capabilities getCapabilities() {
  Capabilities        result;
  
  result = new SMO().getCapabilities();
  
  result.setOwner(this);
  
  // only binary attributes are allowed, otherwise the NominalToBinary
  // filter inside SMO will increase the number of attributes which in turn
  // will lead to ArrayIndexOutOfBounds-Exceptions.
  result.disable(Capability.NOMINAL_ATTRIBUTES);
  result.enable(Capability.BINARY_ATTRIBUTES);
  result.disableAllAttributeDependencies();
  
  return result;
}
 
Example #7
Source File: TunedClassifier.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
public void setupTestTunedClassifier() {         
    //setup classifier. in this example, if we wanted to tune the kernel as well, 
    //we'd have to extend smo and override the setOptions to allow specific options
    //for kernal settings... see SMO.setOptions()
    SMO svm = new SMO();
    PolyKernel p=new PolyKernel();
    p.setExponent(2);
    svm.setKernel(p);
    this.classifier = new SMO();                
    
    //setup tuner, defaults to 10foldCV grid-search
    this.tuner = new Tuner(); 
    
    //setup para space 
    int size = 13;
    double[] cs = new double[size];
    for (int i = 0; i < cs.length; i++)
        cs[i] = Math.pow(10.0, (i-size/2));
    
    this.space = new ParameterSpace();
    this.space.addParameter("C", cs);
}
 
Example #8
Source File: DeepMethodsTests.java    From meka with GNU General Public License v3.0 6 votes vote down vote up
public void testDeepML() {
	System.out.println("Test Stacked Boltzmann Machines with an off-the-shelf multi-label classifier");
	DeepML dbn = new DeepML();

	MCC h = new MCC();
	SMO smo = new SMO();
	smo.setBuildCalibrationModels(true);
	h.setClassifier(smo);

	dbn.setClassifier(h);
	dbn.setE(100);
	dbn.setH(30);

	Result r = EvaluationTests.cvEvaluateClassifier(dbn);
	System.out.println("DeepML + MCC" + r.getMeasurement("Accuracy"));
	String s = (String)r.getMeasurement("Accuracy");
	assertTrue("DeepML+MCC Accuracy Correct", s.startsWith("0.53")); // Good enough 
}
 
Example #9
Source File: TunedSVM.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
public static void cheatOnMNIST(){
    Instances train=DatasetLoading.loadDataNullable("\\\\cmptscsvr.cmp.uea.ac.uk\\ueatsc\\Data\\LargeProblems\\MNIST\\MNIST_TRAIN");
    Instances test=DatasetLoading.loadDataNullable("\\\\cmptscsvr.cmp.uea.ac.uk\\ueatsc\\Data\\LargeProblems\\MNIST\\MNIST_TEST");
    SMO svm=new SMO();
    RBFKernel k=new RBFKernel();
    svm.setKernel(k);
    System.out.println("Data loaded ......");
    double a =ClassifierTools.singleTrainTestSplitAccuracy(svm, train, test);
    System.out.println("Default acc = "+a);
    int min=1;//These search values are used for all kernels with C. It is also used for Gamma in RBF, but not for the Polynomial exponent search
    int max=6;
    for(double c=min;c<=max;c++)
        for(double r=min;r<=max;r++){
                 svm.setC(Math.pow(2, c));
                 k.setGamma(Math.pow(2, r));
                 svm.setKernel(k);//Just in case ...
                a =ClassifierTools.singleTrainTestSplitAccuracy(svm, train, test);
                System.out.println("logC ="+c+" logGamma = "+r+" acc = "+a);
            }
    
}
 
Example #10
Source File: LPMethodsTests.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
public void testLC() {

		Result r = null;

		// Test LC
		LC lc = new LC();
		lc.setClassifier(new SMO());
		r = EvaluationTests.cvEvaluateClassifier(lc);
		String s = (String)r.getMeasurement("Accuracy");
		System.out.println("LC "+s);
		TestHelper.assertAlmostEquals("LC Accuracy Correct", "0.568 +/- 0.032", (String)s, 1);

		// Test PS (0,0) -- should be identical
		PS ps = new PS();
		ps.setClassifier(new SMO());
		r = EvaluationTests.cvEvaluateClassifier(ps);
		System.out.println("PS "+r.getMeasurement("Accuracy"));
		assertTrue("PS(0,0) Accuracy Identical to LC", s.equals(r.getMeasurement("Accuracy")));

		// Test PS (3,1) -- should be faster 
		ps.setP(3);
		ps.setN(1);

		r = EvaluationTests.cvEvaluateClassifier(ps);
		System.out.println("PS(3,1) "+r.getMeasurement("Accuracy"));
		TestHelper.assertAlmostEquals("PS(3,1) Accuracy Correct", "0.565 +/- 0.04", (String)r.getMeasurement("Accuracy"), 1);

		// Test EPS
		EnsembleML eps = new EnsembleML();
		eps.setClassifier(ps);
		r = EvaluationTests.cvEvaluateClassifier(eps);
		System.out.println("EPS "+r.getMeasurement("Accuracy"));
		TestHelper.assertAlmostEquals("EPS Accuracy Correct", "0.574 +/- 0.042", (String)r.getMeasurement("Accuracy"), 1);
	}
 
Example #11
Source File: MetaMethodsTests.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
public void testEBR() {

		// Test BR
		BR br = new BR();
		br.setClassifier(new SMO());
		Result r = EvaluationTests.cvEvaluateClassifier(br);
		assertTrue("BR Accuracy Correct", r.getMeasurement("Accuracy").equals("0.493 +/- 0.036") );

		// Test EBR
		EnsembleML ebr = new EnsembleML();
		ebr.setClassifier(br);
		Result Er = EvaluationTests.cvEvaluateClassifier(ebr);
		assertTrue("EBR Accuracy Correct", Er.getMeasurement("Accuracy").equals("0.557 +/- 0.04 ") );
	}
 
Example #12
Source File: EvaluationTests.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
public MultiLabelClassifier makeECC() {
	BaggingML h = new BaggingML();
	CC cc = new CC();
	cc.setClassifier(new SMO());
	h.setClassifier(cc);
	return h;
}
 
Example #13
Source File: MultivariateShapeletTransformClassifier.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
public void configureEnsemble(){
    ensemble.setWeightingScheme(new TrainAcc(4));
    ensemble.setVotingScheme(new MajorityConfidence());
    
    Classifier[] classifiers = new Classifier[3];
    String[] classifierNames = new String[3];
    
    SMO smo = new SMO();
    smo.turnChecksOff();
    smo.setBuildLogisticModels(true);
    PolyKernel kl = new PolyKernel();
    kl.setExponent(2);
    smo.setKernel(kl);
    if (seedClassifier)
        smo.setRandomSeed((int)seed);
    classifiers[0] = smo;
    classifierNames[0] = "SVMQ";

    RandomForest r=new RandomForest();
    r.setNumTrees(500);
    if(seedClassifier)
       r.setSeed((int)seed);            
    classifiers[1] = r;
    classifierNames[1] = "RandF";
        
        
    RotationForest rf=new RotationForest();
    rf.setNumIterations(100);
    if(seedClassifier)
       rf.setSeed((int)seed);
    classifiers[2] = rf;
    classifierNames[2] = "RotF";
    
    
   ensemble.setClassifiers(classifiers, classifierNames, null);        
    
}
 
Example #14
Source File: CCMethodsTests.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
public void testsCC() {

		// Test BR
		BR br = new BR();
		br.setClassifier(new SMO());
		Result r = EvaluationTests.cvEvaluateClassifier(br);
		assertEquals("BR Accuracy Correct", "0.493 +/- 0.036", r.getMeasurement("Accuracy"));

		// Test EBR
		EnsembleML ebr = new EnsembleML();
		ebr.setClassifier(br);
		Result Er = EvaluationTests.cvEvaluateClassifier(ebr);
		assertEquals("EBR Accuracy Correct", "0.557 +/- 0.04 ", Er.getMeasurement("Accuracy"));
	}
 
Example #15
Source File: CCMethodsTests.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
public void testMCC() {
	// Test MCC (with SMO -- -M)
	System.out.println("Test MCC");
	MCC h = new MCC();
	SMO smo = new SMO();
	smo.setBuildCalibrationModels(true);
	h.setClassifier(smo);
	Result r = EvaluationTests.cvEvaluateClassifier(h);
	assertEquals("MCC Accuracy Correct", "0.561 +/- 0.035", r.getMeasurement("Accuracy"));
}
 
Example #16
Source File: CCMethodsTests.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
public void testPMCC() {
	// Test MCC (with SMO -- -M)
	System.out.println("Test PMCC");
	PMCC h = new PMCC();
	h.setM(10);
	h.setChainIterations(50);
	h.setInferenceIterations(20);
	SMO smo = new SMO();
	smo.setBuildCalibrationModels(true);
	h.setClassifier(smo);
	Result r = EvaluationTests.cvEvaluateClassifier(h);
	assertEquals("PMCC Accuracy Correct", "0.594 +/- 0.029", r.getMeasurement("Accuracy"));
}
 
Example #17
Source File: CCMethodsTests.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
public void testPCC() {
	// Test PCC (with SMO -- -M)
	System.out.println("Test PCC");
	PCC h = new PCC();
	SMO smo = new SMO();
	smo.setBuildCalibrationModels(true);
	h.setClassifier(smo);
	Result r = EvaluationTests.cvEvaluateClassifier(h);
	assertEquals("PCC Accuracy Correct", "0.565 +/- 0.032", r.getMeasurement("Accuracy"));
}
 
Example #18
Source File: CCMethodsTests.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
public void testCT() {
	// Test CT (with SMO -- -M)
	System.out.println("Test CT");
	CT h = new CT();
	SMO smo = new SMO();
	smo.setBuildCalibrationModels(true);
	h.setClassifier(smo);
	h.setInferenceIterations(10);
	h.setChainIterations(10);
	Result r = EvaluationTests.cvEvaluateClassifier(h);
	//System.out.println("CT ACC: "+r.getMeasurement("Accuracy"));
	assertEquals("CT Accuracy Correct", "0.56  +/- 0.034", r.getMeasurement("Accuracy"));
}
 
Example #19
Source File: CCMethodsTests.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
public void testCDT() {
	// Test CDT (with SMO -- -M)
	System.out.println("Test CDT");
	CDT h = new CDT();
	SMO smo = new SMO();
	smo.setBuildCalibrationModels(true);
	h.setClassifier(smo);
	Result r = EvaluationTests.cvEvaluateClassifier(h);
	//System.out.println("CDT ACC: "+r.getMeasurement("Accuracy"));
	assertEquals("CDT Accuracy Correct", "0.519 +/- 0.039", r.getMeasurement("Accuracy") );
}
 
Example #20
Source File: EnsembleProvider.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Initializes the CAWPE ensemble model consisting of five classifiers (SMO,
 * KNN, J48, Logistic and MLP) using a majority voting strategy. The ensemble
 * uses Weka classifiers. It refers to "Heterogeneous ensemble of standard
 * classification algorithms" (HESCA) as described in Lines, Jason & Taylor,
 * Sarah & Bagnall, Anthony. (2018). Time Series Classification with HIVE-COTE:
 * The Hierarchical Vote Collective of Transformation-Based Ensembles. ACM
 * Transactions on Knowledge Discovery from Data. 12. 1-35. 10.1145/3182382.
 *
 * @param seed
 *            Seed used within the classifiers and the majority confidence
 *            voting scheme
 * @param numFolds
 *            Number of folds used within the determination of the classifier
 *            weights for the {@link MajorityConfidenceVote}
 * @return Returns an initialized (but untrained) ensemble model.
 * @throws Exception
 *             Thrown when the initialization has failed
 */
public static Classifier provideCAWPEEnsembleModel(final int seed, final int numFolds) throws Exception {
	Classifier[] classifiers = new Classifier[5];

	Vote voter = new MajorityConfidenceVote(numFolds, seed);

	SMO smo = new SMO();
	smo.turnChecksOff();
	smo.setBuildCalibrationModels(true);
	PolyKernel kl = new PolyKernel();
	kl.setExponent(1);
	smo.setKernel(kl);
	smo.setRandomSeed(seed);
	classifiers[0] = smo;

	IBk k = new IBk(100);
	k.setCrossValidate(true);
	EuclideanDistance ed = new EuclideanDistance();
	ed.setDontNormalize(true);
	k.getNearestNeighbourSearchAlgorithm().setDistanceFunction(ed);
	classifiers[1] = k;

	J48 c45 = new J48();
	c45.setSeed(seed);
	classifiers[2] = c45;

	classifiers[3] = new Logistic();

	classifiers[4] = new MultilayerPerceptron();

	voter.setClassifiers(classifiers);
	return voter;
}
 
Example #21
Source File: Tuner.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    int seed = 0;
    
    SMO svm = new SMO();
    PolyKernel p=new PolyKernel();
    p.setExponent(2);
    svm.setKernel(p);
    svm.setRandomSeed(seed);
    
    int size = 5;
    double[] cs = new double[size];
    for (int i = 0; i < cs.length; i++)
        cs[i] = Math.pow(10.0, (i-size/2));
    
    ParameterSpace space = new ParameterSpace();
    space.addParameter("C", cs);
    
    Tuner tuner = new Tuner();
    tuner.setPathToSaveParameters("C:/Temp/TunerTests/first/");
    tuner.setSeed(seed);
    
    String dataset = "hayes-roth";
    Instances all = DatasetLoading.loadDataNullable("Z:\\Data\\UCIDelgado\\"+dataset+"\\"+dataset+".arff");
    Instances[] data = InstanceTools.resampleInstances(all, seed, 0.5);
    
    System.out.println(tuner.tune(svm, data[0], space));
}
 
Example #22
Source File: SVMAttributeEval.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * The filtering mode to pass to SMO
 *
 * @param newType the new filtering mode
 */
public void setFilterType(SelectedTag newType) {
  
  if (newType.getTags() == SMO.TAGS_FILTER) {
    m_smoFilterType = newType.getSelectedTag().getID();
  }
}
 
Example #23
Source File: CAWPE.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Uses the 'basic UCI' set up:
 * Comps: SVML, MLP, NN, Logistic, C4.5
 * Weight: TrainAcc(4) (train accuracies to the power 4)
 * Vote: MajorityConfidence (summing probability distributions)
 */
public final void setupDefaultSettings_NoLogistic() {
    this.ensembleName = "CAWPE-NoLogistic";
    
    this.weightingScheme = new TrainAcc(4);
    this.votingScheme = new MajorityConfidence();
    
    CrossValidationEvaluator cv = new CrossValidationEvaluator(seed, false, false, false, false); 
    cv.setNumFolds(10);
    this.trainEstimator = cv; 
    
    Classifier[] classifiers = new Classifier[4];
    String[] classifierNames = new String[4];

    SMO smo = new SMO();
    smo.turnChecksOff();
    smo.setBuildLogisticModels(true);
    PolyKernel kl = new PolyKernel();
    kl.setExponent(1);
    smo.setKernel(kl);
    smo.setRandomSeed(seed);
    classifiers[0] = smo;
    classifierNames[0] = "SVML";

    kNN k=new kNN(100);
    k.setCrossValidate(true);
    k.normalise(false);
    k.setDistanceFunction(new EuclideanDistance());
    classifiers[1] = k;
    classifierNames[1] = "NN";

    classifiers[2] = new J48();
    classifierNames[2] = "C4.5";

    classifiers[3] = new MultilayerPerceptron();
    classifierNames[3] = "MLP";

    setClassifiers(classifiers, classifierNames, null);
}
 
Example #24
Source File: CAWPE.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
public final void setupAdvancedSettings() {
    this.ensembleName = "CAWPE-A";
    
    this.weightingScheme = new TrainAcc(4);
    this.votingScheme = new MajorityConfidence();
    
    CrossValidationEvaluator cv = new CrossValidationEvaluator(seed, false, false, false, false); 
    cv.setNumFolds(10);
    this.trainEstimator = cv; 

    Classifier[] classifiers = new Classifier[3];
    String[] classifierNames = new String[3];

    SMO smo = new SMO();
    smo.turnChecksOff();
    smo.setBuildLogisticModels(true);
    PolyKernel kl = new PolyKernel();
    kl.setExponent(2);
    smo.setKernel(kl);
    smo.setRandomSeed(seed);
    classifiers[0] = smo;
    classifierNames[0] = "SVMQ";
    RandomForest rf= new RandomForest();
    rf.setNumTrees(500);
    classifiers[1] = rf;
    classifierNames[1] = "RandF";
    RotationForest rotf=new RotationForest();
    rotf.setNumIterations(200);
    classifiers[2] = rotf;
    classifierNames[2] = "RotF";

    setClassifiers(classifiers, classifierNames, null);
}
 
Example #25
Source File: Main-SVG.java    From Java-for-Data-Science with MIT License 5 votes vote down vote up
public Main() {
    try {
        BufferedReader datafile;
        datafile = readDataFile("camping.txt");
        Instances data = new Instances(datafile);
        data.setClassIndex(data.numAttributes() - 1);

        Instances trainingData = new Instances(data, 0, 14);
        Instances testingData = new Instances(data, 14, 5);
        Evaluation evaluation = new Evaluation(trainingData);

        SMO smo = new SMO();
        smo.buildClassifier(data);

        evaluation.evaluateModel(smo, testingData);
        System.out.println(evaluation.toSummaryString());

        // Test instance 
        Instance instance = new DenseInstance(3);
        instance.setValue(data.attribute("age"), 78);
        instance.setValue(data.attribute("income"), 125700);
        instance.setValue(data.attribute("camps"), 1);            
        instance.setDataset(data);
        System.out.println("The instance: " + instance);
        System.out.println(smo.classifyInstance(instance));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
 
Example #26
Source File: CNode.java    From meka with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Main - run some tests.
 */
public static void main(String args[]) throws Exception {
	Instances D = new Instances(new FileReader(args[0]));
	Instance x = D.lastInstance();
	D.remove(D.numInstances()-1);
	int L = Integer.parseInt(args[1]);
	D.setClassIndex(L);
	double y[] = new double[L];
	Random r = new Random();
	int s[] = new int[]{1,0,2};
	int PA_J[][] = new int[][]{
		{},{},{0,1},
	};

	//MLUtils.randomize(s,r);
	// MUST GO IN TREE ORDER !!
	for(int j : s) {
		int pa_j[] = PA_J[j];
		System.out.println("PARENTS = "+Arrays.toString(pa_j));
		//MLUtils.randomize(pa_j,r);
		System.out.println("**** TRAINING ***");
		CNode n = new CNode(j,null,pa_j);
		n.build(D,new SMO());
		/*
		 */
		//Instances D_ = n.transform(D);
		//n.T = D_;
		System.out.println("============== D_"+j+" / class = "+n.T.classIndex()+" =");
		System.out.println(""+n.T);
		System.out.println("**** TESTING ****");
		/*
		Instance x_ = MLUtils.setTemplate(x,(Instance)D_.firstInstance().copy(),D_);
		for(int pa : pa_j) {
			//System.out.println(""+map[pa]);
			x_.setValue(n.map[pa],y[pa]);
		}
		//x_.setDataset(T);
		x_.setClassMissing();
		 */
		//n.T = D_;
		Instance x_ = n.transform(x,y);
		System.out.println(""+x_);
		y[j] = 1;
	}
}
 
Example #27
Source File: CAWPE.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
     * Comps: NN, SVML, SVMQ, C4.5, NB,  RotF, RandF, BN,
     * Weight: TrainAcc
     * Vote: MajorityVote
     *
     * As used originally in ST_HESCA, COTE.
     * NOTE the original also contained Bayes Net (BN). We have removed it because the classifier crashes
     * unpredictably when discretising features (due to lack of variance in the feature, but not easily detected and
     * dealt with
     *
     */
    public final void setupOriginalHESCASettings() {
        this.ensembleName = "HESCA";
        
        this.weightingScheme = new TrainAcc();
        this.votingScheme = new MajorityVote();
        
        CrossValidationEvaluator cv = new CrossValidationEvaluator(seed, false, false, false, false); 
        cv.setNumFolds(10);
        this.trainEstimator = cv; 
        int numClassifiers=7;
        Classifier[] classifiers = new Classifier[numClassifiers];
        String[] classifierNames = new String[numClassifiers];

        kNN k=new kNN(100);
        k.setCrossValidate(true);
        k.normalise(false);
        k.setDistanceFunction(new EuclideanDistance());
        classifiers[0] = k;
        classifierNames[0] = "NN";

        classifiers[1] = new NaiveBayes();
        classifierNames[1] = "NB";

        classifiers[2] = new J48();
        classifierNames[2] = "C45";

        SMO svml = new SMO();
        svml.turnChecksOff();
        PolyKernel kl = new PolyKernel();
        kl.setExponent(1);
        svml.setKernel(kl);
        svml.setRandomSeed(seed);
        classifiers[3] = svml;
        classifierNames[3] = "SVML";

        SMO svmq =new SMO();
//Assumes no missing, all real valued and a discrete class variable
        svmq.turnChecksOff();
        PolyKernel kq = new PolyKernel();
        kq.setExponent(2);
        svmq.setKernel(kq);
        svmq.setRandomSeed(seed);
        classifiers[4] =svmq;
        classifierNames[4] = "SVMQ";

        RandomForest r=new RandomForest();
        r.setNumTrees(500);
        r.setSeed(seed);
        classifiers[5] = r;
        classifierNames[5] = "RandF";

        RotationForest rf=new RotationForest();
        rf.setNumIterations(50);
        rf.setSeed(seed);
        classifiers[6] = rf;
        classifierNames[6] = "RotF";

//        classifiers[7] = new BayesNet();
//        classifierNames[7] = "bayesNet";

        setClassifiers(classifiers, classifierNames, null);
    }
 
Example #28
Source File: CAWPE.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Uses the 'basic UCI' set up:
 * Comps: SVML, MLP, NN, Logistic, C4.5
 * Weight: TrainAcc(4) (train accuracies to the power 4)
 * Vote: MajorityConfidence (summing probability distributions)
 */
@Override //Abstract Ensemble 
public final void setupDefaultEnsembleSettings() {
    this.ensembleName = "CAWPE";
    
    this.weightingScheme = new TrainAcc(4);
    this.votingScheme = new MajorityConfidence();
    this.transform = null;
    
    CrossValidationEvaluator cv = new CrossValidationEvaluator(seed, false, false, false, false); 
    cv.setNumFolds(10);
    this.trainEstimator = cv; 

    Classifier[] classifiers = new Classifier[5];
    String[] classifierNames = new String[5];

    SMO smo = new SMO();
    smo.turnChecksOff();
    smo.setBuildLogisticModels(true);
    PolyKernel kl = new PolyKernel();
    kl.setExponent(1);
    smo.setKernel(kl);
    smo.setRandomSeed(seed);
    classifiers[0] = smo;
    classifierNames[0] = "SVML";

    kNN k=new kNN(100);
    k.setCrossValidate(true);
    k.normalise(false);
    k.setDistanceFunction(new EuclideanDistance());
    classifiers[1] = k;
    classifierNames[1] = "NN";

    classifiers[2] = new J48();
    classifierNames[2] = "C4.5";

    classifiers[3] = new Logistic();
    classifierNames[3] = "Logistic";

    classifiers[4] = new MultilayerPerceptron();
    classifierNames[4] = "MLP";
    
    setClassifiers(classifiers, classifierNames, null);
}
 
Example #29
Source File: TunedSVM.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
@Override
    public void buildClassifier(Instances train) throws Exception {
        res =new ClassifierResults();
        long t=System.currentTimeMillis();
//        if(kernelOptimise)
//            selectKernel(train);
        if(buildFromPartial){
            if(paraSpace1==null)
                setStandardParaSearchSpace();  
            if(kernel==KernelType.RBF)
                setRBFParasFromPartiallyCompleteSearch();            
//            else if(kernel==KernelType.LINEAR || kernel==KernelType.QUADRATIC)
//                setFixedPolynomialParasFromPartiallyCompleteSearch();            
                else if(kernel==KernelType.POLYNOMIAL)
                    setPolynomialParasFromPartiallyCompleteSearch();            
        }
        else if(tuneParameters){
            if(paraSpace1==null)
                setStandardParaSearchSpace();
            if(buildFromFile){
                throw new Exception("Build from file in TunedSVM Not implemented yet");
            }else{
                if(kernel==KernelType.RBF)
                    tuneRBF(train); //Tunes two parameters
                else if(kernel==KernelType.LINEAR || kernel==KernelType.QUADRATIC)
                    tuneCForFixedPolynomial(train);//Tunes one parameter
                else if(kernel==KernelType.POLYNOMIAL)
                    tunePolynomial(train);
            }
        }
/*If there is no parameter search, then there is no train CV available.        
this gives the option of finding one using 10xCV  
*/        
        else if(findTrainAcc){
            int folds=10;
            if(folds>train.numInstances())
                folds=train.numInstances();
            SMO model = new SMO();
            model.setKernel(this.m_kernel);
            model.setC(this.getC());
            model.setBuildLogisticModels(true);
            model.setRandomSeed(seed);
            CrossValidationEvaluator cv = new CrossValidationEvaluator();
            cv.setSeed(seed); //trying to mimick old seeding behaviour below
            cv.setNumFolds(folds);
            cv.buildFolds(train);
            res = cv.crossValidateWithStats(model, train);
      }        
        
        
        
//If both kernelOptimise and tuneParameters are false, it just builds and SVM        
//With whatever the parameters are set to        
        super.buildClassifier(train);
        
        if(saveEachParaAcc)
            res.setBuildTime(combinedBuildTime);
        else
            res.setBuildTime(System.currentTimeMillis()-t);
        if(trainPath!=null && trainPath!=""){  //Save basic train results
            res.setClassifierName("TunedSVM"+kernel);
            res.setDatasetName(train.relationName());
            res.setFoldID(seed);
            res.setSplit("train");
            
            res.setParas(getParameters());
            res.writeFullResultsToFile(trainPath);
            File x=new File(trainPath);
            x.setWritable(true, false);
            
        }        
    }
 
Example #30
Source File: SVMAttributeEval.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Get SVM-ranked attribute indexes (best to worst) selected for
 * the class attribute indexed by classInd (one-vs-all).
 */
private int[] rankBySVM(int classInd, Instances data) {
  // Holds a mapping into the original array of attribute indices
  int[] origIndices = new int[data.numAttributes()];
  for (int i = 0; i < origIndices.length; i++)
    origIndices[i] = i;
  
  // Count down of number of attributes remaining
  int numAttrLeft = data.numAttributes()-1;
  // Ranked attribute indices for this class, one vs.all (highest->lowest)
  int[] attRanks = new int[numAttrLeft];

  try {
    MakeIndicator filter = new MakeIndicator();
    filter.setAttributeIndex("" + (data.classIndex() + 1));
    filter.setNumeric(false);
    filter.setValueIndex(classInd);
    filter.setInputFormat(data);
    Instances trainCopy = Filter.useFilter(data, filter);
    double pctToElim = ((double) m_percentToEliminate) / 100.0;
    while (numAttrLeft > 0) {
      int numToElim;
      if (pctToElim > 0) {
        numToElim = (int) (trainCopy.numAttributes() * pctToElim);
        numToElim = (numToElim > 1) ? numToElim : 1;
        if (numAttrLeft - numToElim <= m_percentThreshold) {
          pctToElim = 0;
          numToElim = numAttrLeft - m_percentThreshold;
        }
      } else {
        numToElim = (numAttrLeft >= m_numToEliminate) ? m_numToEliminate : numAttrLeft;
      }
      
      // Build the linear SVM with default parameters
      SMO smo = new SMO();
                              
      // SMO seems to get stuck if data not normalised when few attributes remain
      // smo.setNormalizeData(numAttrLeft < 40);
      smo.setFilterType(new SelectedTag(m_smoFilterType, SMO.TAGS_FILTER));
      smo.setEpsilon(m_smoPParameter);
      smo.setToleranceParameter(m_smoTParameter);
      smo.setC(m_smoCParameter);
      smo.buildClassifier(trainCopy);
                              
      // Find the attribute with maximum weight^2
      double[] weightsSparse = smo.sparseWeights()[0][1];
      int[] indicesSparse = smo.sparseIndices()[0][1];
      double[] weights = new double[trainCopy.numAttributes()];
      for (int j = 0; j < weightsSparse.length; j++) {
        weights[indicesSparse[j]] = weightsSparse[j] * weightsSparse[j];
      }
      weights[trainCopy.classIndex()] = Double.MAX_VALUE;
      int minWeightIndex;
      int[] featArray = new int[numToElim];
      boolean[] eliminated = new boolean[origIndices.length];
      for (int j = 0; j < numToElim; j++) {
        minWeightIndex = Utils.minIndex(weights);
        attRanks[--numAttrLeft] = origIndices[minWeightIndex];
        featArray[j] = minWeightIndex;
        eliminated[minWeightIndex] = true;
        weights[minWeightIndex] = Double.MAX_VALUE;
      }
                              
      // Delete the worst attributes. 
      weka.filters.unsupervised.attribute.Remove delTransform =
        new weka.filters.unsupervised.attribute.Remove();
      delTransform.setInvertSelection(false);
      delTransform.setAttributeIndicesArray(featArray);
      delTransform.setInputFormat(trainCopy);
      trainCopy = Filter.useFilter(trainCopy, delTransform);
                              
      // Update the array of remaining attribute indices
      int[] temp = new int[origIndices.length - numToElim];
      int k = 0;
      for (int j = 0; j < origIndices.length; j++) {
        if (!eliminated[j]) {
          temp[k++] = origIndices[j];
        }
      }
      origIndices = temp;
    }                 
    // Carefully handle all exceptions
  } catch (Exception e) {
    e.printStackTrace();                      
  }
  return attRanks;
}