本文整理汇总了Java中de.bwaldvogel.liblinear.Linear.predict方法的典型用法代码示例。如果您正苦于以下问题:Java Linear.predict方法的具体用法?Java Linear.predict怎么用?Java Linear.predict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类de.bwaldvogel.liblinear.Linear
的用法示例。
在下文中一共展示了Linear.predict方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: evaluateSvm
import de.bwaldvogel.liblinear.Linear; //导入方法依赖的package包/类
public double[] evaluateSvm() throws Exception{
int right=0;
Model model = Model.load(modelFile);
for(int t=0;t<test;t++){
double prediction = Linear.predict(model, vectest[t]);
if(prediction==testattr[t]){
right++;
}
}
double precision=(double)right/test;
System.err.println("*************Precision = "+precision*100+"%*************");
double storeResult[]=new double[3];
storeResult[0]=right;
storeResult[1]=test;
storeResult[2]=precision;
return storeResult;
}
示例2: predictOne
import de.bwaldvogel.liblinear.Linear; //导入方法依赖的package包/类
public Matrix predictOne(Feature[] x) {
Matrix result = null;
if (model.isProbabilityModel()) {
double[] probabilities = new double[model.getNrClass()];
Linear.predictProbability(model, x, probabilities);
result = Matrix.Factory.zeros(1, model.getNrClass());
for (int i = 0; i < probabilities.length; i++) {
int label = model.getLabels()[i];
result.setAsDouble(probabilities[i], 0, label);
}
} else {
double classId = Linear.predict(model, x);
result = Matrix.Factory.zeros(1, Math.max(model.getNrClass(), (int) (classId + 1)));
result.setAsDouble(1.0, 0, (int) classId);
}
return result;
}
示例3: train
import de.bwaldvogel.liblinear.Linear; //导入方法依赖的package包/类
public static void train() throws IOException, InvalidInputDataException{
String file = "output\\svm/book_svm.svm";
Problem problem = Problem.readFromFile(new File(file),-1);
SolverType solver = SolverType.L2R_LR; // -s 0
double C = 1.0; // cost of constraints violation
double eps = 0.01; // stopping criteria
Parameter parameter = new Parameter(solver, C, eps);
Model model = Linear.train(problem, parameter);
File modelFile = new File("output/model");
model.save(modelFile);
System.out.println(modelFile.getAbsolutePath());
// load model or use it directly
model = Model.load(modelFile);
Feature[] instance = { new FeatureNode(1, 4), new FeatureNode(2, 2) };
double prediction = Linear.predict(model, instance);
System.out.println(prediction);
int nr_fold = 10;
double[] target = new double[problem.l];
Linear.crossValidation(problem, parameter, nr_fold, target);
}
示例4: predict2
import de.bwaldvogel.liblinear.Linear; //导入方法依赖的package包/类
@Deprecated
public static int[] predict2(Model model, Feature[][] data, int[] labels) {
int N = data.length;
int[] pre_label = new int[N];
for ( int i = 0; i < N; i ++ ) {
pre_label[i] = Linear.predict(model, data[i]);
}
if (labels != null) {
int cnt_correct = 0;
for ( int i = 0; i < N; i ++ ) {
if ( pre_label[i] == labels[i] )
cnt_correct ++;
}
double accuracy = (double)cnt_correct / (double)N;
System.out.println(String.format("Accuracy: %.2f%%\n", accuracy * 100));
}
return pre_label;
}
示例5: getLabel
import de.bwaldvogel.liblinear.Linear; //导入方法依赖的package包/类
@Override
public String getLabel(JCas cas) {
Vector<Feature[]> instanceFeatures = applyFeatures(cas, features);
Feature[] instance = combineInstanceFeatures(instanceFeatures);
probEstimates = new double[model.getNrClass()];
Double prediction;
if (model.getSolverType().isLogisticRegressionSolver()) {
prediction = Linear.predictProbability(model, instance, probEstimates);
score = probEstimates[prediction.intValue()];
} else {
prediction = Linear.predict(model, instance);
}
label = labelMappings.get(prediction);
return label;
}
示例6: performPrediction
import de.bwaldvogel.liblinear.Linear; //导入方法依赖的package包/类
@Override
public ExampleSet performPrediction(ExampleSet exampleSet, Attribute predictedLabel) throws OperatorException {
FastExample2SparseTransform ripper = new FastExample2SparseTransform(exampleSet);
Attribute label = getLabel();
Attribute[] confidenceAttributes = null;
if (label.isNominal() && label.getMapping().size() >= 2) {
confidenceAttributes = new Attribute[linearModel.label.length];
for (int j = 0; j < linearModel.label.length; j++) {
String labelName = label.getMapping().mapIndex(linearModel.label[j]);
confidenceAttributes[j] = exampleSet.getAttributes()
.getSpecial(Attributes.CONFIDENCE_NAME + "_" + labelName);
}
}
Iterator<Example> i = exampleSet.iterator();
while (i.hasNext()) {
Example e = i.next();
// set prediction
FeatureNode[] currentNodes = FastLargeMargin.makeNodes(e, ripper, this.useBias);
double predictedClass = Linear.predict(linearModel, currentNodes);
e.setValue(predictedLabel, predictedClass);
// use simple calculation for binary cases...
if (label.getMapping().size() == 2) {
double[] functionValues = new double[linearModel.nr_class];
Linear.predictValues(linearModel, currentNodes, functionValues);
double prediction = functionValues[0];
if (confidenceAttributes != null && confidenceAttributes.length > 0) {
e.setValue(confidenceAttributes[0], 1.0d / (1.0d + java.lang.Math.exp(-prediction)));
if (confidenceAttributes.length > 1) {
e.setValue(confidenceAttributes[1], 1.0d / (1.0d + java.lang.Math.exp(prediction)));
}
}
}
}
return exampleSet;
}
示例7: predictScore
import de.bwaldvogel.liblinear.Linear; //导入方法依赖的package包/类
public double predictScore(FeaturePack<T> fp, FeatureNormalizer fn) {
return Linear.predict(model, featureMapToFeatures(fn.ftrToNormalizedFtrArray(fp)));
}
示例8: classify
import de.bwaldvogel.liblinear.Linear; //导入方法依赖的package包/类
@Override
public Integer classify(SparseInstance instance) {
Feature[] feat = getFeatureArray(instance);
return (int) Linear.predict(model, feat);
}
示例9: performPrediction
import de.bwaldvogel.liblinear.Linear; //导入方法依赖的package包/类
@Override
public ExampleSet performPrediction(ExampleSet exampleSet, Attribute predictedLabel) throws OperatorException {
FastExample2SparseTransform ripper = new FastExample2SparseTransform(exampleSet);
Attribute label = getLabel();
Attribute[] confidenceAttributes = null;
if (label.isNominal() && label.getMapping().size() >= 2) {
confidenceAttributes = new Attribute[linearModel.label.length];
for (int j = 0; j < linearModel.label.length; j++) {
String labelName = label.getMapping().mapIndex(linearModel.label[j]);
confidenceAttributes[j] = exampleSet.getAttributes()
.getSpecial(Attributes.CONFIDENCE_NAME + "_" + labelName);
}
}
Iterator<Example> i = exampleSet.iterator();
OperatorProgress progress = null;
if (getShowProgress() && getOperator() != null && getOperator().getProgress() != null) {
progress = getOperator().getProgress();
progress.setTotal(exampleSet.size());
}
int progressCounter = 0;
while (i.hasNext()) {
Example e = i.next();
// set prediction
FeatureNode[] currentNodes = FastLargeMargin.makeNodes(e, ripper, this.useBias);
double predictedClass = Linear.predict(linearModel, currentNodes);
e.setValue(predictedLabel, predictedClass);
// use simple calculation for binary cases...
if (label.getMapping().size() == 2) {
double[] functionValues = new double[linearModel.nr_class];
Linear.predictValues(linearModel, currentNodes, functionValues);
double prediction = functionValues[0];
if (confidenceAttributes != null && confidenceAttributes.length > 0) {
e.setValue(confidenceAttributes[0], 1.0d / (1.0d + java.lang.Math.exp(-prediction)));
if (confidenceAttributes.length > 1) {
e.setValue(confidenceAttributes[1], 1.0d / (1.0d + java.lang.Math.exp(prediction)));
}
}
}
if (progress != null && ++progressCounter % OPERATOR_PROGRESS_STEPS == 0) {
progress.setCompleted(progressCounter);
}
}
return exampleSet;
}
示例10: predict
import de.bwaldvogel.liblinear.Linear; //导入方法依赖的package包/类
public Integer predict(CounterInterface<Integer> toPredict) {
return (int) Linear.predict(model, convertToFeatureNodes(toPredict));
}
示例11: computePred
import de.bwaldvogel.liblinear.Linear; //导入方法依赖的package包/类
private double computePred(FeatureNode[] f) {
double pred = 0;
//double[] prob_estimates = new double[2];
/*if (libLinearModel.isProbabilityModel()) {
prob_estimates = new double[2];
Linear.predictProbability(libLinearModel, f, prob_estimates);
pred = prob_estimates[0];
} else
pred += Linear.predict(libLinearModel, f);*/
pred = Linear.predict(libLinearModel, f);
return pred;
}
示例12: classify
import de.bwaldvogel.liblinear.Linear; //导入方法依赖的package包/类
@Override
public OUTCOME_TYPE classify(List<Feature> features) throws CleartkProcessingException {
FeatureNode[] encodedFeatures = this.featuresEncoder.encodeAll(features);
int encodedOutcome = (int)Linear.predict(this.model, encodedFeatures);
return this.outcomeEncoder.decode(encodedOutcome);
}
示例13: predict
import de.bwaldvogel.liblinear.Linear; //导入方法依赖的package包/类
public double predict(Feature[] instance, ModelType modelType)
{
return Linear.predict(modelType == ModelType.labeller ? labellerModel : identifierModel, instance);
}