/// <summary> /// Dummy attribute /// </summary> /// <param name="instances"></param> /// <returns></returns> public void NominalToBinary(weka.core.Instances instances) { weka.filters.Filter nominalToBinary = new weka.filters.unsupervised.attribute.NominalToBinary(); nominalToBinary.setInputFormat(instances); instances = weka.filters.Filter.useFilter(instances, nominalToBinary); this.Instance = instances; }
public static double SupportVectorMachineTest(weka.core.Instances insts) { try { //weka.core.Instances insts = new weka.core.Instances(new java.io.FileReader("iris.arff")); insts.setClassIndex(insts.numAttributes() - 1); SupportVectorMachine = new weka.classifiers.functions.SMO(); weka.filters.Filter myDummy = new weka.filters.unsupervised.attribute.NominalToBinary(); myDummy.setInputFormat(insts); insts = weka.filters.Filter.useFilter(insts, myDummy); weka.filters.Filter myNormalize = new weka.filters.unsupervised.instance.Normalize(); myNormalize.setInputFormat(insts); insts = weka.filters.Filter.useFilter(insts, myNormalize); 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); SupportVectorMachine.buildClassifier(train); int numCorrect = 0; for (int i = trainSize; i < insts.numInstances(); i++) { weka.core.Instance currentInst = insts.instance(i); double predictedClass = SupportVectorMachine.classifyInstance(currentInst); if (predictedClass == insts.instance(i).classValue()) { numCorrect++; } } return((double)numCorrect / (double)testSize * 100.0); } catch (java.lang.Exception ex) { ex.printStackTrace(); return(0); } }
private void button1_Click(object sender, EventArgs e) { weka.core.Instances insts = new weka.core.Instances(new java.io.FileReader(file)); double[] Data = new double[insts.numAttributes()]; for (int i = 0; i < list.Count; i++) { if (list[i].GetType() == typeof(TextBox)) { TextBox txt = (TextBox)list[i]; string value = txt.Text.Replace('.', ','); Data[i] = Convert.ToDouble(value); } else { ComboBox combobox = (ComboBox)list[i]; Data[i] = Convert.ToDouble(combobox.SelectedIndex); } } // Data[(insts.numAttributes() - 1] = 0; insts.setClassIndex(insts.numAttributes() - 1); Instance newInsts = new Instance(1.0, Data); insts.add(newInsts); string type = model.GetType().ToString(); if (type == "weka.classifiers.bayes.NaiveBayes") { weka.filters.Filter myDiscretize = new weka.filters.unsupervised.attribute.Discretize(); myDiscretize.setInputFormat(insts); insts = weka.filters.Filter.useFilter(insts, myDiscretize); } else if (type == "weka.classifiers.lazy.IBk") { weka.filters.Filter myDummy = new weka.filters.unsupervised.attribute.NominalToBinary(); myDummy.setInputFormat(insts); insts = weka.filters.Filter.useFilter(insts, myDummy); weka.filters.Filter myNormalize = new weka.filters.unsupervised.instance.Normalize(); myNormalize.setInputFormat(insts); insts = weka.filters.Filter.useFilter(insts, myNormalize); } double index = model.classifyInstance(insts.lastInstance()); string result = insts.attribute(insts.numAttributes() - 1).value(Convert.ToInt16(index)); MessageBox.Show(result); }
//Artificial NN public static double ArtificialNN(weka.core.Instances insts) { try { insts.setClassIndex(insts.numAttributes() - 1); Anncl = new weka.classifiers.functions.MultilayerPerceptron(); weka.filters.Filter myDummy = new weka.filters.unsupervised.attribute.NominalToBinary(); myDummy.setInputFormat(insts); insts = weka.filters.Filter.useFilter(insts, myDummy); weka.filters.Filter myNormalize = new weka.filters.unsupervised.instance.Normalize(); myNormalize.setInputFormat(insts); insts = weka.filters.Filter.useFilter(insts, myNormalize); 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); Anncl.buildClassifier(train); int numCorrect = 0; for (int i = trainSize; i < insts.numInstances(); i++) { weka.core.Instance currentInst = insts.instance(i); double predictedClass = Anncl.classifyInstance(currentInst); if (predictedClass == insts.instance(i).classValue()) { numCorrect++; } } return((double)numCorrect / (double)testSize * 100.0); } catch (java.lang.Exception ex) { ex.printStackTrace(); return(0); } }
private void btnDiscover_Click(object sender, EventArgs e) { string type = model.GetType().ToString(); bool flag = false; bool flag2 = false; //input kontrolleri if (nominal != null) { for (int i = 0; i < nominal.Length; i++) { if (nominal[i].SelectedIndex == -1) { flag = true; break; } } } if (numeric != null) { for (int i = 0; i < numeric.Length; i++) { if (String.IsNullOrEmpty(numeric[i].Text)) { flag2 = true; break; } } } if (numAtt == numeric.Length && flag2 == true) { MessageBox.Show("Please select value!", "Error Message!"); } else if (numAtt == nominal.Length && flag == true) { MessageBox.Show("Please select value!", "Error Message!"); } else if ((nominal.Length + numeric.Length) == numAtt && (flag == true || flag2 == true)) { MessageBox.Show("Please select value!", "Error Message!"); } else { weka.core.Instance newIns = new weka.core.Instance(numAtt + 1); newIns.setDataset(insts); int i1 = 0, i2 = 0; for (int i = 0; i < numAtt; i++) { //nominal if (typeAtt[i]) { newIns.setValue(i, nominal[i1].SelectedItem.ToString()); i1++; } //numeric else { newIns.setValue(i, double.Parse(numeric[i2].Text)); i2++; } } weka.core.Instances insts2 = new weka.core.Instances(insts); insts2.add(newIns); if (type == "weka.classifiers.bayes.NaiveBayes") { weka.filters.Filter myDiscretize = new weka.filters.unsupervised.attribute.Discretize(); myDiscretize.setInputFormat(insts2); insts2 = weka.filters.Filter.useFilter(insts2, myDiscretize); } else if (type == "weka.classifiers.functions.Logistic") { weka.filters.Filter myDummy = new weka.filters.unsupervised.attribute.NominalToBinary(); myDummy.setInputFormat(insts2); insts2 = weka.filters.Filter.useFilter(insts2, myDummy); weka.filters.Filter myNormalize = new weka.filters.unsupervised.instance.Normalize(); myNormalize.setInputFormat(insts2); insts2 = weka.filters.Filter.useFilter(insts2, myNormalize); } else if (type == "new weka.classifiers.lazy.IBk") { weka.filters.Filter myDummy = new weka.filters.unsupervised.attribute.NominalToBinary(); myDummy.setInputFormat(insts2); insts2 = weka.filters.Filter.useFilter(insts2, myDummy); weka.filters.Filter myNormalize = new weka.filters.unsupervised.instance.Normalize(); myNormalize.setInputFormat(insts2); insts2 = weka.filters.Filter.useFilter(insts2, myNormalize); } else if (type == "weka.classifiers.trees.J48") { } else if (type == "weka.classifiers.trees.RandomForest") { } else if (type == "weka.classifiers.trees.RandomTree") { } else if (type == "weka.classifiers.functions.MultilayerPerceptron") { weka.filters.Filter myDummy = new weka.filters.unsupervised.attribute.NominalToBinary(); myDummy.setInputFormat(insts2); insts2 = weka.filters.Filter.useFilter(insts2, myDummy); weka.filters.Filter myNormalize = new weka.filters.unsupervised.instance.Normalize(); myNormalize.setInputFormat(insts2); insts2 = weka.filters.Filter.useFilter(insts2, myNormalize); } else if (type == "weka.classifiers.functions.SMO") { weka.filters.Filter myDummy = new weka.filters.unsupervised.attribute.NominalToBinary(); myDummy.setInputFormat(insts2); insts2 = weka.filters.Filter.useFilter(insts2, myDummy); weka.filters.Filter myNormalize = new weka.filters.unsupervised.instance.Normalize(); myNormalize.setInputFormat(insts2); insts2 = weka.filters.Filter.useFilter(insts2, myNormalize); } double index = model.classifyInstance(insts2.lastInstance()); //Model okuma kısmı weka.classifiers.Classifier cls = (weka.classifiers.Classifier)weka.core.SerializationHelper.read("models/mdl.model"); lblResult2.Text = "Result= " + insts2.attribute(insts2.numAttributes() - 1).value(Convert.ToInt16(index)); } }
private void button1_Click(object sender, EventArgs e) { OpenFileDialog file = new OpenFileDialog(); if (file.ShowDialog() == DialogResult.OK) { string filename = file.FileName; string filee = Path.GetFileName(filename); bool attributeType; string attributeName = " "; int numAttributeValue = 0; string attributeValueName = " "; textBox1.Text = filee + " chosen succesfully!"; ///////Decision Tree weka.core.Instances insts = new weka.core.Instances(new java.io.FileReader(filename)); insts.setClassIndex(insts.numAttributes() - 1); //find nominal or numeric attributes and create dropbox or textbox int numofAttributes = insts.numAttributes() - 1; for (int i = 0; i < numofAttributes; i++) { attributeType = insts.attribute(i).isNumeric(); attributeName = insts.attribute(i).name(); dataGridView1.Rows.Add(attributeName); if (attributeType == true) { } else { numAttributeValue = insts.attribute(i).numValues(); string[] name = new string[numAttributeValue]; for (int j = 0; j < numAttributeValue; j++) { attributeValueName = insts.attribute(i).value(j); name[j] += attributeValueName; } DataGridViewComboBoxCell combo = new DataGridViewComboBoxCell(); combo.DataSource = name.ToList(); dataGridView1.Rows[i].Cells[1] = combo; } } cl = new weka.classifiers.trees.J48(); textBox2.Text = "Performing " + percentSplit + "% split evaluation."; //filling missing values weka.filters.Filter missingval = new weka.filters.unsupervised.attribute.ReplaceMissingValues(); missingval.setInputFormat(insts); insts = weka.filters.Filter.useFilter(insts, missingval); weka.filters.Filter myNormalized = new weka.filters.unsupervised.instance.Normalize(); myNormalized.setInputFormat(insts); insts = weka.filters.Filter.useFilter(insts, myNormalized); //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); string str = cl.toString(); 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++; } } textBox3.Text = numCorrect + " out of " + testSize + " correct (" + (double)((double)numCorrect / (double)testSize * 100.0) + "%)"; //////////Naive Bayes //dosya okuma weka.core.Instances insts2 = new weka.core.Instances(new java.io.FileReader(filename)); insts2.setClassIndex(insts2.numAttributes() - 1); //naive bayes cl2 = new weka.classifiers.bayes.NaiveBayes(); //filling missing values weka.filters.Filter missingval2 = new weka.filters.unsupervised.attribute.ReplaceMissingValues(); missingval2.setInputFormat(insts2); insts2 = weka.filters.Filter.useFilter(insts2, missingval2); //for naive bayes weka.filters.Filter discrete2 = new weka.filters.unsupervised.attribute.Discretize(); discrete2.setInputFormat(insts2); insts2 = weka.filters.Filter.useFilter(insts2, discrete2); //randomize the order of the instances in the dataset. -ortak weka.filters.Filter myRandom2 = new weka.filters.unsupervised.instance.Randomize(); myRandom2.setInputFormat(insts2); insts2 = weka.filters.Filter.useFilter(insts2, myRandom2); //ortak int trainSize2 = insts2.numInstances() * percentSplit / 100; int testSize2 = insts2.numInstances() - trainSize2; weka.core.Instances train2 = new weka.core.Instances(insts2, 0, trainSize2); cl2.buildClassifier(train2); string str2 = cl2.toString(); int numCorrect2 = 0; for (int i = trainSize2; i < insts2.numInstances(); i++) { weka.core.Instance currentInst2 = insts2.instance(i); double predictedClass2 = cl2.classifyInstance(currentInst2); if (predictedClass2 == insts2.instance(i).classValue()) { numCorrect2++; } } textBox4.Text = numCorrect2 + " out of " + testSize2 + " correct (" + (double)((double)numCorrect2 / (double)testSize2 * 100.0) + "%)"; /////////K-Nearest Neigbour //dosya okuma weka.core.Instances insts3 = new weka.core.Instances(new java.io.FileReader(filename)); insts3.setClassIndex(insts3.numAttributes() - 1); cl3 = new weka.classifiers.lazy.IBk(); //filling missing values weka.filters.Filter missingval3 = new weka.filters.unsupervised.attribute.ReplaceMissingValues(); missingval3.setInputFormat(insts3); insts3 = weka.filters.Filter.useFilter(insts3, missingval3); //Convert to dummy attribute knn,svm,neural network weka.filters.Filter dummy3 = new weka.filters.unsupervised.attribute.NominalToBinary(); dummy3.setInputFormat(insts3); insts3 = weka.filters.Filter.useFilter(insts3, dummy3); //normalize numeric attribute weka.filters.Filter myNormalized3 = new weka.filters.unsupervised.instance.Normalize(); myNormalized3.setInputFormat(insts3); insts3 = weka.filters.Filter.useFilter(insts3, myNormalized3); //randomize the order of the instances in the dataset. weka.filters.Filter myRandom3 = new weka.filters.unsupervised.instance.Randomize(); myRandom3.setInputFormat(insts3); insts3 = weka.filters.Filter.useFilter(insts3, myRandom3); int trainSize3 = insts3.numInstances() * percentSplit / 100; int testSize3 = insts3.numInstances() - trainSize3; weka.core.Instances train3 = new weka.core.Instances(insts3, 0, trainSize3); cl3.buildClassifier(train3); string str3 = cl3.toString(); int numCorrect3 = 0; for (int i = trainSize3; i < insts3.numInstances(); i++) { weka.core.Instance currentInst3 = insts3.instance(i); double predictedClass3 = cl3.classifyInstance(currentInst3); if (predictedClass3 == insts3.instance(i).classValue()) { numCorrect3++; } } textBox5.Text = numCorrect3 + " out of " + testSize3 + " correct (" + (double)((double)numCorrect3 / (double)testSize3 * 100.0) + "%)"; //////////Artificial neural network //dosya okuma weka.core.Instances insts4 = new weka.core.Instances(new java.io.FileReader(filename)); insts4.setClassIndex(insts4.numAttributes() - 1); cl4 = new weka.classifiers.functions.MultilayerPerceptron(); //filling missing values weka.filters.Filter missingval4 = new weka.filters.unsupervised.attribute.ReplaceMissingValues(); missingval4.setInputFormat(insts4); insts4 = weka.filters.Filter.useFilter(insts4, missingval4); //Convert to dummy attribute weka.filters.Filter dummy4 = new weka.filters.unsupervised.attribute.NominalToBinary(); dummy4.setInputFormat(insts4); insts4 = weka.filters.Filter.useFilter(insts4, dummy4); //normalize numeric attribute weka.filters.Filter myNormalized4 = new weka.filters.unsupervised.instance.Normalize(); myNormalized4.setInputFormat(insts4); insts4 = weka.filters.Filter.useFilter(insts4, myNormalized4); //randomize the order of the instances in the dataset. weka.filters.Filter myRandom4 = new weka.filters.unsupervised.instance.Randomize(); myRandom4.setInputFormat(insts4); insts4 = weka.filters.Filter.useFilter(insts4, myRandom4); int trainSize4 = insts4.numInstances() * percentSplit / 100; int testSize4 = insts4.numInstances() - trainSize4; weka.core.Instances train4 = new weka.core.Instances(insts4, 0, trainSize4); cl4.buildClassifier(train4); string str4 = cl4.toString(); int numCorrect4 = 0; for (int i = trainSize4; i < insts4.numInstances(); i++) { weka.core.Instance currentInst4 = insts4.instance(i); double predictedClass4 = cl4.classifyInstance(currentInst4); if (predictedClass4 == insts4.instance(i).classValue()) { numCorrect4++; } } textBox6.Text = numCorrect4 + " out of " + testSize4 + " correct (" + (double)((double)numCorrect4 / (double)testSize4 * 100.0) + "%)"; ///////Support Vector Machine // dosya okuma weka.core.Instances insts5 = new weka.core.Instances(new java.io.FileReader(filename)); insts5.setClassIndex(insts5.numAttributes() - 1); cl5 = new weka.classifiers.functions.SMO(); //filling missing values weka.filters.Filter missingval5 = new weka.filters.unsupervised.attribute.ReplaceMissingValues(); missingval5.setInputFormat(insts5); insts5 = weka.filters.Filter.useFilter(insts5, missingval5); //Convert to dummy attribute weka.filters.Filter dummy5 = new weka.filters.unsupervised.attribute.NominalToBinary(); dummy5.setInputFormat(insts5); insts5 = weka.filters.Filter.useFilter(insts5, dummy5); //normalize numeric attribute weka.filters.Filter myNormalized5 = new weka.filters.unsupervised.instance.Normalize(); myNormalized5.setInputFormat(insts5); insts5 = weka.filters.Filter.useFilter(insts5, myNormalized5); //randomize the order of the instances in the dataset. weka.filters.Filter myRandom5 = new weka.filters.unsupervised.instance.Randomize(); myRandom5.setInputFormat(insts5); insts5 = weka.filters.Filter.useFilter(insts5, myRandom5); int trainSize5 = insts5.numInstances() * percentSplit / 100; int testSize5 = insts5.numInstances() - trainSize5; weka.core.Instances train5 = new weka.core.Instances(insts5, 0, trainSize5); cl5.buildClassifier(train5); string str5 = cl5.toString(); int numCorrect5 = 0; for (int i = trainSize5; i < insts5.numInstances(); i++) { weka.core.Instance currentInst5 = insts5.instance(i); double predictedClass5 = cl5.classifyInstance(currentInst5); if (predictedClass5 == insts5.instance(i).classValue()) { numCorrect5++; } } textBox7.Text = numCorrect5 + " out of " + testSize5 + " correct (" + (double)((double)numCorrect5 / (double)testSize5 * 100.0) + "%)"; string result1 = textBox3.Text; string output1 = result1.Split('(', ')')[1]; output1 = output1.Remove(output1.Length - 1); double r1 = Convert.ToDouble(output1); string result2 = textBox4.Text; string output2 = result2.Split('(', ')')[1]; output2 = output2.Remove(output2.Length - 1); double r2 = Convert.ToDouble(output2); string result3 = textBox5.Text; string output3 = result3.Split('(', ')')[1]; output3 = output3.Remove(output3.Length - 1); double r3 = Convert.ToDouble(output3); string result4 = textBox6.Text; string output4 = result4.Split('(', ')')[1]; output4 = output4.Remove(output4.Length - 1); double r4 = Convert.ToDouble(output4); string result5 = textBox7.Text; string output5 = result5.Split('(', ')')[1]; output5 = output5.Remove(output5.Length - 1); double r5 = Convert.ToDouble(output5); double[] max_array = new double[] { r1, r2, r3, r4, r5 }; double max = max_array.Max(); if (r1 == max) { textBox8.Text = "Best Algoritm is Decision Tree Algorithm "; } else if (r2 == max) { textBox8.Text = "Best Algoritm is Naive Bayes Algorithm "; } else if (r3 == max) { textBox8.Text = "Best Algoritm is K-Nearest Neighbour Algorithm "; } else if (r4 == max) { textBox8.Text = "Best Algoritm is Artificial Neural Network Algorithm "; } else if (r5 == max) { textBox8.Text = "Best Algoritm is Support Vector Machine Algorithm "; } } }
public override void buildClassifier(Instances insts) { if (insts.checkForStringAttributes()) { throw new Exception("Cannot handle string attributes!"); } if (insts.numClasses() > 2) { throw new System.Exception("Can only handle two-class datasets!"); } if (insts.classAttribute().Numeric) { throw new Exception("Can't handle a numeric class!"); } // Filter data m_Train = new Instances(insts); m_Train.deleteWithMissingClass(); m_ReplaceMissingValues = new ReplaceMissingValues(); m_ReplaceMissingValues.setInputFormat(m_Train); m_Train = Filter.useFilter(m_Train, m_ReplaceMissingValues); m_NominalToBinary = new NominalToBinary(); m_NominalToBinary.setInputFormat(m_Train); m_Train = Filter.useFilter(m_Train, m_NominalToBinary); /** Randomize training data */ //UPGRADE_TODO: The differences in the expected value of parameters for constructor 'java.util.Random.Random' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'" m_Train.randomize(new System.Random((System.Int32) m_Seed)); /** Make space to store perceptrons */ m_Additions = new int[m_MaxK + 1]; m_IsAddition = new bool[m_MaxK + 1]; m_Weights = new int[m_MaxK + 1]; /** Compute perceptrons */ m_K = 0; for (int it = 0; it < m_NumIterations; it++) { for (int i = 0; i < m_Train.numInstances(); i++) { Instance inst = m_Train.instance(i); if (!inst.classIsMissing()) { int prediction = makePrediction(m_K, inst); //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" int classValue = (int) inst.classValue(); if (prediction == classValue) { m_Weights[m_K]++; } else { m_IsAddition[m_K] = (classValue == 1); m_Additions[m_K] = i; m_K++; m_Weights[m_K]++; } if (m_K == m_MaxK) { //UPGRADE_NOTE: Labeled break statement was changed to a goto statement. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1012'" goto out_brk; } } } } //UPGRADE_NOTE: Label 'out_brk' was added. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1011'" out_brk: ; }
private void result_Click(object sender, EventArgs e) { ArrayList algorithms = new ArrayList(); algorithms.Add("Naive Bayes"); algorithms.Add("K Nearest Neighbor"); algorithms.Add("Decision Tree"); algorithms.Add("Neural Network"); algorithms.Add("Support Vector Machine"); ArrayList successPercent = new ArrayList(); double res_Naive, res_KNN, res_NN, res_Tree, res_SVM = 0.0; string nameOfAlgo = ""; //NAIVE BAYES ALGORITHM weka.core.Instances insts = new weka.core.Instances(new java.io.FileReader(fileDirectory)); //CREATIING DYNAMIC GRIDVIEW FOR ADDING NEW INSTANCE dataGridView1.ColumnCount = 2; dataGridView1.RowCount = insts.numAttributes(); String[,] matrixOfInstances = new String[insts.numInstances(), insts.numAttributes()]; for (int y = 0; y < insts.numAttributes() - 1; y++) { dataGridView1.Rows[y].Cells[0].Value = insts.attribute(y).name(); if (insts.attribute(y).isNominal()) { //nominalDataValues.Add(insts.attribute(y).toString()); string phrase = insts.attribute(y).toString(); string[] first = phrase.Split('{'); string[] second = first[1].Split('}'); string[] attributeValues = second[0].Split(','); DataGridViewComboBoxCell comboColumn = new DataGridViewComboBoxCell(); foreach (var a in attributeValues) { comboColumn.Items.Add(a); } dataGridView1.Rows[y].Cells[1] = comboColumn; } } insts.setClassIndex(insts.numAttributes() - 1); cl_Naive = new weka.classifiers.bayes.NaiveBayes(); weka.filters.Filter myNominalData = new weka.filters.unsupervised.attribute.Discretize(); myNominalData.setInputFormat(insts); insts = weka.filters.Filter.useFilter(insts, myNominalData); //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_Naive.buildClassifier(train); string str = cl_Naive.toString(); int numCorrect = 0; for (int i = trainSize; i < insts.numInstances(); i++) { weka.core.Instance currentInst = insts.instance(i); double predictedClass = cl_Naive.classifyInstance(currentInst); if (predictedClass == insts.instance(i).classValue()) { numCorrect++; } } res_Naive = (double)((double)numCorrect / (double)testSize * 100.0); successPercent.Add(res_Naive); //kNN weka.core.Instances insts2 = new weka.core.Instances(new java.io.FileReader(fileDirectory)); insts2.setClassIndex(insts2.numAttributes() - 1); cl_Knn = new weka.classifiers.lazy.IBk(); //Nominal to Binary weka.filters.Filter myBinaryData = new weka.filters.unsupervised.attribute.NominalToBinary(); myBinaryData.setInputFormat(insts2); insts2 = weka.filters.Filter.useFilter(insts2, myBinaryData); //Normalization weka.filters.Filter myNormalized = new weka.filters.unsupervised.instance.Normalize(); myNormalized.setInputFormat(insts2); insts2 = weka.filters.Filter.useFilter(insts2, myNormalized); //randomize the order of the instances in the dataset. weka.filters.Filter myRandom2 = new weka.filters.unsupervised.instance.Randomize(); myRandom2.setInputFormat(insts2); insts2 = weka.filters.Filter.useFilter(insts2, myRandom2); int trainSize2 = insts2.numInstances() * percentSplit / 100; int testSize2 = insts2.numInstances() - trainSize2; weka.core.Instances train2 = new weka.core.Instances(insts2, 0, trainSize2); cl_Knn.buildClassifier(train2); string str2 = cl_Knn.toString(); int numCorrect2 = 0; for (int i = trainSize2; i < insts2.numInstances(); i++) { weka.core.Instance currentInst2 = insts2.instance(i); double predictedClass = cl_Knn.classifyInstance(currentInst2); if (predictedClass == insts2.instance(i).classValue()) { numCorrect2++; } } res_KNN = (double)((double)numCorrect2 / (double)testSize2 * 100.0); successPercent.Add(res_KNN); //Decision tree weka.core.Instances insts3 = new weka.core.Instances(new java.io.FileReader(fileDirectory)); insts3.setClassIndex(insts3.numAttributes() - 1); cl_Tree = new weka.classifiers.trees.J48(); weka.filters.Filter myNormalized2 = new weka.filters.unsupervised.instance.Normalize(); myNormalized2.setInputFormat(insts3); insts3 = weka.filters.Filter.useFilter(insts3, myNormalized2); //randomize the order of the instances in the dataset. weka.filters.Filter myRandom3 = new weka.filters.unsupervised.instance.Randomize(); myRandom3.setInputFormat(insts3); insts3 = weka.filters.Filter.useFilter(insts3, myRandom3); int trainSize3 = insts3.numInstances() * percentSplit / 100; int testSize3 = insts3.numInstances() - trainSize3; weka.core.Instances train3 = new weka.core.Instances(insts3, 0, trainSize3); cl_Tree.buildClassifier(train3); string str3 = cl_Tree.toString(); int numCorrect3 = 0; for (int i = trainSize3; i < insts3.numInstances(); i++) { weka.core.Instance currentInst3 = insts3.instance(i); double predictedClass = cl_Tree.classifyInstance(currentInst3); if (predictedClass == insts3.instance(i).classValue()) { numCorrect3++; } } res_Tree = (double)((double)numCorrect3 / (double)testSize3 * 100.0); successPercent.Add(res_Tree); //Neural Network weka.core.Instances insts4 = new weka.core.Instances(new java.io.FileReader(fileDirectory)); insts4.setClassIndex(insts4.numAttributes() - 1); cl_NN = new weka.classifiers.functions.MultilayerPerceptron(); //Nominal to Binary weka.filters.Filter myBinaryData2 = new weka.filters.unsupervised.attribute.NominalToBinary(); myBinaryData2.setInputFormat(insts4); insts4 = weka.filters.Filter.useFilter(insts4, myBinaryData2); //Normalization weka.filters.Filter myNormalized3 = new weka.filters.unsupervised.instance.Normalize(); myNormalized3.setInputFormat(insts4); insts4 = weka.filters.Filter.useFilter(insts4, myNormalized3); //randomize the order of the instances in the dataset. weka.filters.Filter myRandom4 = new weka.filters.unsupervised.instance.Randomize(); myRandom4.setInputFormat(insts4); insts4 = weka.filters.Filter.useFilter(insts4, myRandom4); int trainSize4 = insts4.numInstances() * percentSplit / 100; int testSize4 = insts4.numInstances() - trainSize4; weka.core.Instances train4 = new weka.core.Instances(insts4, 0, trainSize4); cl_NN.buildClassifier(train4); string str4 = cl_NN.toString(); int numCorrect4 = 0; for (int i = trainSize4; i < insts4.numInstances(); i++) { weka.core.Instance currentInst4 = insts4.instance(i); double predictedClass = cl_NN.classifyInstance(currentInst4); if (predictedClass == insts4.instance(i).classValue()) { numCorrect4++; } } res_NN = (double)((double)numCorrect4 / (double)testSize4 * 100.0); successPercent.Add(res_NN); //SVM weka.core.Instances insts5 = new weka.core.Instances(new java.io.FileReader(fileDirectory)); insts5.setClassIndex(insts5.numAttributes() - 1); cl_SVM = new weka.classifiers.functions.SMO(); //Nominal to Binary weka.filters.Filter myBinaryData3 = new weka.filters.unsupervised.attribute.NominalToBinary(); myBinaryData3.setInputFormat(insts5); insts5 = weka.filters.Filter.useFilter(insts5, myBinaryData3); //Normalization weka.filters.Filter myNormalized4 = new weka.filters.unsupervised.instance.Normalize(); myNormalized4.setInputFormat(insts5); insts5 = weka.filters.Filter.useFilter(insts5, myNormalized4); //randomize the order of the instances in the dataset. weka.filters.Filter myRandom5 = new weka.filters.unsupervised.instance.Randomize(); myRandom5.setInputFormat(insts5); insts5 = weka.filters.Filter.useFilter(insts5, myRandom5); int trainSize5 = insts5.numInstances() * percentSplit / 100; int testSize5 = insts5.numInstances() - trainSize5; weka.core.Instances train5 = new weka.core.Instances(insts5, 0, trainSize5); cl_SVM.buildClassifier(train5); string str5 = cl_SVM.toString(); int numCorrect5 = 0; for (int i = trainSize5; i < insts5.numInstances(); i++) { weka.core.Instance currentInst5 = insts5.instance(i); double predictedClass = cl_SVM.classifyInstance(currentInst5); if (predictedClass == insts5.instance(i).classValue()) { numCorrect5++; } } res_SVM = (double)((double)numCorrect5 / (double)testSize5 * 100.0); successPercent.Add(res_SVM); for (int i = 0; i < successPercent.Count; i++) { if ((double)successPercent[i] > max) { max = (double)successPercent[i]; count = i + 1; } } for (int i = 0; i < count; i++) { nameOfAlgo = (string)algorithms[i]; } textBox1.Text = nameOfAlgo + " is the most successful algorithm for this data set." + "(" + max + "%)\n"; }
private void button_Discover_Click(object sender, EventArgs e) { String s_newInstance = ""; StreamReader sr = new StreamReader(fileDirectory); StreamWriter sw = new StreamWriter(@"test.arff", true); String newDirectory = "test.arff"; // for algortihms string line = ""; string comp = "@data"; string comp2 = "@DATA"; line = sr.ReadLine(); do { sw.WriteLine(line); if (line == comp || line == comp2) { break; } } while ((line = sr.ReadLine()) != null); for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) { s_newInstance += (String)dataGridView1.Rows[i].Cells[1].Value + ","; //değiştir } s_newInstance += "?"; sw.WriteLine(s_newInstance); sr.Close(); sw.Close(); switch (count) { case 1: weka.core.Instances insts = new weka.core.Instances(new java.io.FileReader(newDirectory)); insts.setClassIndex(insts.numAttributes() - 1); weka.filters.Filter myNominalData = new weka.filters.unsupervised.attribute.Discretize(); myNominalData.setInputFormat(insts); insts = weka.filters.Filter.useFilter(insts, myNominalData); //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); double predictedClass = cl_Naive.classifyInstance(insts.instance(0)); Console.WriteLine("hey", insts.instance(0)); textBox3.Text = insts.classAttribute().value(Convert.ToInt32(predictedClass)); break; case 2: weka.core.Instances insts2 = new weka.core.Instances(new java.io.FileReader(fileDirectory)); insts2.setClassIndex(insts2.numAttributes() - 1); //Nominal to Binary weka.filters.Filter myBinaryData = new weka.filters.unsupervised.attribute.NominalToBinary(); myBinaryData.setInputFormat(insts2); insts2 = weka.filters.Filter.useFilter(insts2, myBinaryData); //Normalization weka.filters.Filter myNormalized = new weka.filters.unsupervised.instance.Normalize(); myNormalized.setInputFormat(insts2); insts2 = weka.filters.Filter.useFilter(insts2, myNormalized); //randomize the order of the instances in the dataset. weka.filters.Filter myRandom2 = new weka.filters.unsupervised.instance.Randomize(); myRandom2.setInputFormat(insts2); insts2 = weka.filters.Filter.useFilter(insts2, myRandom2); double predictedClass2 = cl_Knn.classifyInstance(insts2.instance(0)); textBox3.Text = insts2.classAttribute().value(Convert.ToInt32(predictedClass2)); break; case 3: weka.core.Instances insts3 = new weka.core.Instances(new java.io.FileReader(newDirectory)); insts3.setClassIndex(insts3.numAttributes() - 1); weka.filters.Filter myNormalized2 = new weka.filters.unsupervised.instance.Normalize(); myNormalized2.setInputFormat(insts3); insts3 = weka.filters.Filter.useFilter(insts3, myNormalized2); //randomize the order of the instances in the dataset. weka.filters.Filter myRandom3 = new weka.filters.unsupervised.instance.Randomize(); myRandom3.setInputFormat(insts3); insts3 = weka.filters.Filter.useFilter(insts3, myRandom3); double predictedClass3 = cl_Tree.classifyInstance(insts3.instance(0)); textBox3.Text = insts3.classAttribute().value(Convert.ToInt32(predictedClass3)); break; case 4: weka.core.Instances insts4 = new weka.core.Instances(new java.io.FileReader(newDirectory)); insts4.setClassIndex(insts4.numAttributes() - 1); //cl = new weka.classifiers.functions.MultilayerPerceptron(); //Nominal to Binary weka.filters.Filter myBinaryData2 = new weka.filters.unsupervised.attribute.NominalToBinary(); myBinaryData2.setInputFormat(insts4); insts4 = weka.filters.Filter.useFilter(insts4, myBinaryData2); //Normalization weka.filters.Filter myNormalized3 = new weka.filters.unsupervised.instance.Normalize(); myNormalized3.setInputFormat(insts4); insts4 = weka.filters.Filter.useFilter(insts4, myNormalized3); //randomize the order of the instances in the dataset. weka.filters.Filter myRandom4 = new weka.filters.unsupervised.instance.Randomize(); myRandom4.setInputFormat(insts4); insts4 = weka.filters.Filter.useFilter(insts4, myRandom4); double predictedClass4 = cl_NN.classifyInstance(insts4.instance(0)); textBox3.Text = insts4.classAttribute().value(Convert.ToInt32(predictedClass4)); break; case 5: weka.core.Instances insts5 = new weka.core.Instances(new java.io.FileReader(newDirectory)); insts5.setClassIndex(insts5.numAttributes() - 1); //Nominal to Binary weka.filters.Filter myBinaryData3 = new weka.filters.unsupervised.attribute.NominalToBinary(); myBinaryData3.setInputFormat(insts5); insts5 = weka.filters.Filter.useFilter(insts5, myBinaryData3); //Normalization weka.filters.Filter myNormalized4 = new weka.filters.unsupervised.instance.Normalize(); myNormalized4.setInputFormat(insts5); insts5 = weka.filters.Filter.useFilter(insts5, myNormalized4); //randomize the order of the instances in the dataset. weka.filters.Filter myRandom5 = new weka.filters.unsupervised.instance.Randomize(); myRandom5.setInputFormat(insts5); insts5 = weka.filters.Filter.useFilter(insts5, myRandom5); double predictedClass5 = cl_SVM.classifyInstance(insts5.instance(0)); textBox3.Text = insts5.classAttribute().value(Convert.ToInt32(predictedClass5)); break; default: textBox3.Text = "Error!"; break; } }