本文整理汇总了Java中weka.classifiers.AbstractClassifier.makeCopies方法的典型用法代码示例。如果您正苦于以下问题:Java AbstractClassifier.makeCopies方法的具体用法?Java AbstractClassifier.makeCopies怎么用?Java AbstractClassifier.makeCopies使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类weka.classifiers.AbstractClassifier
的用法示例。
在下文中一共展示了AbstractClassifier.makeCopies方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: crossValidate
import weka.classifiers.AbstractClassifier; //导入方法依赖的package包/类
/**
* Utility method for fast 5-fold cross validation of a naive bayes
* model
*
* @param fullModel a <code>NaiveBayesUpdateable</code> value
* @param trainingSet an <code>Instances</code> value
* @param r a <code>Random</code> value
* @return a <code>double</code> value
* @exception Exception if an error occurs
*/
public static double crossValidate(NaiveBayesUpdateable fullModel,
Instances trainingSet,
Random r) throws Exception {
// make some copies for fast evaluation of 5-fold xval
Classifier [] copies = AbstractClassifier.makeCopies(fullModel, 5);
Evaluation eval = new Evaluation(trainingSet);
// make some splits
for (int j = 0; j < 5; j++) {
Instances test = trainingSet.testCV(5, j);
// unlearn these test instances
for (int k = 0; k < test.numInstances(); k++) {
test.instance(k).setWeight(-test.instance(k).weight());
((NaiveBayesUpdateable)copies[j]).updateClassifier(test.instance(k));
// reset the weight back to its original value
test.instance(k).setWeight(-test.instance(k).weight());
}
eval.evaluateModel(copies[j], test);
}
return eval.incorrect();
}
示例2: buildClassifier
import weka.classifiers.AbstractClassifier; //导入方法依赖的package包/类
/**
* Builds the classifiers.
*
* @param insts the training data.
* @throws Exception if a classifier can't be built
*/
public void buildClassifier(Instances insts) throws Exception {
Instances newInsts;
// can classifier handle the data?
getCapabilities().testWithFail(insts);
// remove instances with missing class
insts = new Instances(insts);
insts.deleteWithMissingClass();
m_Classifiers = AbstractClassifier.makeCopies(m_Classifier, insts.numClasses());
m_ClassFilters = new MakeIndicator[insts.numClasses()];
for (int i = 0; i < insts.numClasses(); i++) {
m_ClassFilters[i] = new MakeIndicator();
m_ClassFilters[i].setAttributeIndex("" + (insts.classIndex() + 1));
m_ClassFilters[i].setValueIndex(i);
m_ClassFilters[i].setNumeric(true);
m_ClassFilters[i].setInputFormat(insts);
newInsts = Filter.useFilter(insts, m_ClassFilters[i]);
m_Classifiers[i].buildClassifier(newInsts);
}
}
示例3: buildClassifier
import weka.classifiers.AbstractClassifier; //导入方法依赖的package包/类
@Override
public void buildClassifier(Instances D) throws Exception {
testCapabilities(D);
int L = D.classIndex();
if(getDebug()) System.out.print("Creating "+L+" models ("+m_Classifier.getClass().getName()+"): ");
m_MultiClassifiers = AbstractClassifier.makeCopies(m_Classifier,L);
m_Templates = new Instances[L];
for(int j = 0; j < L; j++) {
//Select only class attribute 'j'
m_Templates[j] = MLUtils.keepAttributesAt(new Instances(D),new int[]{j},L);
m_Templates[j].setClassIndex(0);
//Build the classifier for that class
m_MultiClassifiers[j].buildClassifier(m_Templates[j]);
if(getDebug()) System.out.print(" " + (m_Templates[j].classAttribute().name()));
m_Templates[j] = new Instances(m_Templates[j], 0);
}
}
示例4: buildClassifier
import weka.classifiers.AbstractClassifier; //导入方法依赖的package包/类
@Override
public void buildClassifier(Instances D) throws Exception {
testCapabilities(D);
int L = D.classIndex();
if(getDebug()) System.out.print("Creating "+L+" models ("+m_Classifier.getClass().getName()+"): ");
m_MultiClassifiers = AbstractClassifier.makeCopies(m_Classifier,L);
m_InstancesTemplates = new Instances[L];
for(int j = 0; j < L; j++) {
//Select only class attribute 'j'
Instances D_j = MLUtils.keepAttributesAt(new Instances(D),new int[]{j},L);
D_j.setClassIndex(0);
//Build the classifier for that class
m_MultiClassifiers[j].buildClassifier(D_j);
if(getDebug()) System.out.print(" " + (D_j.classAttribute().name()));
m_InstancesTemplates[j] = new Instances(D_j, 0);
}
}
示例5: buildClassifier
import weka.classifiers.AbstractClassifier; //导入方法依赖的package包/类
@Override
public void buildClassifier(Instances D) throws Exception {
testCapabilities(D);
int L = D.classIndex();
Random r = new Random(m_S);
if (getDebug())
System.out.println("Building "+m_M+" models of "+m_K+" random subsets:");
m_InstancesTemplates = new Instances[m_M];
kMap = new int[m_M][m_K];
m_Classifiers = AbstractClassifier.makeCopies(m_Classifier,m_M);
for(int i = 0; i < m_M; i++) {
kMap[i] = SuperLabelUtils.get_k_subset(L,m_K,r);
if (getDebug())
System.out.println("\tmodel "+(i+1)+"/"+m_M+": "+Arrays.toString(kMap[i])+", P="+m_P+", N="+m_N);
Instances D_i = SuperLabelUtils.makePartitionDataset(D,kMap[i],m_P,m_N);
m_Classifiers[i].buildClassifier(D_i);
m_InstancesTemplates[i] = new Instances(D_i,0);
}
}
示例6: buildClassifier
import weka.classifiers.AbstractClassifier; //导入方法依赖的package包/类
@Override
public void buildClassifier(Instances D) throws Exception {
int L = D.classIndex();
int N = D.numInstances();
// Get partition from dataset hierarchy
kMap = SuperLabelUtils.getPartitionFromDatasetHierarchy(D);
m_M = kMap.length;
m_Classifiers = AbstractClassifier.makeCopies(m_Classifier,m_M);
m_InstancesTemplates = new Instances[m_M];
for(int i = 0; i < m_M; i++) {
if (getDebug())
System.out.println("Building model "+(i+1)+"/"+m_M+": "+Arrays.toString(kMap[i]));
Instances D_i = SuperLabelUtils.makePartitionDataset(D,kMap[i]);
m_Classifiers[i].buildClassifier(D_i);
m_InstancesTemplates[i] = new Instances(D_i,0);
}
}
示例7: buildClassifier
import weka.classifiers.AbstractClassifier; //导入方法依赖的package包/类
/**
* Builds the committee of randomizable classifiers.
*
* @param data the training data to be used for generating the
* bagged classifier.
* @exception Exception if the classifier could not be built successfully
*/
public void buildClassifier(Instances data) throws Exception {
// can classifier handle the data?
getCapabilities().testWithFail(data);
// get fresh instances
m_data = new Instances(data);
super.buildClassifier(m_data);
if (!(m_Classifier instanceof Randomizable)) {
throw new IllegalArgumentException("Base learner must implement Randomizable!");
}
m_Classifiers = AbstractClassifier.makeCopies(m_Classifier, m_NumIterations);
Random random = m_data.getRandomNumberGenerator(m_Seed);
// Resample data based on weights if base learner can't handle weights
if (!(m_Classifier instanceof WeightedInstancesHandler)) {
m_data = m_data.resampleWithWeights(random);
}
for (int j = 0; j < m_Classifiers.length; j++) {
// Set the random number seed for the current classifier.
((Randomizable) m_Classifiers[j]).setSeed(random.nextInt());
// Build the classifier.
// m_Classifiers[j].buildClassifier(m_data);
}
buildClassifiers();
// save memory
m_data = null;
}
示例8: setClassifier
import weka.classifiers.AbstractClassifier; //导入方法依赖的package包/类
/**
* Set the base learner. Creates copies of the given classifier for the two
* classifiers used internally. <br/>
* Note: also unsets the flag whether the classifier has been built so far.
*
* @param newClassifier the classifier to use.
* @see #m_ClassifierBuilt
*/
public void setClassifier(Classifier newClassifier) {
super.setClassifier(newClassifier);
try {
m_Classifier2 = AbstractClassifier.makeCopies(newClassifier, 1)[0];
}
catch (Exception e) {
e.printStackTrace();
m_Classifier = new weka.classifiers.trees.J48();
m_Classifier2 = new weka.classifiers.trees.J48();
}
}
示例9: buildClassifier
import weka.classifiers.AbstractClassifier; //导入方法依赖的package包/类
/**
* Builds the committee of randomizable classifiers.
*
* @param data the training data to be used for generating the
* bagged classifier.
* @exception Exception if the classifier could not be built successfully
*/
public void buildClassifier(Instances data) throws Exception {
// can classifier handle the data?
getCapabilities().testWithFail(data);
// remove instances with missing class
m_data = new Instances(data);
m_data.deleteWithMissingClass();
super.buildClassifier(m_data);
if (!(m_Classifier instanceof Randomizable)) {
throw new IllegalArgumentException("Base learner must implement Randomizable!");
}
m_Classifiers = AbstractClassifier.makeCopies(m_Classifier, m_NumIterations);
Random random = m_data.getRandomNumberGenerator(m_Seed);
// Resample data based on weights if base learner can't handle weights
if (!(m_Classifier instanceof WeightedInstancesHandler)) {
m_data = m_data.resampleWithWeights(random);
}
for (int j = 0; j < m_Classifiers.length; j++) {
// Set the random number seed for the current classifier.
((Randomizable) m_Classifiers[j]).setSeed(random.nextInt());
// Build the classifier.
// m_Classifiers[j].buildClassifier(m_data);
}
buildClassifiers();
// save memory
m_data = null;
}
示例10: buildClassifier
import weka.classifiers.AbstractClassifier; //导入方法依赖的package包/类
@Override
public void buildClassifier(Instances D) throws Exception {
int L = D.classIndex();
int N = D.numInstances();
Random r = new Random(m_S);
// Note: a slightly round-about way of doing it:
int num = (int)Math.ceil(L / m_K);
kMap = SuperLabelUtils.generatePartition(MLUtils.gen_indices(L),num,r,true);
m_M = kMap.length;
m_Classifiers = AbstractClassifier.makeCopies(m_Classifier,m_M);
m_InstancesTemplates = new Instances[m_M];
if (getDebug())
System.out.println("Building "+m_M+" models of "+m_K+" partitions:");
for(int i = 0; i < m_M; i++) {
if (getDebug())
System.out.println("\tpartitioning model "+(i+1)+"/"+m_M+": "+Arrays.toString(kMap[i])+", P="+m_P+", N="+m_N);
Instances D_i = SuperLabelUtils.makePartitionDataset(D,kMap[i],m_P,m_N);
if (getDebug())
System.out.println("\tbuilding model "+(i+1)+"/"+m_M+": "+Arrays.toString(kMap[i]));
m_Classifiers[i].buildClassifier(D_i);
m_InstancesTemplates[i] = new Instances(D_i,0);
//int L_i = kMap[i].length;
//int remv[] = MLUtils.invert(kMap[i],L); // i.e., remove the rest < L
//Instances D_i = F.remove(new Instances(D),remv,false);
//System.out.println(""+D_i);
//D_i.setClassIndex(L_i);
//m_Classifiers[i].buildClassifier(D_i);
//D_i.clear();
//m_InstancesTemplates[i] = D_i;
}
}
示例11: buildClassifier
import weka.classifiers.AbstractClassifier; //导入方法依赖的package包/类
@Override
public void buildClassifier(Instances data) throws Exception {
testCapabilities(data);
int c = data.classIndex();
if(getDebug()) System.out.print("-: Creating "+c+" models ("+m_Classifier.getClass().getName()+"): ");
m_MultiClassifiers = AbstractClassifier.makeCopies(m_Classifier,c);
Instances sub_data = null;
for(int i = 0; i < c; i++) {
int indices[][] = new int[c][c - 1];
for(int j = 0, k = 0; j < c; j++) {
if(j != i) {
indices[i][k++] = j;
}
}
//Select only class attribute 'i'
Remove FilterRemove = new Remove();
FilterRemove.setAttributeIndicesArray(indices[i]);
FilterRemove.setInputFormat(data);
FilterRemove.setInvertSelection(true);
sub_data = Filter.useFilter(data, FilterRemove);
sub_data.setClassIndex(0);
/* BEGIN downsample for this link */
sub_data.randomize(m_Random);
int numToRemove = sub_data.numInstances() - (int)Math.round(sub_data.numInstances() * m_DownSampleRatio);
for(int m = 0, removed = 0; m < sub_data.numInstances(); m++) {
if (sub_data.instance(m).classValue() <= 0.0) {
sub_data.instance(m).setClassMissing();
if (++removed >= numToRemove)
break;
}
}
sub_data.deleteWithMissingClass();
/* END downsample for this link */
//Build the classifier for that class
m_MultiClassifiers[i].buildClassifier(sub_data);
if(getDebug()) System.out.print(" " + (i+1));
}
if(getDebug()) System.out.println(" :-");
m_InstancesTemplate = new Instances(sub_data, 0);
}