expsingle.java
来自「用于multivariate时间序列分类」· Java 代码 · 共 389 行 · 第 1/2 页
JAVA
389 行
/** * Single classifier solution. That is to say, we cluster all the instances * using the same clustering algorithms. * * * @author Waleed Kadous * @version $Id: ExpSingle.java,v 1.1.1.1 2002/06/28 07:36:16 waleed Exp $ */package tclass; import tclass.clusteralg.*; import tclass.util.*; // import tclass.learnalg.*; import weka.classifiers.*; import weka.classifiers.j48.*; import weka.attributeSelection.*; import weka.filters.*; import weka.core.*; import java.io.*; import java.util.*; public class ExpSingle { // Ok. What we are going to do is to separate the learning task in // an interesting way. // First of all, though, the standard stuff static Runtime rt = Runtime.getRuntime(); String domDescFile = "sl.tdd"; String trainDataFile = "sl.tsl"; String testDataFile = "sl.ttl"; // String globalDesc = "test._gc"; // String evExtractDesc = "test._ee"; String evClusterDesc = "test._ec"; String settingsFile = "test.tal"; String learnerStuff = "weka.classifiers.j48.J48"; boolean featureSel = false; boolean makeDesc = false; boolean trainResults = false; void parseArgs(String[] args){ for(int i=0; i < args.length; i++){ if(args[i].equals("-tr")){ trainDataFile = args[++i]; } if(args[i].equals("-dd")){ domDescFile = args[++i]; } if(args[i].equals("-te")){ testDataFile = args[++i]; } if(args[i].equals("-settings")){ settingsFile = args[++i]; } if(args[i].equals("-fs")){ featureSel = true; } if(args[i].equals("-md")){ makeDesc = true; } if(args[i].equals("-trainres")){ trainResults = true; } if(args[i].equals("-l")){ learnerStuff = args[++i]; learnerStuff = learnerStuff.replace(':', ' '); System.err.println("Learner String is: " + learnerStuff); } } } // Alright. This is downright funky hacky stuff. public static void mem(String label){ System.out.println("Memory at checkpt " + label + ": " + (rt.totalMemory()/1024/1024) + " megabytes"); } public static void main(String[] args) throws Exception { Debug.setDebugLevel(Debug.PROGRESS); ExpSingle thisExp = new ExpSingle(); thisExp.parseArgs(args); mem("PARSE"); DomDesc domDesc = new DomDesc(thisExp.domDescFile); ClassStreamVecI trainStreamData = new ClassStreamVec(thisExp.trainDataFile, domDesc); ClassStreamVecI testStreamData = new ClassStreamVec(thisExp.testDataFile, domDesc); Debug.dp(Debug.PROGRESS, "PROGRESS: Data read in"); mem("DATAIN"); Settings settings = new Settings(thisExp.settingsFile, domDesc); EventExtractor evExtractor = settings.getEventExtractor(); // Global data is likely to be included in every model; so we // might as well calculated now GlobalCalc globalCalc = settings.getGlobalCalc(); ClassStreamAttValVecI trainGlobalData = globalCalc.applyGlobals(trainStreamData); ClassStreamAttValVecI testGlobalData = globalCalc.applyGlobals(testStreamData); // And we might as well extract the events. Debug.dp(Debug.PROGRESS, "PROGRESS: Globals calculated."); mem("GLOBAL"); Debug.dp(Debug.PROGRESS, "Train: " + trainGlobalData.size() + " Test: " + testGlobalData.size()); ClassStreamEventsVecI trainEventData = evExtractor.extractEvents(trainStreamData); ClassStreamEventsVecI testEventData = evExtractor.extractEvents(testStreamData); Debug.dp(Debug.PROGRESS, "PROGRESS: Events extracted"); mem("EVENTEXTRACT"); // System.out.println(trainEventData.toString()); // Now we want the clustering algorithms only to cluster // instances of each class. Make an array of clusterers, // one per class. int numTestStreams = testEventData.size(); int numClasses = domDesc.getClassDescVec().size(); EventDescVecI eventDescVec = evExtractor.getDescription(); EventClusterer eventClusterer = settings.getEventClusterer(); Debug.dp(Debug.PROGRESS, "PROGRESS: Data rearranged."); mem("REARRANGE"); //And now load it up. StreamEventsVecI trainEventSEV = trainEventData.getStreamEventsVec(); ClassificationVecI trainEventCV = trainEventData.getClassVec(); int numTrainStreams = trainEventCV.size(); ClusterVecI clusters = eventClusterer.clusterEvents(trainEventData); Debug.dp(Debug.PROGRESS, "PROGRESS: Clustering complete"); Debug.dp(Debug.PROGRESS, "Clusters are:"); Debug.dp(Debug.PROGRESS, "\n" + eventClusterer.getMapping()); Debug.dp(Debug.PROGRESS, "PROGRESS: Clustering complete. "); mem("CLUSTER"); // But wait! There's more! There is always more. // The first thing was only useful for clustering. // Now attribution. We want to attribute all the data. So we are going // to have one dataset for each learner. // First set up the attributors. Attributor attribs = new Attributor(domDesc, clusters, eventClusterer.getDescription()); Debug.dp(Debug.PROGRESS, "PROGRESS: AttributorMkr complete."); mem("MAKEATTRIBUTOR"); ClassStreamAttValVecI trainEventAtts =attribs.attribute(trainStreamData, trainEventData); ClassStreamAttValVecI testEventAtts = attribs.attribute(testStreamData, testEventData); Debug.dp(Debug.PROGRESS, "PROGRESS: Attribution complete."); mem("ATTRIBUTION"); // Combine all data sources. For now, globals go in every // one. Combiner c = new Combiner(); ClassStreamAttValVecI trainAtts = c.combine(trainGlobalData, trainEventAtts); ClassStreamAttValVecI testAtts = c.combine(testGlobalData, testEventAtts); mem("COMBINATION"); trainStreamData = null; testStreamData = null; trainEventSEV = null; trainEventCV = null; if(!thisExp.makeDesc){ clusters = null; eventClusterer = null; } attribs = null; System.gc(); mem("GARBAGECOLLECT"); // So now we have the raw data in the correct form for each // attributor. // And now, we can construct a learner for each case. // Well, for now, I'm going to do something completely crazy. // Let's run each classifier nonetheless over the whole data // ... and see what the hell happens. Maybe some voting scheme // is possible!! This is a strange form of ensemble // classifier. // Each naive bayes algorithm only gets one Debug.setDebugLevel(Debug.PROGRESS); int[] selectedIndices = null;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?