/// <summary> /// erzeugt einen WEKA-kompatiblen Datensatz /// </summary> /// <param name="classNames">Aufzählung der Klassennamen die im Datensatz verwendet werden sollen</param> /// <param name="featureNames">Namen der Merkmale</param> /// <param name="name">Name des Datensatzes</param> /// <param name="classAttributeName">>Name für das Klassenattribut</param> /// <returns></returns> public static Instances CreateInstances(IEnumerable <string> classNames, IEnumerable <string> featureNames, string name = "data", string classAttributeName = "class") { var features = new ArrayList(); foreach (var featureName in featureNames) { var attribute = new Attribute(featureName); features.add(attribute); } Attribute classAttribute = null; if (null != classNames) { var classNamesList = new List <string>(classNames); classNamesList.Sort(); var classValues = new ArrayList(); foreach (var className in classNamesList) { classValues.add(className); } classAttribute = new Attribute(classAttributeName, classValues); features.add(classAttribute); } var dataset = new Instances(name, features, 1); if (null != classAttribute) { dataset.setClass(classAttribute); } return(dataset); }
public HashSet <HtmlNode> RunOnTestSeenSet() { HashSet <HtmlNode> classifierSelectedNodes = new HashSet <HtmlNode>(); InitTestSeen(); foreach (string featureString in FeaturesUsed) { HashSet <HtmlNode> resNodes = DomPool.TESTSeenRunXpathQuery(useNormalPerformanceQUERY(featureString)); foreach (HtmlNode nd in resNodes) { if (!testSeenAllNodes.Contains(nd)) { continue; } testSeenNodeFeatures[nd].Add(featureString); } } FastVector fvWekaAttributes = GetDataSetAtts(); Instances testSet = new Instances("TestSeenSet", fvWekaAttributes, 10); testSet.setClassIndex(fvWekaAttributes.size() - 1); foreach (HtmlNode currNode in testSeenAllNodes) { Instance item = new SparseInstance(fvWekaAttributes.size()); for (int i = 0; i < fvWekaAttributes.size() - 1; i++) { weka.core.Attribute currFeature = (weka.core.Attribute)fvWekaAttributes.elementAt(i); if (testSeenNodeFeatures[currNode].Contains(currFeature.name())) { item.setValue(currFeature, 1); } else { item.setValue(currFeature, 0); } } //set the class weka.core.Attribute classFeature = (weka.core.Attribute)fvWekaAttributes.elementAt(fvWekaAttributes.size() - 1); //string rightVal = DomPool.TargetNodes.Contains(currNode) ? "yes" : "no"; item.setDataset(testSet); double classifierdv = classifierTree.classifyInstance(item); string classifierVal = classFeature.value((int)classifierdv); if (classifierVal.Equals("yes")) { classifierSelectedNodes.Add(currNode); } testSet.add(item); } return(classifierSelectedNodes); }
/// <summary> Generates the classifier. /// /// </summary> /// <param name="instances">set of instances serving as training data /// </param> /// <exception cref="Exception">if the classifier has not been generated successfully /// </exception> public override void buildClassifier(Instances instances) { double sumOfWeights = 0; m_Class = instances.classAttribute(); m_ClassValue = 0; switch (instances.classAttribute().type()) { case weka.core.Attribute.NUMERIC: m_Counts = null; break; case weka.core.Attribute.NOMINAL: m_Counts = new double[instances.numClasses()]; for (int i = 0; i < m_Counts.Length; i++) { m_Counts[i] = 1; } sumOfWeights = instances.numClasses(); break; default: throw new System.Exception("ZeroR can only handle nominal and numeric class" + " attributes."); } System.Collections.IEnumerator enu = instances.enumerateInstances(); //UPGRADE_TODO: Method 'java.util.Enumeration.hasMoreElements' was converted to 'System.Collections.IEnumerator.MoveNext' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilEnumerationhasMoreElements'" while (enu.MoveNext()) { //UPGRADE_TODO: Method 'java.util.Enumeration.nextElement' was converted to 'System.Collections.IEnumerator.Current' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilEnumerationnextElement'" Instance instance = (Instance) enu.Current; if (!instance.classIsMissing()) { if (instances.classAttribute().Nominal) { //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'" m_Counts[(int) instance.classValue()] += instance.weight(); } else { m_ClassValue += instance.weight() * instance.classValue(); } sumOfWeights += instance.weight(); } } if (instances.classAttribute().Numeric) { if (Utils.gr(sumOfWeights, 0)) { m_ClassValue /= sumOfWeights; } } else { m_ClassValue = Utils.maxIndex(m_Counts); Utils.normalize(m_Counts, sumOfWeights); } }
public FastVector GetDataSetAtts() { if (_fvWekaAttributes != null) { return(_fvWekaAttributes); } // Declare features FastVector fvWekaAttributes = new FastVector(DomPool.SelectorFeatures.Count() + 1); foreach (Feature currFeature in DomPool.SelectorFeatures) { weka.core.Attribute feature = new weka.core.Attribute(currFeature.ToString()); fvWekaAttributes.addElement(feature); } // Declare the class attribute along with its values FastVector fvClassVal = new FastVector(2); fvClassVal.addElement("yes"); fvClassVal.addElement("no"); weka.core.Attribute ClassAttribute = new weka.core.Attribute("theClass", fvClassVal); // Declare the feature vector fvWekaAttributes.addElement(ClassAttribute); _fvWekaAttributes = fvWekaAttributes; return(_fvWekaAttributes); }
public List <double> testMLPUsingWeka(string[] attributeArray, string[] classNames, double[] dataValues, string classHeader, string defaultclass, string modelName, int hiddelLayers = 7, double learningRate = 0.03, double momentum = 0.4, int decimalPlaces = 2, int trainingTime = 1000) { java.util.ArrayList classLabel = new java.util.ArrayList(); foreach (string className in classNames) { classLabel.Add(className); } weka.core.Attribute classHeaderName = new weka.core.Attribute(classHeader, classLabel); java.util.ArrayList attributeList = new java.util.ArrayList(); foreach (string attribute in attributeArray) { weka.core.Attribute newAttribute = new weka.core.Attribute(attribute); attributeList.Add(newAttribute); } attributeList.add(classHeaderName); weka.core.Instances data = new weka.core.Instances("TestInstances", attributeList, 0); data.setClassIndex(data.numAttributes() - 1); // Set instance's values for the attributes weka.core.Instance inst_co = new DenseInstance(data.numAttributes()); for (int i = 0; i < data.numAttributes() - 1; i++) { inst_co.setValue(i, dataValues.ElementAt(i)); } inst_co.setValue(classHeaderName, defaultclass); data.add(inst_co); java.io.File path = new java.io.File("/models/"); weka.classifiers.functions.MultilayerPerceptron clRead = loadModel(modelName, path); clRead.setHiddenLayers(hiddelLayers.ToString()); clRead.setLearningRate(learningRate); clRead.setMomentum(momentum); clRead.setNumDecimalPlaces(decimalPlaces); clRead.setTrainingTime(trainingTime); weka.filters.Filter myRandom = new weka.filters.unsupervised.instance.Randomize(); myRandom.setInputFormat(data); data = weka.filters.Filter.useFilter(data, myRandom); double classValue = clRead.classifyInstance(data.get(0)); double[] predictionDistribution = clRead.distributionForInstance(data.get(0)); List <double> predictionDistributions = new List <double>(); for (int predictionDistributionIndex = 0; predictionDistributionIndex < predictionDistribution.Count(); predictionDistributionIndex++) { string classValueString1 = classLabel.get(predictionDistributionIndex).ToString(); double prob = predictionDistribution[predictionDistributionIndex] * 100; predictionDistributions.Add(prob); } List <double> prediction = new List <double>(); prediction.Add(classValue); prediction.AddRange(predictionDistributions); return(prediction); }
public VectorClassif(int nbTags) { tagsNb = nbTags; ArrayList nomi = new ArrayList(); nomi.add("0"); nomi.add("1"); ArrayList attr = new ArrayList(); weka.core.Attribute stringAttr = new weka.core.Attribute("todoString", (List)null); attr.add(stringAttr); for (int i = 1; i <= nbTags; i++) { attr.add(new weka.core.Attribute("label" + i, nomi)); } oDataSet = new Instances("Todo-Instances", attr, 500); }
/// <summary> /// Adds teta results of gini results to the list /// Change the attributes of the arff file /// Adds the attributes to arff file /// </summary> /// <param name="insts"></param> /// <param name="result"></param> /// <param name="path"></param> private void CreateNewDataset(weka.core.Instances insts, List <double[]> result, string path) { //Tetaları Listeye Ekle 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() - 1; j++) { string value = insts.instance(i).toString(j); for (int k = 0; k < categories[j].Length; k++) { if (insts.instance(i).toString(j) == categories[j][k]) { lst[lst.Count - 1].Add(String.Format("{0:0.00}", result[j][k])); break; } } } } //Attiribute Değiştir for (int i = 0; i < insts.numAttributes() - 1; i++) { string name = insts.attribute(i).name().ToString(); insts.deleteAttributeAt(i); weka.core.Attribute att = new weka.core.Attribute(name); insts.insertAttributeAt(att, i); } //Attiributeları yaz for (int i = 0; i < insts.numInstances(); i++) { for (int j = 0; j < insts.instance(i).numValues() - 1; j++) { insts.instance(i).setValue(j, Convert.ToDouble(lst[i][j])); } } if (File.Exists(path)) { File.Delete(path); } var saver = new ArffSaver(); saver.setInstances(insts); saver.setFile(new java.io.File(path)); saver.writeBatch(); }
public Runtime AddNumericAttribute(string name, double[] values) { if (values.Length != NumInstances) { throw new ArgumentException("Values should be " + NumInstances + " in length."); } var att = new weka.core.Attribute(name); var attidx = NumAttributes; InsertAttributeAt(new PmlAttribute(att), attidx); for (int j = 0; j < values.Length; j++) { this[j].SetValue(attidx, values[j]); } return(this); }
public async Task makeNewInstance() { int a = 0; ins = new DenseInstance(insts.numAttributes()); ins.setDataset(insts); foreach (trainedData item in myData) { if (item.isNumber == false) { weka.core.Attribute m = insts.attribute(item.name); ins.setValue(m, item.value); a++; } else { } } }
public Runtime AddNominalAttribute(string name, string[] values) { if (values.Length != NumInstances) { throw new ArgumentException("Values should be " + NumInstances + " in length."); } var labels = values.Distinct().ToArray(); var att = new weka.core.Attribute(name, labels.ToArrayList()); var attidx = NumAttributes; InsertAttributeAt(new PmlAttribute(att), attidx); for (int j = 0; j < values.Length; j++) { this[j].SetValue(attidx, values[j]); } return(this); }
public string testHybridEmotionUsingWeka(string[] attributeArray, string[] classNames, double[] dataValues, string classHeader, string defaultclass, string modelName) { java.util.ArrayList classLabel = new java.util.ArrayList(); foreach (string className in classNames) { classLabel.Add(className); } weka.core.Attribute classHeaderName = new weka.core.Attribute(classHeader, classLabel); java.util.ArrayList attributeList = new java.util.ArrayList(); foreach (string attribute in attributeArray) { weka.core.Attribute newAttribute = new weka.core.Attribute(attribute); attributeList.Add(newAttribute); } attributeList.add(classHeaderName); weka.core.Instances data = new weka.core.Instances("TestInstances", attributeList, 0); data.setClassIndex(data.numAttributes() - 1); // Set instance's values for the attributes weka.core.Instance inst_co = new DenseInstance(data.numAttributes()); for (int i = 0; i < data.numAttributes() - 1; i++) { inst_co.setValue(i, dataValues.ElementAt(i)); } inst_co.setValue(classHeaderName, defaultclass); data.add(inst_co); java.io.File path = new java.io.File("/models/"); weka.classifiers.meta.Bagging clRead = loadBaggingModel(modelName, path); weka.filters.Filter myRandom = new weka.filters.unsupervised.instance.Randomize(); myRandom.setInputFormat(data); data = weka.filters.Filter.useFilter(data, myRandom); double classValue = clRead.classifyInstance(data.get(0)); string classValueString = classLabel.get(Int32.Parse(classValue.ToString())).ToString(); return(classValueString); }
//code taken from here http://stackoverflow.com/questions/9616872/classification-of-instances-in-weka/14876081#14876081 // This creates the data set's attributes vector public static FastVector CreateFastVector(int size) { var fv = new FastVector(); weka.core.Attribute att; foreach (int key in TrainingTesting_SharedVariables._trainTopIGFeatures) { if (key != TrainingTesting_SharedVariables._trainTopIGFeatures[TrainingTesting_SharedVariables._trainTopIGFeatures.Length - 1]) { att = new weka.core.Attribute("att_" + (key + 1).ToString()); fv.addElement(att); } } { var classValues = new FastVector(1); //it doesnt matter if its 3 or 1, when addElement is used the fastvector grows. List <string> labels = GuiPreferences.Instance.getLabels(); GuiPreferences.Instance.setLog("automatically! adding " + (labels.Count - 1).ToString() + " classes 2 -> " + (labels.Count + 1).ToString() + " to fast vector, based on protocol labels"); //baseline is ignored, we start from the second event in the protocol for (int l = 1; l < labels.Count; l++) { classValues.addElement((l + 1).ToString()); } //classValues.addElement("2"); //classValues.addElement("3"); //classValues.addElement("4"); //classValues.addElement("5"); var classAttribute = new weka.core.Attribute("class", classValues); fv.addElement(classAttribute); } return(fv); }
public MITesDataCollectionForm(string dataDirectory, string arffFile, bool isHierarchical) { //where data is being stored this.dataDirectory = dataDirectory; //Initialize high resolution unix timer UnixTime.InitializeTime(); //Initialize and start GUI progress thread progressMessage = null; aProgressThread = new Thread(new ThreadStart(ProgressThread)); aProgressThread.Start(); #region Load Configuration files //load the activity and sensor configuration files progressMessage = "Loading XML protocol and sensors ..."; AXML.Reader reader = new AXML.Reader(Constants.MASTER_DIRECTORY, dataDirectory); #if (!PocketPC) if (reader.validate() == false) { throw new Exception("Error Code 0: XML format error - activities.xml does not match activities.xsd!"); } else { #endif this.annotation = reader.parse(); this.annotation.DataDirectory = dataDirectory; SXML.Reader sreader = new SXML.Reader(Constants.MASTER_DIRECTORY, dataDirectory); #if (!PocketPC) if (sreader.validate() == false) { throw new Exception("Error Code 0: XML format error - sensors.xml does not match sensors.xsd!"); } else { #endif this.sensors = sreader.parse(Constants.MAX_CONTROLLERS); progressMessage += " Completed\r\n"; //TODO: remove BT components progressMessage += "Loading configuration file ..."; MITesFeatures.core.conf.ConfigurationReader creader = new MITesFeatures.core.conf.ConfigurationReader(dataDirectory); this.configuration = creader.parse(); progressMessage += " Completed\r\n"; #if (!PocketPC) } } #endif #endregion Load Configuration files #region Initialize External Data Reception Channels //Initialize 1 master decoder this.masterDecoder = new MITesDecoder(); //Initialize the software mode isExtracting = false; isCollectingDetailedData = false; isPlotting = true; isClassifying = true; #region Initialize Feature Extraction this.isExtracting = false; if (this.sensors.TotalReceivers > 0) // if there is at least 1 MIT //Extractor.Initialize(this.mitesDecoders[0], dataDirectory, this.annotation, this.sensors, this.configuration); Extractor.Initialize(this.masterDecoder, dataDirectory, this.annotation, this.sensors, this.configuration); else if (this.sensors.Sensors.Count > 0) // only built in Extractor.Initialize(this.masterDecoder, dataDirectory, this.annotation, this.sensors, this.configuration); #endregion Initialize Feature Extraction labelIndex = new Hashtable(); instances = new Instances(new StreamReader(arffFile)); instances.Class = instances.attribute(Extractor.ArffAttributeLabels.Length); classifier = new J48(); if (!File.Exists("model.xml")) { classifier.buildClassifier(instances); TextWriter tc = new StreamWriter("model.xml"); classifier.toXML(tc); tc.Flush(); tc.Close(); } else classifier.buildClassifier("model.xml", instances); fvWekaAttributes = new FastVector(Extractor.ArffAttributeLabels.Length + 1); for (int i = 0; (i < Extractor.ArffAttributeLabels.Length); i++) fvWekaAttributes.addElement(new weka.core.Attribute(Extractor.ArffAttributeLabels[i])); FastVector fvClassVal = new FastVector(); labelCounters = new int[((AXML.Category)this.annotation.Categories[0]).Labels.Count + 1]; activityLabels = new string[((AXML.Category)this.annotation.Categories[0]).Labels.Count + 1]; for (int i = 0; (i < ((AXML.Category)this.annotation.Categories[0]).Labels.Count); i++) { labelCounters[i] = 0; string label = ""; int j = 0; for (j = 0; (j < this.annotation.Categories.Count - 1); j++) label += ((AXML.Label)((AXML.Category)this.annotation.Categories[j]).Labels[i]).Name.Replace(' ', '_') + "_"; label += ((AXML.Label)((AXML.Category)this.annotation.Categories[j]).Labels[i]).Name.Replace(' ', '_'); activityLabels[i] = label; labelIndex.Add(label, i); fvClassVal.addElement(label); } weka.core.Attribute ClassAttribute = new weka.core.Attribute("activity", fvClassVal); isClassifying = true; this.aMITesActivityCounters = new Hashtable(); if (!((this.sensors.Sensors.Count == 1) && (this.sensors.HasBuiltinSensors))) { //Initialize arrays to store USB and Bluetooth controllers this.mitesControllers = new MITesReceiverController[this.sensors.TotalWiredReceivers]; #if (PocketPC) this.bluetoothControllers = new BluetoothController[this.sensors.TotalBluetoothReceivers]; //this.ts = new Thread[this.sensors.TotalBluetoothReceivers]; #endif //Initialize array to store Bluetooth connection status //this.bluetoothConnectionStatus = new bool[this.sensors.TotalBluetoothReceivers]; //Initialize a decoder for each sensor this.mitesDecoders = new MITesDecoder[this.sensors.TotalReceivers]; #if (PocketPC) #region Bluetooth reception channels initialization //Initialize and search for wockets connections progressMessage += "Initializing Bluetooth receivers ... searching " + this.sensors.TotalBluetoothReceivers + " BT receivers\r\n"; //Try to initialize all Bluetooth receivers 10 times then exit int initializationAttempt = 0; while (initializationAttempt <= 10) { if (InitializeBluetoothReceivers() == false) { initializationAttempt++; if (initializationAttempt == 10) { MessageBox.Show("Exiting: Some Bluetooth receivers in your configuration were not initialized."); Application.Exit(); System.Diagnostics.Process.GetCurrentProcess().Kill(); } else progressMessage += "Failed to initialize all BT connections. Retrying (" + initializationAttempt + ")...\r\n"; } else break; Thread.Sleep(2000); } #endregion Bluetooth reception channels initialization #endif #region USB reception channels initialization if (InitializeUSBReceivers() == false) { MessageBox.Show("Exiting: Some USB receivers in your configuration were not initialized."); #if (PocketPC) Application.Exit(); System.Diagnostics.Process.GetCurrentProcess().Kill(); #else Environment.Exit(0); #endif } #endregion USB reception channels initialization } //} #endregion Initialize External Data Reception Channels #if (PocketPC) #region Initialize Builtin Data Reception Channels if (InitializeBuiltinReceivers() == false) { MessageBox.Show("Exiting: A built in receiver channel was not found."); Application.Exit(); System.Diagnostics.Process.GetCurrentProcess().Kill(); } #endregion Initialize Builtin Data Reception Channels #endif #region Initialize GUI Components //initialize the interface components InitializeComponent(); //Initialize GUI timers progressMessage += "Initializing Timers ..."; InitializeTimers(); progressMessage += " Completed\r\n"; //Initialize different GUI components progressMessage += "Initializing GUI ..."; InitializeInterface(); progressMessage += " Completed\r\n"; this.isPlotting = true; //count the number of accelerometers if (this.sensors.IsHR) this.maxPlots = this.sensors.Sensors.Count - 1; else this.maxPlots = this.sensors.Sensors.Count; SetFormPositions(); if (this.sensors.TotalReceivers > 0) aMITesPlotter = new MITesScalablePlotter(this.panel1, MITesScalablePlotter.DeviceTypes.IPAQ, maxPlots, this.masterDecoder, GetGraphSize(false)); else aMITesPlotter = new MITesScalablePlotter(this.panel1, MITesScalablePlotter.DeviceTypes.IPAQ, maxPlots, this.masterDecoder, GetGraphSize(false)); //Override the resize event #if (PocketPC) this.Resize += new EventHandler(OnResize); #else this.form1.Resize += new EventHandler(OnResizeForm1); this.form1.FormClosing += new FormClosingEventHandler(form_FormClosing); this.form2.Resize += new EventHandler(OnResizeForm2); this.form2.FormClosing += new FormClosingEventHandler(form_FormClosing); this.form3.Resize += new EventHandler(OnResizeForm3); this.form3.FormClosing += new FormClosingEventHandler(form_FormClosing); this.form4.Resize += new EventHandler(OnResizeForm4); this.form4.FormClosing += new FormClosingEventHandler(form_FormClosing); #endif //Initialize the quality interface progressMessage += "Initializing MITes Quality GUI ..."; InitializeQualityInterface(); progressMessage += " Completed\r\n"; //Remove classifier tabs #if (PocketPC) this.tabControl1.TabPages.RemoveAt(4); this.tabControl1.SelectedIndex = 0; #else this.ShowForms(); #endif #endregion Initialize GUI Components #region Initialize Quality Tracking variables InitializeQuality(); #endregion Initialize Quality Tracking variables #region Initialize Logging InitializeLogging(dataDirectory); #endregion Initialize Logging #region Initialize CSV Storage (PC Only) #if (!PocketPC) //create some counters for activity counts averageX = new int[this.sensors.MaximumSensorID + 1]; averageY = new int[this.sensors.MaximumSensorID + 1]; averageZ = new int[this.sensors.MaximumSensorID + 1]; averageRawX = new int[this.sensors.MaximumSensorID + 1]; averageRawY = new int[this.sensors.MaximumSensorID + 1]; averageRawZ = new int[this.sensors.MaximumSensorID + 1]; prevX = new int[this.sensors.MaximumSensorID + 1]; prevY = new int[this.sensors.MaximumSensorID + 1]; prevZ = new int[this.sensors.MaximumSensorID + 1]; acCounters = new int[this.sensors.MaximumSensorID + 1]; activityCountWindowSize = 0; activityCountCSVs = new StreamWriter[this.sensors.MaximumSensorID + 1]; samplingCSVs = new StreamWriter[this.sensors.MaximumSensorID + 1]; averagedRaw = new StreamWriter[this.sensors.MaximumSensorID + 1]; masterCSV = new StreamWriter(dataDirectory + "\\MITesSummaryData.csv"); hrCSV = new StreamWriter(dataDirectory + "\\HeartRate_MITes.csv"); string csv_line1 = "UnixTimeStamp,TimeStamp,X,Y,Z"; string csv_line2 = "UnixTimeStamp,TimeStamp,Sampling"; string hr_csv_header = "UnixTimeStamp,TimeStamp,HR"; string master_csv_header = "UnixTimeStamp,TimeStamp"; foreach (Category category in this.annotation.Categories) master_csv_header += "," + category.Name; foreach (Sensor sensor in this.sensors.Sensors) { int sensor_id = Convert.ToInt32(sensor.ID); string location = sensor.Location.Replace(' ', '-'); if (sensor_id > 0) //exclude HR { activityCountCSVs[sensor_id] = new StreamWriter(dataDirectory + "\\MITes_" + sensor_id.ToString("00") + "_ActivityCount_" + location + ".csv"); activityCountCSVs[sensor_id].WriteLine(csv_line1); averagedRaw[sensor_id] = new StreamWriter(dataDirectory + "\\MITes_" + sensor_id.ToString("00") + "_1s-RawMean_" + location + ".csv"); averagedRaw[sensor_id].WriteLine(csv_line1); samplingCSVs[sensor_id] = new StreamWriter(dataDirectory + "\\MITes_" + sensor_id.ToString("00") + "_SampleRate_" + location + ".csv"); samplingCSVs[sensor_id].WriteLine(csv_line2); master_csv_header += ",MITes" + sensor_id.ToString("00") + "_SR," + "MITes" + sensor_id.ToString("00") + "_AVRaw_X," + "MITes" + sensor_id.ToString("00") + "_AVRaw_Y," + "MITes" + sensor_id.ToString("00") + "_AVRaw_Z," + "MITes" + sensor_id.ToString("00") + "_AC_X," + "MITes" + sensor_id.ToString("00") + "_AC_Y," + "MITes" + sensor_id.ToString("00") + "_AC_Z"; } } master_csv_header += ",HR"; this.masterCSV.WriteLine(master_csv_header); this.hrCSV.WriteLine(hr_csv_header); #endif #endregion Initialize CSV Storage (PC Only) #region Start Collecting Data //if (this.sensors.TotalReceivers > 0) // isStartedReceiver = true; //Start the built in polling thread #if (PocketPC) if (this.sensors.HasBuiltinSensors) { this.pollingThread = new Thread(new ThreadStart(this.pollingData)); this.pollingThread.Priority = ThreadPriority.Lowest; this.pollingThread.Start(); } #endif //Terminate the progress thread progressThreadQuit = true; //Enable all timer functions this.readDataTimer.Enabled = true; this.qualityTimer.Enabled = true; if (this.sensors.IsHR) this.HRTimer.Enabled = true; #endregion Start Collecting Data }
public static void CreateArffFiles() { java.util.ArrayList atts; java.util.ArrayList attsRel; java.util.ArrayList attVals; java.util.ArrayList attValsRel; Instances data; Instances dataRel; double[] vals; double[] valsRel; int i; // 1. set up attributes atts = new java.util.ArrayList(); // - numeric atts.Add(new weka.core.Attribute("att1")); // - nominal attVals = new java.util.ArrayList(); for (i = 0; i < 5; i++) { attVals.add("val" + (i + 1)); } weka.core.Attribute nominal = new weka.core.Attribute("att2", attVals); atts.add(nominal); // - string atts.add(new weka.core.Attribute("att3", (java.util.ArrayList)null)); // - date atts.add(new weka.core.Attribute("att4", "yyyy-MM-dd")); // - relational attsRel = new java.util.ArrayList(); // -- numeric attsRel.add(new weka.core.Attribute("att5.1")); // -- nominal attValsRel = new java.util.ArrayList(); for (i = 0; i < 5; i++) { attValsRel.Add("val5." + (i + 1)); } attsRel.add(new weka.core.Attribute("att5.2", attValsRel)); dataRel = new Instances("att5", attsRel, 0); atts.add(new weka.core.Attribute("att5", dataRel, 0)); // 2. create Instances object data = new Instances("MyRelation", atts, 0); // 3. fill with data // first instance vals = new double[data.numAttributes()]; // - numeric vals[0] = Math.PI; // - nominal vals[1] = attVals.indexOf("val3"); // - string vals[2] = data.attribute(2).addStringValue("This is a string!"); // - date vals[3] = data.attribute(3).parseDate("2001-11-09"); // - relational dataRel = new Instances(data.attribute(4).relation(), 0); // -- first instance valsRel = new double[2]; valsRel[0] = Math.PI + 1; valsRel[1] = attValsRel.indexOf("val5.3"); weka.core.Instance inst = new DenseInstance(2); inst.setValue(1, valsRel[0]); inst.setValue(1, valsRel[1]); dataRel.add(inst); // -- second instance valsRel = new double[2]; valsRel[0] = Math.PI + 2; valsRel[1] = attValsRel.indexOf("val5.2"); dataRel.add(inst); vals[4] = data.attribute(4).addRelation(dataRel); // add weka.core.Instance inst2 = new DenseInstance(4); inst2.setValue(1, vals[0]); inst2.setValue(1, vals[1]); inst2.setValue(1, vals[2]); inst2.setValue(1, vals[3]); data.add(inst2); // second instance vals = new double[data.numAttributes()]; // important: needs NEW array! // - numeric vals[0] = Math.E; // - nominal vals[1] = attVals.indexOf("val1"); // - string vals[2] = data.attribute(2).addStringValue("And another one!"); // - date vals[3] = data.attribute(3).parseDate("2000-12-01"); // - relational dataRel = new Instances(data.attribute(4).relation(), 0); // -- first instance valsRel = new double[2]; valsRel[0] = Math.E + 1; valsRel[1] = attValsRel.indexOf("val5.4"); dataRel.add(inst); // -- second instance valsRel = new double[2]; valsRel[0] = Math.E + 2; valsRel[1] = attValsRel.indexOf("val5.1"); dataRel.add(inst); vals[4] = data.attribute(4).addRelation(dataRel); // add data.add(inst2); data.setClassIndex(data.numAttributes() - 1); // 4. output data for (int x = 0; x < data.numInstances(); x++) { weka.core.Instance ins = data.instance(x); System.Console.WriteLine(ins.value(x).ToString()); } return; }
public void LearnModel() { Init(); foreach (Feature currFeature in DomPool.SelectorFeatures) { String featureString = currFeature.ToString(); HashSet <HtmlNode> resNodes = DomPool.RunXpathQuery(featureString); foreach (HtmlNode nd in resNodes) { if (!allNodes.Contains(nd)) { continue; } nodeFeatures[nd].Add(featureString); } } FastVector fvWekaAttributes = GetDataSetAtts(); Instances trainingSet = new Instances("TS", fvWekaAttributes, 10); trainingSet.setClassIndex(fvWekaAttributes.size() - 1); foreach (HtmlNode currNode in allNodes) { Instance item = new SparseInstance(fvWekaAttributes.size()); for (int i = 0; i < fvWekaAttributes.size() - 1; i++) { weka.core.Attribute currFeature = (weka.core.Attribute)fvWekaAttributes.elementAt(i); if (nodeFeatures[currNode].Contains(currFeature.name())) { item.setValue(currFeature, 1); } else { item.setValue(currFeature, 0); } } //set the class weka.core.Attribute classFeature = (weka.core.Attribute)fvWekaAttributes.elementAt(fvWekaAttributes.size() - 1); item.setValue(classFeature, (DomPool.TargetNodes.Contains(currNode)?"yes":"no")); item.setDataset(trainingSet); if (DomPool.TargetNodes.Contains(currNode)) { for (int t = 0; t < (DomPool.NonTargetNodes.Count() / DomPool.TargetNodes.Count()); t++) { trainingSet.add(new SparseInstance(item)); } } else { trainingSet.add(item); } } //String[] options = new String[2]; //options = new string[] { "-C", "0.05" }; // unpruned tree NaiveBayes cls = new NaiveBayes(); // new instance of tree //cls.setOptions(weka.core.Utils.splitOptions("-C 1.0 -L 0.0010 -P 1.0E-12 -N 0 -V -1 -W 1 -K \"weka.classifiers.functions.supportVector.PolyKernel -C 250007 -E 1.0\"")); //cls.setOptions(options); // set the options cls.buildClassifier(trainingSet); // build classifier //save the resulting classifier classifier = cls; // Reader treeDot = new StringReader(tree.graph()); // TreeBuild treeBuild = new TreeBuild(); // Node treeRoot = treeBuild.create(treeDot); FeaturesUsed = new HashSet <string>(); foreach (Feature f in DomPool.SelectorFeatures) { FeaturesUsed.Add(f.ToString()); } }
/// <summary> Builds the boosted classifier</summary> public virtual void buildClassifier(Instances data) { m_RandomInstance = new Random(m_Seed); Instances boostData; int classIndex = data.classIndex(); if (data.classAttribute().Numeric) { throw new Exception("LogitBoost can't handle a numeric class!"); } if (m_Classifier == null) { throw new System.Exception("A base classifier has not been specified!"); } if (!(m_Classifier is WeightedInstancesHandler) && !m_UseResampling) { m_UseResampling = true; } if (data.checkForStringAttributes()) { throw new Exception("Cannot handle string attributes!"); } if (m_Debug) { System.Console.Error.WriteLine("Creating copy of the training data"); } m_NumClasses = data.numClasses(); m_ClassAttribute = data.classAttribute(); // Create a copy of the data data = new Instances(data); data.deleteWithMissingClass(); // Create the base classifiers if (m_Debug) { System.Console.Error.WriteLine("Creating base classifiers"); } m_Classifiers = new Classifier[m_NumClasses][]; for (int j = 0; j < m_NumClasses; j++) { m_Classifiers[j] = Classifier.makeCopies(m_Classifier, this.NumIterations); } // Do we want to select the appropriate number of iterations // using cross-validation? int bestNumIterations = this.NumIterations; if (m_NumFolds > 1) { if (m_Debug) { System.Console.Error.WriteLine("Processing first fold."); } // Array for storing the results double[] results = new double[this.NumIterations]; // Iterate throught the cv-runs for (int r = 0; r < m_NumRuns; r++) { // Stratify the data data.randomize(m_RandomInstance); data.stratify(m_NumFolds); // Perform the cross-validation for (int i = 0; i < m_NumFolds; i++) { // Get train and test folds Instances train = data.trainCV(m_NumFolds, i, m_RandomInstance); Instances test = data.testCV(m_NumFolds, i); // Make class numeric Instances trainN = new Instances(train); trainN.ClassIndex = - 1; trainN.deleteAttributeAt(classIndex); trainN.insertAttributeAt(new weka.core.Attribute("'pseudo class'"), classIndex); trainN.ClassIndex = classIndex; m_NumericClassData = new Instances(trainN, 0); // Get class values int numInstances = train.numInstances(); double[][] tmpArray = new double[numInstances][]; for (int i2 = 0; i2 < numInstances; i2++) { tmpArray[i2] = new double[m_NumClasses]; } double[][] trainFs = tmpArray; double[][] tmpArray2 = new double[numInstances][]; for (int i3 = 0; i3 < numInstances; i3++) { tmpArray2[i3] = new double[m_NumClasses]; } double[][] trainYs = tmpArray2; for (int j = 0; j < m_NumClasses; j++) { for (int k = 0; k < numInstances; k++) { trainYs[k][j] = (train.instance(k).classValue() == j)?1.0 - m_Offset:0.0 + (m_Offset / (double) m_NumClasses); } } // Perform iterations double[][] probs = initialProbs(numInstances); m_NumGenerated = 0; double sumOfWeights = train.sumOfWeights(); for (int j = 0; j < this.NumIterations; j++) { performIteration(trainYs, trainFs, probs, trainN, sumOfWeights); Evaluation eval = new Evaluation(train); eval.evaluateModel(this, test); results[j] += eval.correct(); } } } // Find the number of iterations with the lowest error //UPGRADE_TODO: The equivalent in .NET for field 'java.lang.Double.MAX_VALUE' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'" double bestResult = - System.Double.MaxValue; for (int j = 0; j < this.NumIterations; j++) { if (results[j] > bestResult) { bestResult = results[j]; bestNumIterations = j; } } if (m_Debug) { System.Console.Error.WriteLine("Best result for " + bestNumIterations + " iterations: " + bestResult); } } // Build classifier on all the data int numInstances2 = data.numInstances(); double[][] trainFs2 = new double[numInstances2][]; for (int i4 = 0; i4 < numInstances2; i4++) { trainFs2[i4] = new double[m_NumClasses]; } double[][] trainYs2 = new double[numInstances2][]; for (int i5 = 0; i5 < numInstances2; i5++) { trainYs2[i5] = new double[m_NumClasses]; } for (int j = 0; j < m_NumClasses; j++) { for (int i = 0, k = 0; i < numInstances2; i++, k++) { trainYs2[i][j] = (data.instance(k).classValue() == j)?1.0 - m_Offset:0.0 + (m_Offset / (double) m_NumClasses); } } // Make class numeric data.ClassIndex = - 1; data.deleteAttributeAt(classIndex); data.insertAttributeAt(new weka.core.Attribute("'pseudo class'"), classIndex); data.ClassIndex = classIndex; m_NumericClassData = new Instances(data, 0); // Perform iterations double[][] probs2 = initialProbs(numInstances2); double logLikelihood = CalculateLogLikelihood(trainYs2, probs2); m_NumGenerated = 0; if (m_Debug) { System.Console.Error.WriteLine("Avg. log-likelihood: " + logLikelihood); } double sumOfWeights2 = data.sumOfWeights(); for (int j = 0; j < bestNumIterations; j++) { double previousLoglikelihood = logLikelihood; performIteration(trainYs2, trainFs2, probs2, data, sumOfWeights2); logLikelihood = CalculateLogLikelihood(trainYs2, probs2); if (m_Debug) { System.Console.Error.WriteLine("Avg. log-likelihood: " + logLikelihood); } if (System.Math.Abs(previousLoglikelihood - logLikelihood) < m_Precision) { return ; } } }
public void LearnModel() { Init(); foreach (Feature currFeature in DomPool.SelectorFeatures) { String featureString = currFeature.ToString(); HashSet <HtmlNode> resNodes = DomPool.RunXpathQuery(featureString); foreach (HtmlNode nd in resNodes) { if (!allNodes.Contains(nd)) { continue; } nodeFeatures[nd].Add(featureString); } } FastVector fvWekaAttributes = GetDataSetAtts(); Instances trainingSet = new Instances("TS", fvWekaAttributes, 100); trainingSet.setClassIndex(fvWekaAttributes.size() - 1); foreach (HtmlNode currNode in allNodes) { Instance item = new SparseInstance(fvWekaAttributes.size()); for (int i = 0; i < fvWekaAttributes.size() - 1; i++) { weka.core.Attribute currFeature = (weka.core.Attribute)fvWekaAttributes.elementAt(i); if (nodeFeatures[currNode].Contains(currFeature.name())) { item.setValue(currFeature, 1); } else { item.setValue(currFeature, 0); } } //set the class weka.core.Attribute classFeature = (weka.core.Attribute)fvWekaAttributes.elementAt(fvWekaAttributes.size() - 1); item.setValue(classFeature, (DomPool.TargetNodes.Contains(currNode)?"yes":"no")); item.setDataset(trainingSet); if (DomPool.TargetNodes.Contains(currNode)) { for (int t = 0; t < (DomPool.NonTargetNodes.Count() / DomPool.TargetNodes.Count()); t++) { trainingSet.add(new SparseInstance(item)); } } else { trainingSet.add(item); } } String[] options = new String[2]; options[0] = "-C"; // unpruned tree options[1] = "0.1"; J48 tree = new J48(); // new instance of tree tree.setOptions(options); // set the options tree.buildClassifier(trainingSet); // build classifier //save the resulting classifier classifierTree = tree; Reader treeDot = new StringReader(tree.graph()); TreeBuild treeBuild = new TreeBuild(); Node treeRoot = treeBuild.create(treeDot); FeaturesUsed = getTreeFeatures(treeRoot); }
public List <double> testSMOUsingWeka(string[] attributeArray, string[] classNames, double[] dataValues, string classHeader, string defaultclass, string modelName, int hiddelLayers = 7, double learningRate = 0.03, double momentum = 0.4, int decimalPlaces = 2, int trainingTime = 1000) { java.util.ArrayList classLabel = new java.util.ArrayList(); foreach (string className in classNames) { classLabel.Add(className); } weka.core.Attribute classHeaderName = new weka.core.Attribute(classHeader, classLabel); java.util.ArrayList attributeList = new java.util.ArrayList(); foreach (string attribute in attributeArray) { weka.core.Attribute newAttribute = new weka.core.Attribute(attribute); attributeList.Add(newAttribute); } attributeList.add(classHeaderName); weka.core.Instances data = new weka.core.Instances("TestInstances", attributeList, 0); data.setClassIndex(data.numAttributes() - 1); // Set instance's values for the attributes weka.core.Instance inst_co = new DenseInstance(data.numAttributes()); for (int i = 0; i < data.numAttributes() - 1; i++) { inst_co.setValue(i, Math.Round(dataValues.ElementAt(i), 5)); } inst_co.setValue(classHeaderName, defaultclass); data.add(inst_co); weka.core.Instance currentInst = data.get(0); int j = 0; //foreach (float value in dataValues) //{ // // double roundedValue = Math.Round(value); // //var rounded = Math.Floor(value * 100) / 100; // if (array.ElementAt(j) != value) // { // System.Console.WriteLine("Masla occur"); // } // j++; //} //double predictedClass = cl.classifyInstance(data.get(0)); weka.classifiers.functions.SMO clRead = new weka.classifiers.functions.SMO(); try { java.io.File path = new java.io.File("/models/"); clRead = loadSMOModel(modelName, path); } catch (Exception e) { //string p1 = Assembly.GetExecutingAssembly().Location; string ClassifierName = Path.GetFileName(Path.GetFileName(modelName)); string Path1 = HostingEnvironment.MapPath(@"~//libs//models//" + ClassifierName); //string Path1 = HostingEnvironment.MapPath(@"~//libs//models//FusionCustomized.model"); clRead = (weka.classifiers.functions.SMO)weka.core.SerializationHelper.read(modelName); } // weka.classifiers.functions.SMO clRead = loadSMOModel(modelName, path); clRead.setBatchSize("100"); clRead.setCalibrator(new weka.classifiers.functions.Logistic()); clRead.setKernel(new weka.classifiers.functions.supportVector.PolyKernel()); clRead.setEpsilon(1.02E-12); clRead.setC(1.0); clRead.setDebug(false); clRead.setChecksTurnedOff(false); clRead.setFilterType(new SelectedTag(weka.classifiers.functions.SMO.FILTER_NORMALIZE, weka.classifiers.functions.SMO.TAGS_FILTER)); double classValue = clRead.classifyInstance(data.get(0)); double[] predictionDistribution = clRead.distributionForInstance(data.get(0)); //for (int predictionDistributionIndex = 0; // predictionDistributionIndex < predictionDistribution.Count(); // predictionDistributionIndex++) //{ // string classValueString1 = classLabel.get(predictionDistributionIndex).ToString(); // double prob= predictionDistribution[predictionDistributionIndex]*100; // System.Console.WriteLine(classValueString1 + ":" + prob); //} List <double> prediction = new List <double>(); prediction.Add(classValue); //prediction.AddRange(predictionDistribution); return(prediction); }