Executing Weka Classification in C# in Parallel

廉价感情. 提交于 2019-12-12 00:16:20

问题


I have asked a few broad questions about the operations of Weka and C# as well as WekaSharp, so I thought I would try to ask a more focused question to try to progress further on my own. As an example given from the weka site on executing weka from C# I was using I would like to run part of the calculation using parallel operations but am not sure how to code it here is the raw code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using weka.classifiers.meta;
using weka.classifiers.functions;
using weka.core;
using java.io;
using weka.clusterers;
using System.Diagnostics;
using System.Threading;

// From http://weka.wikispaces.com/IKVM+with+Weka+tutorial

class MainClass
{
    public static void Main(string[] args)
    {
        System.Console.WriteLine("J48 in C#");
        classifyTest();
    }

    const int percentSplit = 66;
    public static void classifyTest()
    {
        try
        {
            weka.core.Instances insts = new weka.core.Instances(new java.io.FileReader(@"C:\Users\Deines\Documents\School\Software\WekaSharp2012\data\iris.arff"));
            insts.setClassIndex(insts.numAttributes() - 1);

            weka.classifiers.Classifier cl = new weka.classifiers.trees.J48();
            System.Console.WriteLine("Performing " + percentSplit + "% split evaluation.");

            //randomize the order of the instances in the dataset.
            weka.filters.Filter myRandom = new weka.filters.unsupervised.instance.Randomize();
            myRandom.setInputFormat(insts);
            insts = weka.filters.Filter.useFilter(insts, myRandom);

            int trainSize = insts.numInstances() * percentSplit / 100;
            int testSize = insts.numInstances() - trainSize;
            weka.core.Instances train = new weka.core.Instances(insts, 0, trainSize);

            cl.buildClassifier(train);
            int numCorrect = 0;
            for (int i = trainSize; i < insts.numInstances(); i++)
            {
                weka.core.Instance currentInst = insts.instance(i);
                double predictedClass = cl.classifyInstance(currentInst);
                if (predictedClass == insts.instance(i).classValue())
                    numCorrect++;
            }
            System.Console.WriteLine(numCorrect + " out of " + testSize + " correct (" +
                       (double)((double)numCorrect / (double)testSize * 100.0) + "%)");
        }
        catch (java.lang.Exception ex)
        {
            ex.printStackTrace();
        }
    }

}

I would like to run :

        for (int i = trainSize; i < insts.numInstances(); i++)
        {
            weka.core.Instance currentInst = insts.instance(i);
            double predictedClass = cl.classifyInstance(currentInst);
            if (predictedClass == insts.instance(i).classValue())
                numCorrect++;
        }

both sequentially and with concurrency in order to compaire the rates. I know the command is System.Linq.ParallelExecutionMode() , but I am not sure how to apply it in this case. Thank you very much.


回答1:


Why not System.Threading.Tasks.Parallel.For instead?

Parallel.For(trainSize, inst.numInstances(), i => 
{
    weka.core.Instance currentInst = insts.instance(i);
    double predictedClass = cl.classifyInstance(currentInst);
    if (predictedClass == insts.instance(i).classValue())
        Interlocked.Increment(ref numCorrect);
});

Please not I didn't execute this code so you may need to add some synchronization code (monitors or locks) to access some shared data.



来源:https://stackoverflow.com/questions/20339725/executing-weka-classification-in-c-sharp-in-parallel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!