/// <summary> /// Uses the classifier to classify an instance (from its featureValues). /// </summary> /// <param name="featureValues">An array of doubles that describe the instance.</param> /// <returns>The string name of the classification of the instance.</returns> public string classify(double[] featureValues) { //if (!classifierBuilt) { _classifier.buildClassifier(_dataSet); classifierBuilt = true; } weka.core.Instance inst = new weka.core.Instance(1, featureValues); inst.setDataset(_dataSet); double result = _classifier.classifyInstance(inst); weka.core.Attribute attribute = _dataSet.attribute(_dataSet.numAttributes() - 1); string resultName = attribute.value((int)result); // Get rid of this line once ARFF files are rewritten if (resultName == "Label") { resultName = "Text"; } //Console.WriteLine(resultName); return(resultName); }
private void testButton_Click(object sender, EventArgs e) { var form = Form.ActiveForm as Form1; if (readyToTest) { weka.classifiers.Classifier cl = classifiers[highestSuccessRate.Key]; weka.core.Instance inst = new weka.core.Instance(insts.numAttributes() - 1); inst.setDataset(insts); for (int i = 0; i < inputObjects.Count; i++) { if (inputObjects[i].numeric) { inst.setValue(i, Decimal.ToDouble(inputObjects[i].num.Value)); } else { inst.setValue(i, inputObjects[i].nom.SelectedItem.ToString()); } } try { string[] values = insts.attribute(insts.numAttributes() - 1).toString().Split('{', '}')[1].Split(','); double classOfData = cl.classifyInstance(inst); int idx = Convert.ToInt32(classOfData); form.testResult.Text = values[idx]; } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("Program is not ready to test, probably needs to process data first.", "Not ready", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
/// <summary> Sets the format of the input instances. /// /// </summary> /// <param name="instanceInfo">an Instances object containing the input instance /// structure (any instances contained in the object are ignored - only the /// structure is required). /// </param> /// <exception cref="UnsupportedAttributeTypeException">if the specified attribute /// is neither numeric or nominal. /// </exception> public override bool setInputFormat(Instances instanceInfo) { base.setInputFormat(instanceInfo); m_AttIndex.Upper=instanceInfo.numAttributes() - 1; if (!Numeric && !Nominal) { throw new Exception("Can only handle numeric " + "or nominal attributes."); } m_Values.Upper=instanceInfo.attribute(m_AttIndex.Index).numValues() - 1; if (Nominal && m_ModifyHeader) { instanceInfo = new Instances(instanceInfo, 0); // copy before modifying Attribute oldAtt = instanceInfo.attribute(m_AttIndex.Index); int[] selection = m_Values.Selection; FastVector newVals = new FastVector(); for (int i = 0; i < selection.Length; i++) { newVals.addElement(oldAtt.value_Renamed(selection[i])); } instanceInfo.deleteAttributeAt(m_AttIndex.Index); instanceInfo.insertAttributeAt(new Attribute(oldAtt.name(), newVals), m_AttIndex.Index); m_NominalMapping = new int[oldAtt.numValues()]; for (int i = 0; i < m_NominalMapping.Length; i++) { bool found = false; for (int j = 0; j < selection.Length; j++) { if (selection[j] == i) { m_NominalMapping[i] = j; found = true; break; } } if (!found) { m_NominalMapping[i] = - 1; } } } setOutputFormat(instanceInfo); return true; }
/// <summary> Gets an array containing the indices of all string attributes. /// /// </summary> /// <param name="insts">the Instances to scan for string attributes. /// </param> /// <returns> an array containing the indices of string attributes in /// the input structure. Will be zero-length if there are no /// string attributes /// </returns> protected internal virtual int[] getStringIndices(Instances insts) { // Scan through getting the indices of String attributes int[] index = new int[insts.numAttributes()]; int indexSize = 0; for (int i = 0; i < insts.numAttributes(); i++) { if (insts.attribute(i).type() == Attribute.STRING) { index[indexSize++] = i; } } int[] result = new int[indexSize]; Array.Copy(index, 0, result, 0, indexSize); return result; }
/// <summary> Takes string values referenced by an Instance and copies them from a /// source dataset to a destination dataset. The instance references are /// updated to be valid for the destination dataset. The instance may have the /// structure (i.e. number and attribute position) of either dataset (this /// affects where references are obtained from). Only works if the number /// of string attributes is the same in both indices (implicitly these string /// attributes should be semantically same but just with shifted positions). /// /// </summary> /// <param name="instance">the instance containing references to strings in the source /// dataset that will have references updated to be valid for the destination /// dataset. /// </param> /// <param name="instSrcCompat">true if the instance structure is the same as the /// source, or false if it is the same as the destination (i.e. which of the /// string attribute indices contains the correct locations for this instance). /// </param> /// <param name="srcDataset">the dataset for which the current instance string /// references are valid (after any position mapping if needed) /// </param> /// <param name="srcStrAtts">an array containing the indices of string attributes /// in the source datset. /// </param> /// <param name="destDataset">the dataset for which the current instance string /// references need to be inserted (after any position mapping if needed) /// </param> /// <param name="destStrAtts">an array containing the indices of string attributes /// in the destination datset. /// </param> protected internal virtual void copyStringValues(Instance instance, bool instSrcCompat, Instances srcDataset, int[] srcStrAtts, Instances destDataset, int[] destStrAtts) { if (srcDataset == destDataset) { return ; } if (srcStrAtts.Length != destStrAtts.Length) { throw new System.ArgumentException("Src and Dest string indices differ in length!!"); } for (int i = 0; i < srcStrAtts.Length; i++) { int instIndex = instSrcCompat?srcStrAtts[i]:destStrAtts[i]; Attribute src = srcDataset.attribute(srcStrAtts[i]); Attribute dest = destDataset.attribute(destStrAtts[i]); if (!instance.isMissing(instIndex)) { //System.err.println(instance.value(srcIndex) // + " " + src.numValues() // + " " + dest.numValues()); //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 valIndex = dest.addStringValue(src, (int) instance.value_Renamed(instIndex)); // setValue here shouldn't be too slow here unless your dataset has // squillions of string attributes instance.setValue(instIndex, (double) valIndex); } } }
private void Classify(string path) { readyToTest = false; // initialize flag // Try reading file, if failed exit function insts = ReadFile(path); if (insts == null) { // Error occured reading file, display error message MessageBox.Show("Instances are null!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } var form = Form.ActiveForm as Form1; // get the current form object // Reset UI and lists succesRates.Clear(); classifiers.Clear(); form.inputPanel.Controls.Clear(); inputObjects.Clear(); form.textMostSuccessful.Text = ""; form.testResult.Text = ""; // Place attribute inputs on UI, max 18, numeric and nominal int offsetV = 60; int offsetH = 10; int width = 75; int height = 30; for (int i = 0; i < insts.numAttributes() - 1; i++) { // Create and place label Label label = new Label(); label.Width = width; label.Height = height; label.Text = insts.attribute(i).name(); label.Parent = form.inputPanel; label.Location = new Point((width * (i % 8)) + offsetH, (height * (i / 8)) + (offsetV * (i / 8))); // NumericUpDown for numeric and ComboBox for nominal values if (insts.attribute(i).isNumeric()) { NumericUpDown nud = new NumericUpDown(); nud.Width = width - 10; nud.Height = height; nud.Parent = form.inputPanel; nud.Location = new Point((width * (i % 8)) + offsetH, (height * (i / 8)) + (offsetV * (i / 8)) + height); inputObjects.Add(new UserInput(nud)); } else { string[] values = insts.attribute(i).toString().Split('{', '}')[1].Split(','); ComboBox comboBox = new ComboBox(); comboBox.DataSource = values; comboBox.Width = width - 10; comboBox.Height = height; comboBox.Parent = form.inputPanel; comboBox.Location = new Point((width * (i % 8)) + offsetH, (height * (i / 8)) + (offsetV * (i / 8)) + height); inputObjects.Add(new UserInput(comboBox)); } } // Set train and test sizes trainSize = insts.numInstances() * percentSplit / 100; testSize = insts.numInstances() - trainSize; // Set target attribute insts.setClassIndex(insts.numAttributes() - 1); // Randomize weka.filters.Filter rndFilter = new weka.filters.unsupervised.instance.Randomize(); rndFilter.setInputFormat(insts); insts = weka.filters.Filter.useFilter(insts, rndFilter); // Start threads for each method Thread t_SuccessNb = new Thread(() => CalculateSuccessForNb(insts)); t_SuccessNb.Start(); Thread t_SuccessKn = new Thread(() => CalculateSuccessForKn(insts)); t_SuccessKn.Start(); Thread t_SuccessDt = new Thread(() => CalculateSuccessForDt(insts)); t_SuccessDt.Start(); Thread t_SuccessAnn = new Thread(() => CalculateSuccessForAnn(insts)); t_SuccessAnn.Start(); Thread t_SuccessSvm = new Thread(() => CalculateSuccessForSvm(insts)); t_SuccessSvm.Start(); // Wait for threads t_SuccessNb.Join(); t_SuccessKn.Join(); t_SuccessDt.Join(); t_SuccessAnn.Join(); t_SuccessSvm.Join(); // Find out which algorithm has the best success rate foreach (var item in succesRates) { if (highestSuccessRate.Equals(default(KeyValuePair <Classifier, double>)) || highestSuccessRate.Value < item.Value) { highestSuccessRate = item; } } form.textMostSuccessful.Text = "Most successful algorithm is " + highestSuccessRate.Key + " and it will be used for testing."; readyToTest = true; // switch flag }
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 btnBrowse_Click(object sender, EventArgs e) { panel1.Controls.Clear(); panel2.Controls.Clear(); testValues = new List <object>(); classes = new List <string>(); algoritmName = String.Empty; SuccessfulAlgorithm = new List <AlgorithmModel>(); labelNames = new List <string>(); DialogResult result = openFileDialog.ShowDialog(); if (result == DialogResult.OK) { txtPath.Text = openFileDialog.FileName; } staticInsts = new weka.core.Instances(new java.io.FileReader(txtPath.Text)); for (int i = 0; i < staticInsts.attribute(staticInsts.numAttributes() - 1).numValues(); i++) { classes.Add(staticInsts.attribute(staticInsts.numAttributes() - 1).value(i)); } AlgoritmAccurancy(staticInsts, new weka.classifiers.bayes.NaiveBayes(), "Naive Bayes", true); AlgoritmAccurancy(staticInsts, new weka.classifiers.lazy.IBk(3), "KNN with k = 3", false); AlgoritmAccurancy(staticInsts, new weka.classifiers.trees.RandomForest(), "Random Forest"); AlgoritmAccurancy(staticInsts, new weka.classifiers.trees.RandomTree(), "Random Tree"); AlgoritmAccurancy(staticInsts, new weka.classifiers.trees.J48(), "J48"); pointY = 20; pointX = 20; for (int i = 0; i < staticInsts.numAttributes() - 1; i++) { if (staticInsts.attribute(i).numValues() == 0) { pointX = 0; string attName = staticInsts.attribute(i).name(); // Bunlar numeric textbox aç Label attributeName = new Label(); attributeName.Size = new Size(70, 20); attributeName.Text = attName + "\t :"; labelNames.Add(attributeName.Text); attributeName.Location = new Point(pointX, pointY); panel1.Controls.Add(attributeName); pointX += 70; TextBox txtValue = new TextBox(); txtValue.Location = new Point(pointX, pointY); panel1.Controls.Add(txtValue); panel1.Show(); pointY += 30; testValues.Add(txtValue); } else { pointX = 0; string attName = staticInsts.attribute(i).name(); // Bunlar numeric textbox aç Label attributeName = new Label(); attributeName.Size = new Size(70, 20); attributeName.Text = attName + "\t :"; labelNames.Add(attributeName.Text); attributeName.Location = new Point(pointX, pointY); panel1.Controls.Add(attributeName); pointX += 70; ComboBox cb = new ComboBox(); cb.DropDownStyle = ComboBoxStyle.DropDownList; cb.Location = new Point(pointX, pointY); List <string> items = new List <string>(); for (int j = 0; j < staticInsts.attribute(i).numValues(); j++) { items.Add(staticInsts.attribute(i).value(j).ToString()); // Bu gelen valueları dropdowna koy } cb.Items.AddRange(items.ToArray()); cb.SelectedIndex = 0; panel1.Controls.Add(cb); panel1.Show(); pointY += 30; testValues.Add(cb); } } double maxRatio = Double.MinValue; foreach (var item in SuccessfulAlgorithm) { if (item.SuccessRatio > maxRatio) { maxRatio = item.SuccessRatio; algoritmName = item.AlgorithName; predictor = item.TrainModel.classifier; } } string _maxRatio = string.Format("{0:0.00}", maxRatio); lblSuccessulAlgorithm.Text = "The most Successful Algoritm is " + algoritmName + " and the ratio of accurancy is %" + _maxRatio; Button btn = new Button(); btn.Click += Btn_Click; btn.Location = new Point(pointX, pointY); btn.Size = new Size(80, 20); btn.Text = "DISCOVER"; btn.BackColor = Color.White; panel1.Controls.Add(btn); panel1.Show(); }
/// <summary> Sets the format of the input instances. /// /// </summary> /// <param name="instanceInfo">an Instances object containing the input instance /// structure (any instances contained in the object are ignored - only the /// structure is required). /// </param> /// <returns> true if the outputFormat may be collected immediately /// </returns> /// <exception cref="Exception">if the format couldn't be set successfully /// </exception> public override bool setInputFormat(Instances instanceInfo) { base.setInputFormat(instanceInfo); m_SelectCols.Upper = instanceInfo.numAttributes() - 1; // Create the output buffer FastVector attributes = new FastVector(); int outputClass = - 1; m_SelectedAttributes = m_SelectCols.Selection; int inStrKeepLen = 0; int[] inStrKeep = new int[m_SelectedAttributes.Length]; for (int i = 0; i < m_SelectedAttributes.Length; i++) { int current = m_SelectedAttributes[i]; if (instanceInfo.classIndex() == current) { outputClass = attributes.size(); } Attribute keep = (Attribute) instanceInfo.attribute(current).copy(); if (keep.type() == Attribute.STRING) { inStrKeep[inStrKeepLen++] = current; } attributes.addElement(keep); } m_InputStringIndex = new int[inStrKeepLen]; Array.Copy(inStrKeep, 0, m_InputStringIndex, 0, inStrKeepLen); Instances outputFormat = new Instances(instanceInfo.relationName(), attributes, 0); outputFormat.ClassIndex = outputClass; setOutputFormat(outputFormat); return true; }
//Dosya seçim bölümü ve yüzde hesabı bölümü private void btnBrowse_Click(object sender, EventArgs e) { clears(); OpenFileDialog file = new OpenFileDialog(); file.Filter = "Files (ARFF)|*.ARFF"; file.Multiselect = false; file.Title = "Please select a dataset file!"; if (file.ShowDialog() == DialogResult.OK) { txtPath.Text = file.FileName; fileName = file.SafeFileName; //dosya seçildikten sonra işlemi gerçekleştiriyor. try { if (txtPath.Text.Length < 1) { MessageBox.Show("Please select file!", "Error Message!"); txtPath.Text = ""; } else { this.Text = "Processing..."; insts = new weka.core.Instances(new java.io.FileReader(txtPath.Text)); //naive bayes double max_value = NaiveBayesTest(insts); model = NaiveBayescl; name = "Naïve Bayes"; //logistic regression double LogRegressionvalue = LogRegressionTest(insts); if (LogRegressionvalue > max_value) { max_value = LogRegressionvalue; model = LogRegressioncl; name = "Logistic Regression"; } //knn double KnnValue = Knn(insts); if (KnnValue > max_value) { max_value = KnnValue; model = Knncl; name = "K-Nearest Neighbour"; } //J48 double J48Value = J48classifyTest(insts); if (J48Value > max_value) { max_value = J48Value; model = J48cl; name = "Decision Tree(J48)"; } //Random forest double RFvalue = RandomForestTest(insts); if (RFvalue > max_value) { max_value = RFvalue; model = RandomForestcl; name = "Decision Tree(Random Forest)"; } //Random Tree double RTvalue = RandomTreeTest(insts); if (RTvalue > max_value) { max_value = RTvalue; model = RandomTreecl; name = "Decision Tree(Random Tree)"; } //Artificial nn double AnnValue = ArtificialNN(insts); if (AnnValue > max_value) { max_value = AnnValue; model = Anncl; name = "Artificial Neural Network"; } //Svm double SvmValue = SVM(insts); if (SvmValue > max_value) { max_value = SvmValue; model = Svmcl; name = "Support Vector Machine"; } //Model kaydetme kısmı weka.core.SerializationHelper.write("models/mdl.model", model); lblResult.Text = name + " is the most successful algorithm for this data set (%" + string.Format("{0:0.00}", max_value) + ")"; this.Text = "DEUCENG - ML Classification Tool"; //seçme işlemleri numAtt = insts.numAttributes() - 1; int x = 30, y = 130, t = 35, l = 110; int txt = 0, cmb = 0, r1 = 0, r2 = 0; labels = new Label[insts.numAttributes()]; for (int i = 0; i < numAtt; i++) { if (insts.attribute(i).isNumeric()) { txt++; } else if (insts.attribute(i).isNominal()) { cmb++; } } nominal = new ComboBox[cmb]; numeric = new TextBox[txt]; typeAtt = new bool[numAtt]; this.Height += (numAtt + 1) * t; for (int i = 0; i < numAtt; i++) { if (insts.attribute(i).isNominal()) { string[] s1 = insts.attribute(i).toString().Split('{'); string[] s2 = s1[1].Split('}'); string[] s3 = s2[0].Split(','); nominal[r1] = new ComboBox(); labels[i] = new Label(); for (int j = 0; j < s3.Length; j++) { nominal[r1].Items.Add(s3[j].Replace('\'', ' ').Trim()); } labels[i].Text = insts.attribute(i).name(); labels[i].Left = x; labels[i].Top = y; nominal[r1].Left = x + l; nominal[r1].Top = y; nominal[r1].DropDownStyle = ComboBoxStyle.DropDownList; y += t; Controls.Add(nominal[r1]); Controls.Add(labels[i]); r1++; typeAtt[i] = true; } else if (insts.attribute(i).isNumeric()) { numeric[r2] = new TextBox(); labels[i] = new Label(); labels[i].Text = insts.attribute(i).name(); labels[i].Left = x; labels[i].Top = y; numeric[r2].Left = x + l; numeric[r2].Top = y; y += t; Controls.Add(numeric[r2]); Controls.Add(labels[i]); r2++; typeAtt[i] = false; } btnDiscover.Enabled = true; } } } catch (Exception e2) { MessageBox.Show(e2.Message, "Error Message!"); } } }
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 bool PrepareDataset() { try { weka.filters.Filter missingFilter = new weka.filters.unsupervised.attribute.ReplaceMissingValues(); // missing values handled missingFilter.setInputFormat(insts); insts = weka.filters.Filter.useFilter(insts, missingFilter); bool isTargetNumeric = insts.attribute(insts.numAttributes() - 1).isNumeric(); List <bool> isNumeric = new List <bool>(); List <bool> is2Categorical = new List <bool>(); List <List <string> > numericColumns = new List <List <string> >(); List <string> atrNames = new List <string>(); for (int i = 0; i < insts.numAttributes(); i++) { atrNames.Add(insts.attribute(i).name()); bool isNum = insts.attribute(i).isNumeric(); isNumeric.Add(isNum); if (isNum == true) { numericColumns.Add(new List <string>()); for (int j = 0; j < insts.numInstances(); j++) { numericColumns[numericColumns.Count - 1].Add(insts.instance(j).toString(i)); } } } weka.filters.unsupervised.attribute.Discretize myDiscretize = new weka.filters.unsupervised.attribute.Discretize(); myDiscretize.setInputFormat(insts); myDiscretize.setFindNumBins(true); insts = weka.filters.Filter.useFilter(insts, myDiscretize); List <List <string> > atrs = new List <List <string> >(); for (int i = 0; i < insts.numAttributes(); i++) { atrs.Add(new List <string>()); for (int j = 0; j < insts.attribute(i).numValues(); j++) { string sub_category = insts.attribute(i).value(j); string temp = sub_category.Replace("'", string.Empty); atrs[atrs.Count - 1].Add(temp); } if (atrs[atrs.Count - 1].Count == 2) { is2Categorical.Add(true); } else { is2Categorical.Add(false); } } List <List <string> > lst = new List <List <string> >(); for (int i = 0; i < insts.numInstances(); i++) { lst.Add(new List <string>()); for (int j = 0; j < insts.instance(i).numValues(); j++) { string temp = insts.instance(i).toString(j); temp = temp.Replace("\\", string.Empty); temp = temp.Replace("'", string.Empty); lst[lst.Count - 1].Add(temp); } } List <string> targetValues = atrs[insts.numAttributes() - 1]; List <List <string> > giniDataset = ConvertToNumericWithGini(lst, atrs); giniDataset = Arrange2CategoricalColumns(giniDataset, lst, is2Categorical); giniDataset = ChangeBackNumericalColumns(giniDataset, numericColumns, isNumeric); WriteFile(giniDataset, filename + "-numeric-gini.arff", atrNames, targetValues, isTargetNumeric); List <List <string> > twoingDataset = ConvertToNumericWithTwoing(lst, atrs); twoingDataset = Arrange2CategoricalColumns(twoingDataset, lst, is2Categorical); twoingDataset = ChangeBackNumericalColumns(twoingDataset, numericColumns, isNumeric); WriteFile(twoingDataset, filename + "-numeric-twoing.arff", atrNames, targetValues, isTargetNumeric); return(true); } catch (Exception e) { return(false); } }
/// <summary> Calculates the area under the ROC curve. This is normalised so /// that 0.5 is random, 1.0 is perfect and 0.0 is bizarre. /// /// </summary> /// <param name="tcurve">a previously extracted threshold curve Instances. /// </param> /// <returns> the ROC area, or Double.NaN if you don't pass in /// a ThresholdCurve generated Instances. /// </returns> public static double getROCArea(Instances tcurve) { //UPGRADE_NOTE: Final was removed from the declaration of 'n '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" int n = tcurve.numInstances(); if (!RELATION_NAME.Equals(tcurve.relationName()) || (n == 0)) { return System.Double.NaN; } //UPGRADE_NOTE: Final was removed from the declaration of 'tpInd '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" int tpInd = tcurve.attribute(TRUE_POS_NAME).index(); //UPGRADE_NOTE: Final was removed from the declaration of 'fpInd '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" int fpInd = tcurve.attribute(FALSE_POS_NAME).index(); //UPGRADE_NOTE: Final was removed from the declaration of 'tpVals '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" double[] tpVals = tcurve.attributeToDoubleArray(tpInd); //UPGRADE_NOTE: Final was removed from the declaration of 'fpVals '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" double[] fpVals = tcurve.attributeToDoubleArray(fpInd); //UPGRADE_NOTE: Final was removed from the declaration of 'tp0 '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" double tp0 = tpVals[0]; //UPGRADE_NOTE: Final was removed from the declaration of 'fp0 '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" double fp0 = fpVals[0]; double area = 0.0; //starts at high values and goes down double xlast = 1.0; double ylast = 1.0; for (int i = 1; i < n; i++) { //UPGRADE_NOTE: Final was removed from the declaration of 'x '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" double x = fpVals[i] / fp0; //UPGRADE_NOTE: Final was removed from the declaration of 'y '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" double y = tpVals[i] / tp0; //UPGRADE_NOTE: Final was removed from the declaration of 'areaDelta '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" double areaDelta = (y + ylast) * (xlast - x) / 2.0; /* System.err.println("[" + i + "]" + " x=" + x + " y'=" + y + " xl=" + xlast + " yl=" + ylast + " a'=" + areaDelta); */ area += areaDelta; xlast = x; ylast = y; } //make sure ends at 0,0 if (xlast > 0.0) { //UPGRADE_NOTE: Final was removed from the declaration of 'areaDelta '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" double areaDelta = ylast * xlast / 2.0; //System.err.println(" a'=" + areaDelta); area += areaDelta; } //System.err.println(" area'=" + area); return area; }
/// <summary> Calculates the n point precision result, which is the precision averaged /// over n evenly spaced (w.r.t recall) samples of the curve. /// /// </summary> /// <param name="tcurve">a previously extracted threshold curve Instances. /// </param> /// <param name="n">the number of points to average over. /// </param> /// <returns> the n-point precision. /// </returns> public static double getNPointPrecision(Instances tcurve, int n) { if (!RELATION_NAME.Equals(tcurve.relationName()) || (tcurve.numInstances() == 0)) { return System.Double.NaN; } int recallInd = tcurve.attribute(RECALL_NAME).index(); int precisInd = tcurve.attribute(PRECISION_NAME).index(); double[] recallVals = tcurve.attributeToDoubleArray(recallInd); int[] sorted = Utils.sort(recallVals); double isize = 1.0 / (n - 1); double psum = 0; for (int i = 0; i < n; i++) { int pos = binarySearch(sorted, recallVals, i * isize); double recall = recallVals[sorted[pos]]; double precis = tcurve.instance(sorted[pos]).value_Renamed(precisInd); /* System.err.println("Point " + (i + 1) + ": i=" + pos + " r=" + (i * isize) + " p'=" + precis + " r'=" + recall); */ // interpolate figures for non-endpoints while ((pos != 0) && (pos < sorted.Length - 1)) { pos++; double recall2 = recallVals[sorted[pos]]; if (recall2 != recall) { double precis2 = tcurve.instance(sorted[pos]).value_Renamed(precisInd); double slope = (precis2 - precis) / (recall2 - recall); double offset = precis - recall * slope; precis = isize * i * slope + offset; /* System.err.println("Point2 " + (i + 1) + ": i=" + pos + " r=" + (i * isize) + " p'=" + precis2 + " r'=" + recall2 + " p''=" + precis); */ break; } } psum += precis; } return psum / n; }
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"; }