public void test_binarisation_in_instances_builder() { TestingRow[] rows = new[] { new TestingRow { CLASS = 1, ATT_1 = 111, ATT_2 = 222, ATT_3 = 333, ATT_4 = 444, ATT_5 = 555, ATT_6 = 666, ATT_7 = 777, ATT_8 = 888 }, new TestingRow { CLASS = 0, ATT_1 = 1112, ATT_2 = 2222, ATT_3 = 3332, ATT_4 = 4442, ATT_5 = 5552, ATT_6 = 6662, ATT_7 = 7772, ATT_8 = 8882 } }; weka.core.Instances instances = new InstancesBuilder <TestingRow>(rows, 0).Build(); Assert.AreEqual(25, instances.numAttributes()); weka.core.Instance i1 = instances.instance(0); weka.core.Instance i2 = instances.instance(1); // These are the standard properties Assert.AreEqual(1.0, i1.value(0)); Assert.AreEqual(1.0, i1.classValue()); Assert.AreEqual(0.0, i2.value(0)); Assert.AreEqual(0.0, i2.classValue()); Assert.AreEqual(0.0, i1.value(1)); Assert.AreEqual("111", i1.stringValue(1)); Assert.AreEqual(1.0, i2.value(1)); Assert.AreEqual("1112", i2.stringValue(1)); Assert.AreEqual(0.0, i1.value(2)); Assert.AreEqual("222", i1.stringValue(2)); Assert.AreEqual(1.0, i2.value(2)); Assert.AreEqual("2222", i2.stringValue(2)); Assert.AreEqual(0.0, i1.value(3)); Assert.AreEqual("333", i1.stringValue(3)); Assert.AreEqual(1.0, i2.value(3)); Assert.AreEqual("3332", i2.stringValue(3)); Assert.AreEqual(0.0, i1.value(4)); Assert.AreEqual("444", i1.stringValue(4)); Assert.AreEqual(1.0, i2.value(4)); Assert.AreEqual("4442", i2.stringValue(4)); Assert.AreEqual(0.0, i1.value(5)); Assert.AreEqual("555", i1.stringValue(5)); Assert.AreEqual(1.0, i2.value(5)); Assert.AreEqual("5552", i2.stringValue(5)); Assert.AreEqual(0.0, i1.value(6)); Assert.AreEqual("666", i1.stringValue(6)); Assert.AreEqual(1.0, i2.value(6)); Assert.AreEqual("6662", i2.stringValue(6)); Assert.AreEqual(0.0, i1.value(7)); Assert.AreEqual("777", i1.stringValue(7)); Assert.AreEqual(1.0, i2.value(7)); Assert.AreEqual("7772", i2.stringValue(7)); Assert.AreEqual(0.0, i1.value(8)); Assert.AreEqual("888", i1.stringValue(8)); Assert.AreEqual(1.0, i2.value(8)); Assert.AreEqual("8882", i2.stringValue(8)); // These are the new binarized for (int i = 9; i < 25; i += 2) { Assert.AreEqual(1.0, i1.value(i)); Assert.AreEqual(0.0, i2.value(i)); Assert.AreEqual(0.0, i1.value(i + 1)); Assert.AreEqual(1.0, i2.value(i + 1)); } }
public override double classifyInstance(weka.core.Instance instance) { if (m_instances.numInstances() == 0) { return(2); } if (m_instances.numAttributes() != instance.numAttributes()) { throw new AssertException("different attribute."); } int n = (instance.numAttributes() - 1) / 2; List <Tuple <int, int> > dist = new List <Tuple <int, int> >(); for (int i = 0; i < m_instances.numInstances(); ++i) { int d1 = 0, d2 = 0; weka.core.Instance instanceI = m_instances.instance(i); for (int j = 0; j < n; ++j) { //d += (int)((instanceI.value(j) - instance.value(j)) * (instanceI.value(j) - instance.value(j))); if (instanceI.value(j) != instance.value(j)) { if (instance.value(j) == 2 || instanceI.value(j) == 2) { d1++; } else { d1 += 4; } } } for (int j = n; j < 2 * n; ++j) { //d += (int)((instanceI.value(j) - instance.value(j)) * (instanceI.value(j) - instance.value(j))); if (instanceI.value(j) != instance.value(j)) { if (instance.value(j) == 2 || instanceI.value(j) == 2) { d2++; } else { d2 += 4; } } } int c = (int)instanceI.classValue(); //if (c == 0) //{ // if (d1 < n / 4 && d1 < d2) // { // dist.Add(new Tuple<int, int>(d1, c)); // } //} //else if (c == 1) //{ // if (d2 < n / 4 && d2 < d1) // { // dist.Add(new Tuple<int, int>(d2, c)); // } //} //else //{ // throw new AssertException(""); //} dist.Add(new Tuple <int, int>(d1 + d2, c)); } if (dist.Count == 0) { return(2); } dist.Sort(new Comparison <Tuple <int, int> >((x, y) => { return(x.Item1.CompareTo(y.Item1)); })); int sum = 0, count = 0; for (int i = 0; i < dist.Count; ++i) { if (dist[i].Item1 < n / 4 * 2 * 4) { if (dist[i].Item2 != 2 && dist[i].Item2 != 3) { sum += dist[i].Item2; count++; } else { } } else { break; } } if (count == 0) { return(2); } if (count < m_instances.numInstances() / 30) { return(2); } return((int)Math.Round((double)sum / count)); }
public void Test() { weka.core.Instances insts = new weka.core.Instances(new java.io.FileReader("D:\\android_analysis\\attributes.arff")); insts.setClassIndex(insts.numAttributes() - 1); weka.classifiers.Classifier cl = new weka.classifiers.trees.J48(); cl.buildClassifier(insts); weka.filters.Filter myRandom = new weka.filters.unsupervised.instance.Randomize(); myRandom.setInputFormat(insts); insts = weka.filters.Filter.useFilter(insts, myRandom); int trainSize = (int)(insts.numInstances() * 0.66); int testSize = insts.numInstances() - trainSize; weka.core.Instances train = new weka.core.Instances(insts, 0, trainSize); cl.buildClassifier(train); for (int i = trainSize; i < insts.numInstances(); i++) { weka.core.Instance currentInst = insts.instance(i); double predictedClass = cl.classifyInstance(currentInst); double[] distrs = cl.distributionForInstance(currentInst); string actual = insts.classAttribute().value((int)currentInst.classValue()); string predicted = insts.classAttribute().value((int)predictedClass); System.Console.WriteLine("ID: " + (i + 1) + ", " + actual + " --> " + predicted); } }
public override double classifyInstance(weka.core.Instance instance) { if (m_instances.numInstances() == 0) { return(2); } if (m_instances.numAttributes() != instance.numAttributes()) { throw new AssertException("different attribute."); } int n = instance.numAttributes(); List <Tuple <int, int> > dist = new List <Tuple <int, int> >(); for (int i = 0; i < m_instances.numInstances(); ++i) { int d1 = 0, d2 = 0; weka.core.Instance instanceI = m_instances.instance(i); for (int j = 0; j < n; ++j) { //d += (int)((instanceI.value(j) - instance.value(j)) * (instanceI.value(j) - instance.value(j))); if (instanceI.value(j) != instance.value(j)) { d1++; } if (instance.value(j) != 0) { d2++; } } int c = (int)instanceI.classValue(); dist.Add(new Tuple <int, int>(d1, c)); } if (dist.Count == 0) { return(2); } dist.Sort(new Comparison <Tuple <int, int> >((x, y) => { return(x.Item1.CompareTo(y.Item1)); })); int sum = 0, count = 0; for (int i = 0; i < dist.Count; ++i) { if (dist[i].Item1 < 4) { sum += dist[i].Item2; count++; } else { break; } } if (count == 0) { return(2); } if (count < m_instances.numInstances() / 70) { return(2); } return((int)Math.Round((double)sum / count)); }
protected override string instanceToLibsvm(weka.core.Instance inst) { //StringBuffer.__<clinit>(); //StringBuffer buffer = new StringBuffer(new StringBuilder().append("").append(inst.classValue()).toString()); StringBuffer buffer = new StringBuffer(new StringBuilder().append("").append(inst.classValue() - 1).toString()); for (int i = 0; i < inst.numAttributes(); i++) { if ((i != inst.classIndex()) && (inst.value(i) != 0f)) { buffer.append(new StringBuilder().append(" ").append((int)(i + 1)).append(":").append(inst.value(i)).toString()); } } return(buffer.toString()); }