⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 maxentclassifier.java

📁 这是一个CRF(条件随机域)算法的实现,希望能对从事算法的有些帮助.
💻 JAVA
字号:
package iitb.MaxentClassifier;import java.util.*;import java.io.*;import iitb.CRF.*;import iitb.Utils.*;/** * * This class shows how to use the CRF package iitb.CRF for basic maxent  * classification where the features are provided as attributes of the  * instances to be classified. The number of classes can be more than two. * * @author Sunita Sarawagi * */ public class MaxentClassifier {    FeatureGenRecord featureGen;    CRF crfModel;    DataDesc dataDesc;    MaxentClassifier(Options opts) throws Exception {	dataDesc = new DataDesc(opts);	// read all parameters	featureGen = new FeatureGenRecord(dataDesc.numColumns, dataDesc.numLabels);	crfModel = new CRF(dataDesc.numLabels,featureGen,opts);    }    void train(String trainFile) throws IOException {	// read training data from the  given file.        double params[] = crfModel.train(new DataSet(FileData.read(trainFile,dataDesc)));        System.out.println("Trained model");        for (int i = 0; i < params.length; i++)            System.out.println(featureGen.featureName(i) + " " + params[i]);    }    void test(String testFile)  throws IOException {	FileData fData = new FileData();	fData.openForRead(testFile,dataDesc);	DataRecord dataRecord = new DataRecord(dataDesc.numColumns);	int confMat[][] = new int[dataDesc.numLabels][dataDesc.numLabels];	while (fData.readNext(dataRecord)) {	    int trueLabel = dataRecord.y();	    crfModel.apply(dataRecord);	    //	    System.out.println(trueLabel + " true:pred " + dataRecord.y());	    confMat[trueLabel][dataRecord.y()]++;	}	// output confusion matrix etc directly.	System.out.println("Confusion matrix ");	for(int i=0 ; i<dataDesc.numLabels ; i++) {	    System.out.print(i);	    for(int j=0 ; j<dataDesc.numLabels ; j++) {		System.out.print("\t"+confMat[i][j]);	    }	    System.out.println();	}    }    public static void main(String args[]) {	try {	    Options opts = new Options(args);	    MaxentClassifier maxent = new MaxentClassifier(opts);	    maxent.train(opts.getMandatoryProperty("trainFile"));	    System.out.println("Finished training...Starting test");	    maxent.test(opts.getMandatoryProperty("testFile"));	} catch (Exception e) {	    e.printStackTrace();	}    }};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -