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

📄 abstractlinearclassifierfactory.java

📁 Standord Classifier实现了一个基于Java的最大熵分类器。用于模式识别
💻 JAVA
字号:
package edu.stanford.nlp.classify;import java.util.*;import java.lang.ref.*;import edu.stanford.nlp.util.*;import edu.stanford.nlp.dbm.*;/** @author Dan Klein */public abstract class AbstractLinearClassifierFactory {  Index labelIndex = new Index();  Index featureIndex = new Index();  int numFeatures() {    return featureIndex.size();  }  int numClasses() {    return labelIndex.size();  }  int[] toLabels(List examples) {    int[] labels = new int[examples.size()];    int j=0;    for (Iterator i=examples.iterator(); i.hasNext();) {      Datum d = (Datum)i.next();      labels[j] = labelIndex.indexOf(d.label());      j++;    }    return labels;  }  int[][] toData(List examples) {    int[][] data = new int[examples.size()][];    int j=0;    for (Iterator i=examples.iterator(); i.hasNext();) {      Datum d = (Datum)i.next();      List l = new ArrayList();      for (Iterator fI=d.asFeatures().iterator(); fI.hasNext();) {	int f = featureIndex.indexOf(fI.next());	if (f >= 0) {	  l.add(new Integer(f));	}      }      data[j] = new int[l.size()];      for (int fN=0; fN<l.size(); fN++) {	int f = ((Integer)l.get(fN)).intValue();	data[j][fN] = f;      }      j++;    }    return data;  }  Counter toCounter(double[][] weights) {    Counter counter = new Counter();    for (int f=0; f<weights.length; f++) {      for (int c=0; c<weights[0].length; c++) {        Pair p = new Pair(featureIndex.get(f), labelIndex.get(c));        counter.setCount(p, weights[f][c]);      }    }       return counter;  }  void index(List examples) {    for (int i=0; i<examples.size(); i++) {      Datum e = (Datum)examples.get(i);      labelIndex.add(e.label());      featureIndex.addAll(e.asFeatures());    }  }  abstract double[][] trainWeights(int[][] data, int[] labels);  public Classifier trainClassifier(List examples) {    return trainClassifier(new WeakReference(examples));  }  public Classifier trainClassifier(Reference ref) {    List examples = (List)ref.get();    index(examples);    int[] labels = toLabels(examples);    int[][] data = toData(examples);    examples = null;    System.err.println("NumFeatures: "+featureIndex.size());    System.err.println("NumClasses: "+labelIndex.size());    double[][] weights = trainWeights(data, labels);    Counter counter = toCounter(weights);    return new LinearClassifier(counter);  }}

⌨️ 快捷键说明

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