• 大小: 89.98MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-05-15
  • 语言: Java
  • 标签: java  机器学习  

资源简介

Java机器学习实例源码,一共有10个源码示例,机器学习入门。

资源截图

代码片段和文件信息

import java.util.ArrayList;

import cc.mallet.types.*;
import cc.mallet.classify.Classifier;
import cc.mallet.classify.ClassifierTrainer;
import cc.mallet.classify.NaiveBayesTrainer;
import cc.mallet.classify.Trial;
import cc.mallet.pipe.*;
import cc.mallet.pipe.iterator.*;

import java.util.regex.*;
import java.io.*;

public class SpamDetector {

public static void main(String[] args){

     String stopListFilePath = “data/stoplists/en.txt“;
     String dataFolderPath = “data/ex6DataEmails/train“;
     String testFolderPath = “data/ex6DataEmails/test“;
    
ArrayList pipeList = new ArrayList();
pipeList.add(new Input2CharSequence(“UTF-8“));
Pattern tokenPattern = Pattern.compile(“[\\p{L}\\p{N}_]+“);
pipeList.add(new CharSequence2TokenSequence(tokenPattern));
pipeList.add(new TokenSequenceLowercase());
pipeList.add(new TokenSequenceRemoveStopwords(new File(stopListFilePath) “utf-8“ false false false));
pipeList.add(new TokenSequence2FeatureSequence());
pipeList.add(new FeatureSequence2FeatureVector());
pipeList.add(new Target2Label());
SerialPipes pipeline = new SerialPipes(pipeList);

FileIterator folderIterator = new FileIterator(
new File[] {new File(dataFolderPath)}
         new TxtFilter()
         FileIterator.LAST_DIRECTORY);


InstanceList instances = new InstanceList(pipeline);

instances.addThruPipe(folderIterator);

ClassifierTrainer classifierTrainer = new NaiveBayesTrainer();
Classifier classifier = classifierTrainer.train(instances);

InstanceList testInstances = new InstanceList(classifier.getInstancePipe());
folderIterator = new FileIterator(
new File[] {new File(testFolderPath)}
         new TxtFilter()
         FileIterator.LAST_DIRECTORY);
        testInstances.addThruPipe(folderIterator);
        
        Trial trial = new Trial(classifier testInstances);
        
        System.out.println(“Accuracy: “ + trial.getAccuracy());
        System.out.println(“F1 for class ‘spam‘: “ + trial.getF1(“spam“));

        System.out.println(“Precision for class ‘“ +
                           classifier.getLabelAlphabet().lookupLabel(1) + “‘: “ +
                           trial.getPrecision(1));

        System.out.println(“Recall for class ‘“ +
                           classifier.getLabelAlphabet().lookupLabel(1) + “‘: “ +
                           trial.getRecall(1));




}
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件      126894  2016-04-26 13:55  MachineLearninginJava_Code\B03686_Software list.pdf
     目录           0  2016-04-26 14:00  MachineLearninginJava_Code\Code\
     文件         417  2016-04-03 07:21  MachineLearninginJava_Code\Code\.gitignore
     目录           0  2016-04-26 13:44  MachineLearninginJava_Code\Code\MLJ-Chapter10\
     文件         402  2016-04-03 07:21  MachineLearninginJava_Code\Code\MLJ-Chapter10\.classpath
     文件         372  2016-04-03 07:21  MachineLearninginJava_Code\Code\MLJ-Chapter10\.project
     目录           0  2016-04-26 13:44  MachineLearninginJava_Code\Code\MLJ-Chapter10\data\
     目录           0  2016-04-26 13:44  MachineLearninginJava_Code\Code\MLJ-Chapter10\data\bbc\
     目录           0  2016-04-26 13:43  MachineLearninginJava_Code\Code\MLJ-Chapter10\data\bbc\business\
     文件        2560  2016-04-03 07:21  MachineLearninginJava_Code\Code\MLJ-Chapter10\data\bbc\business\001.txt
     文件        2252  2016-04-03 07:21  MachineLearninginJava_Code\Code\MLJ-Chapter10\data\bbc\business\002.txt
     文件        1552  2016-04-03 07:21  MachineLearninginJava_Code\Code\MLJ-Chapter10\data\bbc\business\003.txt
     文件        2412  2016-04-03 07:21  MachineLearninginJava_Code\Code\MLJ-Chapter10\data\bbc\business\004.txt
     文件        1570  2016-04-03 07:21  MachineLearninginJava_Code\Code\MLJ-Chapter10\data\bbc\business\005.txt
     文件        1187  2016-04-03 07:21  MachineLearninginJava_Code\Code\MLJ-Chapter10\data\bbc\business\006.txt
     文件        1669  2016-04-03 07:21  MachineLearninginJava_Code\Code\MLJ-Chapter10\data\bbc\business\007.txt
     文件        1922  2016-04-03 07:21  MachineLearninginJava_Code\Code\MLJ-Chapter10\data\bbc\business\008.txt
     文件        1494  2016-04-03 07:21  MachineLearninginJava_Code\Code\MLJ-Chapter10\data\bbc\business\009.txt
     文件        1449  2016-04-03 07:21  MachineLearninginJava_Code\Code\MLJ-Chapter10\data\bbc\business\010.txt
     文件        1144  2016-04-03 07:21  MachineLearninginJava_Code\Code\MLJ-Chapter10\data\bbc\business\011.txt
     文件        1847  2016-04-03 07:21  MachineLearninginJava_Code\Code\MLJ-Chapter10\data\bbc\business\012.txt
     文件        1830  2016-04-03 07:21  MachineLearninginJava_Code\Code\MLJ-Chapter10\data\bbc\business\013.txt
     文件        2981  2016-04-03 07:21  MachineLearninginJava_Code\Code\MLJ-Chapter10\data\bbc\business\014.txt
     文件        3808  2016-04-03 07:21  MachineLearninginJava_Code\Code\MLJ-Chapter10\data\bbc\business\015.txt
     文件        1393  2016-04-03 07:21  MachineLearninginJava_Code\Code\MLJ-Chapter10\data\bbc\business\016.txt
     文件        1299  2016-04-03 07:21  MachineLearninginJava_Code\Code\MLJ-Chapter10\data\bbc\business\017.txt
     文件        1002  2016-04-03 07:21  MachineLearninginJava_Code\Code\MLJ-Chapter10\data\bbc\business\018.txt
     文件        1733  2016-04-03 07:21  MachineLearninginJava_Code\Code\MLJ-Chapter10\data\bbc\business\019.txt
     文件        3854  2016-04-03 07:21  MachineLearninginJava_Code\Code\MLJ-Chapter10\data\bbc\business\020.txt
     文件        2046  2016-04-03 07:21  MachineLearninginJava_Code\Code\MLJ-Chapter10\data\bbc\business\021.txt
     文件        1933  2016-04-03 07:21  MachineLearninginJava_Code\Code\MLJ-Chapter10\data\bbc\business\022.txt
............此处省略3343个文件信息

评论

共有 条评论