本文整理汇总了Java中de.bwaldvogel.liblinear.Parameter类的典型用法代码示例。如果您正苦于以下问题:Java Parameter类的具体用法?Java Parameter怎么用?Java Parameter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Parameter类属于de.bwaldvogel.liblinear包,在下文中一共展示了Parameter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: learn
import de.bwaldvogel.liblinear.Parameter; //导入依赖的package包/类
/** Learns a new SVM model with the LibSVM package. */
@Override
public Model learn(ExampleSet exampleSet) throws OperatorException {
Parameter params = getParameters(exampleSet);
if (exampleSet.size() < 2) {
throw new UserError(this, 110, 2);
}
Linear.resetRandom();
Linear.disableDebugOutput();
Problem problem = getProblem(exampleSet);
de.bwaldvogel.liblinear.Model model = Linear.train(problem, params);
return new FastMarginModel(exampleSet, model, getParameterAsBoolean(PARAMETER_USE_BIAS));
}
示例2: getFeatureImportance
import de.bwaldvogel.liblinear.Parameter; //导入依赖的package包/类
/**
* @param gatherer
* @param features
* @return an array of feature IDs (>=1), ordered by feature importance, without zero-importance features.
*/
private static <T extends Serializable, G extends Serializable> int[] getFeatureImportance(ExampleGatherer<T, G> gatherer,
int[] features) {
ZScoreFeatureNormalizer scaleFn = ZScoreFeatureNormalizer.fromGatherer(gatherer);
Parameter param = new Parameter(SolverType.L2R_L2LOSS_SVR, 0.01, 0.001);
Problem problem = gatherer.generateLibLinearProblem(features, scaleFn);
Model m = Linear.train(problem, param);
double[] weights = m.getFeatureWeights();
int[] ftrImportance = Arrays.stream(features).boxed().sorted(new Comparator<Integer>() {
@Override
public int compare(Integer fId0, Integer fId1) {
return Double.compare(Math.abs(weights[ArrayUtils.indexOf(features, fId0)]), Math.abs(ArrayUtils.indexOf(features, fId1)));
}
}).filter(fId -> weights[ArrayUtils.indexOf(features, fId)] != 0.0).mapToInt(fId -> fId.intValue()).toArray();
return ftrImportance;
}
示例3: trainSvm
import de.bwaldvogel.liblinear.Parameter; //导入依赖的package包/类
/**
* Train SVM model. Return alpha and w matrix.
*
* */
public StoreAlphaWeight trainSvm(File saveModel) throws Exception{
StoreAlphaWeight saww=new StoreAlphaWeight();
this.modelFile=saveModel;
Problem problem=new Problem();
problem.l=train;
problem.n=dimensions;
problem.x=vectrain;
problem.y=trainattr;
SolverType s=SolverType.MCSVM_CS;
Parameter parameter = new Parameter(s, C, eps);
Model modelg = Linear.train(problem, parameter, saww);
try {
modelg.save(saveModel);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return saww;
}
示例4: train
import de.bwaldvogel.liblinear.Parameter; //导入依赖的package包/类
public void train(List<Pair<CounterInterface<Integer>,Integer>> trainSet) {
Problem problem = new Problem();
FeatureNode[][] x = new FeatureNode[trainSet.size()][];
double[] y = new double[trainSet.size()];
int maxFeature = 0;
for (int i=0; i<x.length; ++i) {
CounterInterface<Integer> features = trainSet.get(i).getFirst();
for (Map.Entry<Integer, Double> feat : features.entries()) {
maxFeature = Math.max(feat.getKey()+1, maxFeature);
}
x[i] = convertToFeatureNodes(features);
y[i] = trainSet.get(i).getSecond();
}
problem.l = trainSet.size();
problem.n = maxFeature;
problem.x = x;
problem.y = y;
problem.bias = 0.0;
Parameter parameter = new Parameter(solverType, C, eps);
model = Linear.train(problem, parameter);
}
示例5: train
import de.bwaldvogel.liblinear.Parameter; //导入依赖的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);
}
示例6: crossValidate
import de.bwaldvogel.liblinear.Parameter; //导入依赖的package包/类
private static Prediction[] crossValidate(Problem prob, Parameter linearParams, int folds) {
double[] prediction = new double[prob.l];
Linear.crossValidation(prob, linearParams, folds, prediction);
Prediction[] pred2 = new Prediction[prob.l];
for (int i = 0; i < pred2.length; i++) {
pred2[i] = new Prediction(prediction[i], i);
}
return pred2;
}
示例7: getParamsCopy
import de.bwaldvogel.liblinear.Parameter; //导入依赖的package包/类
public Parameter getParamsCopy() {
Parameter p2 = new Parameter(params.getSolverType(), params.getC(), params.getEps());
if (weights != null) {
p2.setWeights(params.getWeights(), params.getWeightLabels());
}
p2.setEps(params.getEps());
return p2;
}
示例8: MultiClassSVM
import de.bwaldvogel.liblinear.Parameter; //导入依赖的package包/类
/**
* Maximal feature index, not including the bias feature.
*/
// int max_index;
public MultiClassSVM(double C, double eps) {
super();
this.bias = 1;
this.C = C;
this.eps = eps;
features = null;
parameter = new Parameter(SolverType.MCSVM_CS, C, eps);
model = null;
}
示例9: doTrain
import de.bwaldvogel.liblinear.Parameter; //导入依赖的package包/类
@Override
public MaxentModel doTrain(DataIndexer indexer) throws IOException {
List<Double> vy = new ArrayList<Double>();
List<Feature[]> vx = new ArrayList<Feature[]>();
// outcomes
int outcomes[] = indexer.getOutcomeList();
int max_index = 0;
// For each event ...
for (int i = 0; i < indexer.getContexts().length; i++) {
int outcome = outcomes[i];
vy.add(Double.valueOf(outcome));
int features[] = indexer.getContexts()[i];
Feature[] x;
if (bias >= 0) {
x = new Feature[features.length + 1];
} else {
x = new Feature[features.length];
}
// for each feature ...
for (int fi = 0; fi < features.length; fi++) {
// TODO: SHOUDL BE indexer.getNumTimesEventsSeen()[i] and not fi !!!
x[fi] = new FeatureNode(features[fi] + 1, indexer.getNumTimesEventsSeen()[i]);
}
if (features.length > 0) {
max_index = Math.max(max_index, x[features.length - 1].getIndex());
}
vx.add(x);
}
Problem problem = constructProblem(vy, vx, max_index, bias);
Parameter parameter = new Parameter(solverType, c, eps, p);
Model liblinearModel = Linear.train(problem, parameter);
Map<String, Integer> predMap = new HashMap<String, Integer>();
String predLabels[] = indexer.getPredLabels();
for (int i = 0; i < predLabels.length; i++) {
predMap.put(predLabels[i], i);
}
return new LiblinearModel(liblinearModel, indexer.getOutcomeLabels(), predMap);
}
示例10: LibLinearClassifier
import de.bwaldvogel.liblinear.Parameter; //导入依赖的package包/类
public LibLinearClassifier() {
this(new Parameter(SolverType.L2R_LR, 1, 0.1));
}
示例11: Multiclass
import de.bwaldvogel.liblinear.Parameter; //导入依赖的package包/类
public Multiclass(SolverType solver, double C, double eps, double bias, boolean dense) {
parameter = new Parameter(solver, C, eps);
this.dense = dense;
this.bias = bias;
}
示例12: Multilabel
import de.bwaldvogel.liblinear.Parameter; //导入依赖的package包/类
public Multilabel(SolverType solver, double C, double eps, double bias, boolean dense) {
parameter = new Parameter(solver, C, eps);
this.dense = dense;
this.bias = bias;
}
示例13: LibLINEARParameters
import de.bwaldvogel.liblinear.Parameter; //导入依赖的package包/类
public LibLINEARParameters(int algorithm) {
this.algorithm = algorithm;
switch (algorithm) {
case SVC_DUAL: solver = SolverType.L2R_L2LOSS_SVC_DUAL;
evalFunction = new Accuracy();
break;
case SVC_PRIMAL: solver = SolverType.L2R_L2LOSS_SVC;
evalFunction = new Accuracy();
break;
case SVR_DUAL: solver = SolverType.L2R_L2LOSS_SVR_DUAL;
evalFunction = new MeanSquaredError();
break;
case SVR_PRIMAL: solver = SolverType.L2R_L2LOSS_SVR;
evalFunction = new MeanSquaredError();
break;
case LR_DUAL: solver = SolverType.L2R_LR_DUAL;
evalFunction = new Accuracy();
break;
case LR_PRIMAL: solver = SolverType.L2R_LR;
evalFunction = new Accuracy();
break;
default: solver = SolverType.L2R_L2LOSS_SVC_DUAL;
evalFunction = new Accuracy();
break;
}
verbose = VERBOSITY_DEFAULT;
bias = -1;
doCrossValidation = true;
doWeightLabels = false;
numFolds = 5;
splitFraction = (float) 0.7;
ps = new double[1];
ps[0] = 0.1;
cs = new double[1];
cs[0] = 1;
eps = 0.1;
params = new Parameter(solver, cs[0], eps);
}
示例14: getParams
import de.bwaldvogel.liblinear.Parameter; //导入依赖的package包/类
public Parameter getParams() {
return params;
}
示例15: LibLinearClassifier
import de.bwaldvogel.liblinear.Parameter; //导入依赖的package包/类
LibLinearClassifier() {
this.mParameter = new Parameter(SolverType.L2R_L2LOSS_SVC_DUAL, 1, Double.POSITIVE_INFINITY);
}