本文整理汇总了Java中weka.core.converters.ArffSaver类的典型用法代码示例。如果您正苦于以下问题:Java ArffSaver类的具体用法?Java ArffSaver怎么用?Java ArffSaver使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ArffSaver类属于weka.core.converters包,在下文中一共展示了ArffSaver类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: saveInstancesToArffFile
import weka.core.converters.ArffSaver; //导入依赖的package包/类
public File saveInstancesToArffFile(Instances instances, String filename) throws IOException
{
LOGGER.trace("Saving data to ARFF file [{}].", filename);
File outputFile = new File(filename);
if (outputFile.exists())
{
outputFile.delete();
outputFile.createNewFile();
}
ArffSaver arffSaver = new ArffSaver();
arffSaver.setInstances(instances);
arffSaver.setFile(outputFile);
arffSaver.writeBatch();
return arffSaver.retrieveFile();
}
示例2: main
import weka.core.converters.ArffSaver; //导入依赖的package包/类
/**
* Main method for testing this class.
*
*
* should contain the path of input dataset and the name of
* target file scheme (see Evaluation)
*@param args arguments
*/
static public void main(String args[]) {
if (args.length == 2) {
TweetCollectionToArff ta = new SemEvalToArff();
try {
Instances dataset = ta.createDataset(args[0]);
ArffSaver saver = new ArffSaver();
saver.setInstances(dataset);
saver.setFile(new File(args[1]));
saver.writeBatch();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
示例3: main
import weka.core.converters.ArffSaver; //导入依赖的package包/类
/**
* Main method for testing this class.
*
* should contain the path of input dataset and the name of
* target file scheme (see Evaluation)
* @param args arguments
*/
static public void main(String args[]) {
if (args.length == 2) {
TweetCollectionToArff ta = new HumanCodedToArff();
try {
Instances dataset = ta.createDataset(args[0]);
ArffSaver saver = new ArffSaver();
saver.setInstances(dataset);
saver.setFile(new File(args[1]));
saver.writeBatch();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
示例4: shuffle
import weka.core.converters.ArffSaver; //导入依赖的package包/类
public static void shuffle(String file, int classindex, String outputFile)
throws IOException {
// create the stream to read the data
ArffFileStream stream = new ArffFileStream(file, classindex);
InstancesHeader header = stream.getHeader();
ArrayList<Instance> instanceList = new ArrayList<Instance>();
System.out.println("Loading data ...");
int cnt = 0;
while (stream.hasMoreInstances()) {
if (++cnt % 10000 == 0) {
System.out.println("Read " + cnt + " items.");
}
instanceList.add(stream.nextInstance());
}
System.out.println("Read all items ... shuffling.");
Collections.shuffle(instanceList);
ArrayList<Attribute> attributeList = new ArrayList<Attribute>();
for (int i = 0; i < header.numAttributes(); i++) {
attributeList.add(header.attribute(i));
}
Instances dataSet = new Instances("reduced", attributeList, 2);
for (Instance inst : instanceList) {
dataSet.add(inst);
inst.setDataset(dataSet);
}
System.out.println("Writing output ...");
ArffSaver saver = new ArffSaver();
saver.setInstances(dataSet);
saver.setFile(new File(outputFile));
saver.writeBatch();
System.out.println("Done.");
}
示例5: reduce
import weka.core.converters.ArffSaver; //导入依赖的package包/类
@Override
public void reduce(Iterable<WikiDocumentOutput> values, Collector<EvaluationResult> out) throws Exception {
Instances instances;
DependencyParser parser = DependencyParser.loadFromModelFile(config.dependencyParserModel());
WekaUtils wekaUtils = new WekaUtils();
instances = wekaUtils.createInstances("AllRelations");
for (WikiDocumentOutput value : values) {
wekaUtils.addRelationsToInstances(parser, value.getRelations(), value.getTitle(), value.getqId(), instances, value.getMaxSentenceLength());
}
if (config.isWriteInstances()) {
File instancesFile = new File(config.getOutputDir() + INSTANCES_ARFF_FILE_NAME);
ArffSaver arffSaver = new ArffSaver();
arffSaver.setFile(instancesFile);
arffSaver.setInstances(instances);
arffSaver.writeBatch();
}
//do model once with all data
if (config.getWriteSvmModel()) {
generateAndWriteFullModel(instances);
}
process(out, instances);
}
示例6: dump_from_scratch
import weka.core.converters.ArffSaver; //导入依赖的package包/类
/**
* When the score changes, rewrite the file.
* This is really rare in practice, so don't bother optimizing it.
*/
private static void dump_from_scratch(Collection<String> names, Timestamp start_time) throws IOException {
saved_schema_version = names.size();
FastVector attributes = new FastVector();
// Answer score names
for (String name: names)
attributes.addElement(new Attribute(name));
Instances data = new Instances("Watsonsim captured question stream", attributes, 0);
// Save the results to a file
saver = new ArffSaver();
saver.setStructure(data);
saver.setRetrieval(Saver.INCREMENTAL);
saver.setFile(new File("data/weka-log." + start_time + ".arff"));
for (Score row : dataset)
saver.writeIncremental(new Instance(1.0, row.getEach(names)));
}
示例7: CSVtoARFF
import weka.core.converters.ArffSaver; //导入依赖的package包/类
public static void CSVtoARFF(String csvfilename, String arfffilename) {
try {
// load CSV
CSVLoader loader = new CSVLoader();
loader.setSource(new File(csvfilename));
Instances data = loader.getDataSet();
// save ARFF
ArffSaver saver = new ArffSaver();
saver.setInstances(data);
saver.setFile(new File(arfffilename));
saver.setDestination(new File(arfffilename));
saver.writeBatch();
} catch (Exception e) {
e.printStackTrace();
}
}
示例8: main
import weka.core.converters.ArffSaver; //导入依赖的package包/类
/**
* takes 2 arguments:
* - CSV input file
* - ARFF output file
*/
public static void main(String[] vagina) throws Exception {
String[] args = {"trainingSet.csv","penis.arff"};
// load CSV
CSVLoader loader = new CSVLoader();
loader.setSource(new File(args[0]));
Instances data = loader.getDataSet();
// save ARFF
ArffSaver saver = new ArffSaver();
saver.setInstances(data);
File penis = new File(args[1]);
saver.setFile(penis);
// saver.setDestination(penis);
saver.writeBatch();
}
示例9: buildTrainingDataFromCorpus
import weka.core.converters.ArffSaver; //导入依赖的package包/类
public static void buildTrainingDataFromCorpus(String dataSetName,
File corpusRoot, FVGenerator fvGenerator, File dest)
throws IOException {
Collection<File> children = FileUtils.listFiles(corpusRoot,
new RegexFileFilter(".+\\.txt", IOCase.INSENSITIVE), DirectoryFileFilter.INSTANCE);
ArffSaver saver = new ArffSaver();
saver.setFile(dest);
saver.setRetrieval(Saver.INCREMENTAL);
boolean first = true;
for (File textFile : children) {
Instances dataSet = buildTrainingDataFromFile(dataSetName, textFile, fvGenerator);
if (first) {
saver.setStructure(dataSet);
first = false;
}
for (int i = 0; i < dataSet.numInstances(); ++i) {
saver.writeIncremental(dataSet.instance(i));
}
}
saver.getWriter().flush();
}
示例10: convertToArff
import weka.core.converters.ArffSaver; //导入依赖的package包/类
public static Instances convertToArff(List<Document> dataSet, List<String> vocabulary, String fileName) {
int dataSetSize = dataSet.size();
/* Create features */
ArrayList<Attribute> attributes = new ArrayList<>();
for (int i = 0; i < vocabulary.size(); i++) {
attributes.add(new Attribute("word_" + i));
}
Attribute classAttribute = new Attribute("Class");
attributes.add(classAttribute);
/* Add examples */
System.out.println("Building instances...");
Instances trainingDataSet = new Instances(fileName, attributes, 0);
for (int k = 0; k < dataSetSize; k++) {
Document document = dataSet.get(k);
Instance example = new DenseInstance(attributes.size());
for (int i = 0; i < vocabulary.size(); i++) {
String word = vocabulary.get(i);
example.setValue(i, Collections.frequency(document.getTerms(), word));
}
example.setValue(classAttribute, document.getDocumentClass());
trainingDataSet.add(example);
int progress = (int) ((k * 100.0) / dataSetSize);
System.out.printf("\rPercent completed: %3d%%", progress);
}
trainingDataSet.setClass(classAttribute);
System.out.println();
System.out.println("Writing to file ...");
try {
ArffSaver saver = new ArffSaver();
saver.setInstances(trainingDataSet);
saver.setFile(new File(fileName));
saver.writeBatch();
} catch (IOException e) {
e.printStackTrace();
}
return trainingDataSet;
}
示例11: saveDataToArffFile
import weka.core.converters.ArffSaver; //导入依赖的package包/类
/**
* Save @param data to the Arff file at @param path
*/
public static void saveDataToArffFile(String path, Instances data) throws IOException{
System.out.println("\nSaving to file " + path + "...");
ArffSaver saver = new ArffSaver();
saver.setInstances(data);
saver.setFile(new File(path));
saver.writeBatch();
}
示例12: generateArff
import weka.core.converters.ArffSaver; //导入依赖的package包/类
public void generateArff() throws Exception{
//
CSVLoader loader = new CSVLoader();
// Set options
loader.setNominalAttributes("last");
loader.setStringAttributes("");
loader.setMissingValue("?");
loader.setFieldSeparator("\t");
loader.setFile(new File(seqConfig.getOutDir().getAbsolutePath()+File.separator+"tmpCounts.mat"));
Instances data = loader.getDataSet();
//Set subgroup index
if(data.classIndex() == -1)
data.setClassIndex(data.numAttributes()-1);
//First, get weight index
int wInd = data.numAttributes()-2;
// Now set weights
for(int i=0; i<data.numInstances(); i++){
double weight = data.instance(i).value(wInd);
data.instance(i).setWeight(weight);
}
// Now delete the weight attribute
data.deleteAttributeAt(wInd);
//Save the arff file
ArffSaver saver = new ArffSaver();
saver.setFile(new File(seqConfig.getOutDir().getAbsolutePath()+File.separator+seqConfig.getArffOutName()));
saver.setInstances(data);
saver.writeBatch();
}
示例13: saveInstancesToArff
import weka.core.converters.ArffSaver; //导入依赖的package包/类
public static void saveInstancesToArff(Instances instances, String path) throws ClassifierException {
try {
ArffSaver arffSaver = new ArffSaver();
arffSaver.setInstances(instances);
File destination = new File(path);
arffSaver.setFile(destination);
arffSaver.writeBatch();
} catch (IOException e) {
throw new ClassifierException("Saving arff file failed", e);
}
}
示例14: saveFoldFiles
import weka.core.converters.ArffSaver; //导入依赖的package包/类
protected void saveFoldFiles(Instances Training_Instances, int i, Instances Testing_Instances) throws IOException {
// Store instances to related fold files in ARFF subdir (WARNING: It must exist)
ArffSaver asSaver = new ArffSaver();
asSaver.setInstances(Training_Instances);
asSaver.setFile(new File(String.format("ARFF/train-fold%d.arff", i)));
asSaver.writeBatch();
asSaver.setInstances(Testing_Instances);
asSaver.setFile(new File(String.format("ARFF/test-fold%d.arff", i)));
asSaver.writeBatch();
}
示例15: CSVToARFF
import weka.core.converters.ArffSaver; //导入依赖的package包/类
public static void CSVToARFF(File input, File output) throws IOException {
CSVLoader csvDataset = new CSVLoader();
csvDataset.setSource(input);
Instances arffDataset = csvDataset.getDataSet();
ArffSaver saver = new ArffSaver();
saver.setInstances(arffDataset);
saver.setFile(output);
saver.writeBatch();
}