# Bati's eHome of Tech

:: :: :: :: :: :: :: :: 174 随笔 :: 0 文章 :: 32 评论 :: 0 引用

Weka，是一个用Java编写的数据挖掘软件。数据挖掘，从字面上来看，它是一个从数据中找寻有用信息的过程，不过，它涉及的内容很多，所以，这里借用“分类”这一面来说事。

import weka.classifiers.Classifier;
import weka.classifiers.bayes.NaiveBayesMultinomial;
import weka.core.Attribute;
import weka.core.FastVector;
import weka.core.Instance;
import weka.core.Instances;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.StringToWordVector;
public class Main {
private static final String GOOD = "G";
private static final String BAD = "B";
private static final String CATEGORY = "category";
private static final String TEXT = "text";
private static final int INIT_CAPACITY = 100;
private static final String[][] TRAINING_DATA = {
{"Good", GOOD},
{"Wonderful", GOOD},
{"Cool", GOOD},
};
private static final String TEST_DATA = "Good";
private static Filter filter = new StringToWordVector();
private static Classifier classifier = new NaiveBayesMultinomial();
public static void main(String[] args) throws Exception {
FastVector categories = new FastVector();
FastVector attributes = new FastVector();
Instances instances = new Instances("Weka", attributes, INIT_CAPACITY);
instances.setClassIndex(instances.numAttributes() - 1);
for (String[] pair : TRAINING_DATA) {
String text = pair;
String category = pair;
Instance instance = createInstanceByText(instances, text);
instance.setClassValue(category);
}
filter.setInputFormat(instances);
Instances filteredInstances = Filter.useFilter(instances, filter);
classifier.buildClassifier(filteredInstances);
// Test
String testText = TEST_DATA;
Instance testInstance = createTestInstance(instances.stringFreeStructure(), testText);
double predicted = classifier.classifyInstance(testInstance);
String category = instances.classAttribute().value((int)predicted);
System.out.println(category);
}
private static Instance createInstanceByText(Instances data, String text) {
Attribute textAtt = data.attribute(TEXT);
Instance instance = new Instance(2);
instance.setValue(textAtt, index);
instance.setDataset(data);
return instance;
}
private static Instance createTestInstance(Instances data, String text) throws Exception {
Instance testInstance = createInstanceByText(data, text);
filter.input(testInstance);
return filter.output();
}
}

classifier.classifyInstance(testInstance);

posted on 2008-05-12 15:29  Bati  阅读(...)  评论(...编辑  收藏
{"ft_lang_label":"__label__zh","ft_lang_prob":0.6307825,"math_prob":0.8747791,"size":3605,"snap":"2019-51-2020-05","text_gpt3_token_len":1492,"char_repetition_ratio":0.18439323,"word_repetition_ratio":0.0,"special_character_ratio":0.18945909,"punctuation_ratio":0.2398374,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95898384,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-28T06:56:25Z\",\"WARC-Record-ID\":\"<urn:uuid:4b00e769-97d1-407e-9baf-3b0fe9ca649e>\",\"Content-Length\":\"15139\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2619143c-2218-439d-b416-87bb4d821128>\",\"WARC-Concurrent-To\":\"<urn:uuid:d25d94ca-502d-46c9-bd99-947f8f338f18>\",\"WARC-IP-Address\":\"118.31.180.41\",\"WARC-Target-URI\":\"https://www.cnblogs.com/ysjxw/archive/2008/05/12/1193735.html\",\"WARC-Payload-Digest\":\"sha1:LAUVK44TLTUFLM2FVBFEGIF2JXI4RTTE\",\"WARC-Block-Digest\":\"sha1:KCFBLAXIE63LMEELZPCWNH4RNOWHWOXN\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579251776516.99_warc_CC-MAIN-20200128060946-20200128090946-00525.warc.gz\"}"}