并行执行C#中的Weka分类 [英] Executing Weka Classification in C# in Parallel

查看:86
本文介绍了并行执行C#中的Weka分类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经问了一些有关Weka和C#以及WekaSharp的操作的广泛问题,所以我想我会尝试提出一个更集中的问题,以尝试自己进一步发展.作为一个从Weka站点给出的示例,我正在使用C#执行weka,我想使用并行操作来运行部分计算,但是不确定如何将其编码为原始代码:

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();
        }
    }

}

我想运行:

        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.Linq.ParallelExecutionMode(),但是我不确定在这种情况下如何应用它.非常感谢.

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.

推荐答案

为什么不是System.Threading.Tasks.Parallel.For呢?

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.

这篇关于并行执行C#中的Weka分类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆