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); } }
public int ClassifyInstance(double[] attributes, out double[] percentages) { double classificationResult = 1.0; testInstance.setDataset(dataSet); testInstance.setClassMissing(); dataSet.add(testInstance); for (int i = 0; i < attributes.Length; i++) { testInstance.setValue(i, attributes[i]); } classificationResult = m_cl.classifyInstance(testInstance); dataSet.delete(0); percentages = m_cl.distributionForInstance(testInstance); return((int)classificationResult); }
public weka.core.Instance GenerateFeatures(PokerHand hand, int rIdx, int aIdx, weka.core.Instances data, bool generateClass = true) { // Check that we are using limit betting. Debug.Assert(hand.Context.BettingType == BettingType.FixedLimit); var results = new weka.core.Instance(data.numAttributes()); results.setDataset(data); int attIdx = 0; foreach (var method in typeof(LimitFeatureGenerator).GetMethods()) { // Get all the features of this class. var attributes = method.GetCustomAttributes(typeof(Feature), true); if (attributes.Length == 0) continue; // Get the feature attribute on this method. var attr = ((Feature)attributes[0]); // Get the name for this attribute string name = attr.Name; //Console.WriteLine("Hand: {0} Feature: {1}", hand.Context.ID, name); // Get the feature only if it's applicable to this situation. object feature = null; if(rIdx >= (int)attr.MinRound && rIdx <= (int)attr.MaxRound) feature = method.Invoke(this, new object[] { hand, rIdx, aIdx }); if (SkipMissingFeatures && (feature == null || feature.ToString() == "?")) continue; switch (attr.FType) { case FeatureType.Continuous: results.setValue(attIdx, (double)feature); break; case FeatureType.Discrete: results.setValue(attIdx, (int)feature); break; case FeatureType.Boolean: case FeatureType.Nominal: case FeatureType.Enum: { var attribute = data.attribute(attIdx); var attVal = attribute.indexOfValue(feature.ToString()); if(attVal < 0 || attVal > attribute.numValues()) throw new Exception(string.Format ("Invalid attribute value: {0} for attribute {1} (idx: {2} total values: {3}", feature.ToString(), name, attVal, attribute.numValues())); results.setValue(attribute, attVal); } break; case FeatureType.String: { var attribute = data.attribute(attIdx); results.setValue(attribute, feature.ToString()); } break; default: throw new Exception("Unspecified feature type for feature: " + method.Name); } attIdx++; } if(generateClass) { var classAttr = data.classAttribute(); switch (hand.Rounds[rIdx].Actions[aIdx].Type) { case ActionType.Bet: case ActionType.Raise: results.setClassValue(classAttr.indexOfValue("Raise")); break; case ActionType.Call: case ActionType.Check: results.setClassValue(classAttr.indexOfValue("Call")); break; case ActionType.Fold: results.setClassValue(classAttr.indexOfValue("Fold"));; break; default: break; } } return results; }
/// <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 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)); } }