Search in sources :

Example 11 with Feature

use of Classifier.supervised.liblinear.Feature in project IR_Base by Linda-sunshine.

the class GlobalSVM method train.

@Override
public double train() {
    init();
    // Transfer all user reviews to instances recognized by SVM, indexed by users.
    int trainSize = 0, validUserIndex = 0;
    ArrayList<Feature[]> fvs = new ArrayList<Feature[]>();
    ArrayList<Double> ys = new ArrayList<Double>();
    // Two for loop to access the reviews, indexed by users.
    ArrayList<_Review> reviews;
    for (_AdaptStruct user : m_userList) {
        reviews = user.getReviews();
        boolean validUser = false;
        for (_Review r : reviews) {
            if (r.getType() == rType.ADAPTATION) {
                // we will only use the adaptation data for this purpose
                fvs.add(createLibLinearFV(r, validUserIndex));
                ys.add(new Double(r.getYLabel()));
                trainSize++;
                validUser = true;
            }
        }
        if (validUser)
            validUserIndex++;
    }
    // Train individual model for each user.
    Problem libProblem = new Problem();
    libProblem.l = trainSize;
    libProblem.x = new Feature[trainSize][];
    libProblem.y = new double[trainSize];
    for (int i = 0; i < trainSize; i++) {
        libProblem.x[i] = fvs.get(i);
        libProblem.y[i] = ys.get(i);
    }
    if (m_bias) {
        // including bias term; global model + user models
        libProblem.n = m_featureSize + 1;
        // bias term in liblinear.
        libProblem.bias = 1;
    } else {
        libProblem.n = m_featureSize;
        // no bias term in liblinear.
        libProblem.bias = -1;
    }
    m_libModel = Linear.train(libProblem, new Parameter(m_solverType, m_C, SVM.EPS));
    setPersonalizedModel();
    return 0;
}
Also used : structures._Review(structures._Review) Classifier.supervised.modelAdaptation._AdaptStruct(Classifier.supervised.modelAdaptation._AdaptStruct) ArrayList(java.util.ArrayList) Parameter(Classifier.supervised.liblinear.Parameter) Problem(Classifier.supervised.liblinear.Problem) Feature(Classifier.supervised.liblinear.Feature)

Example 12 with Feature

use of Classifier.supervised.liblinear.Feature in project IR_Base by Linda-sunshine.

the class IndividualSVM method createLibLinearFV.

public Feature[] createLibLinearFV(_Review r, int userIndex) {
    int fIndex;
    double fValue;
    _SparseFeature fv;
    _SparseFeature[] fvs = r.getSparse();
    Feature[] node;
    if (m_bias)
        node = new Feature[fvs.length + 1];
    else
        node = new Feature[fvs.length];
    for (int i = 0; i < fvs.length; i++) {
        fv = fvs[i];
        // liblinear's feature index starts from one
        fIndex = fv.getIndex() + 1;
        fValue = fv.getValue();
        // Construct the user part of the training instance.
        node[i] = new FeatureNode(fIndex, fValue);
    }
    if (// add the bias term
    m_bias)
        // user model's bias
        node[fvs.length] = new FeatureNode(m_featureSize + 1, 1.0);
    return node;
}
Also used : FeatureNode(Classifier.supervised.liblinear.FeatureNode) structures._SparseFeature(structures._SparseFeature) Feature(Classifier.supervised.liblinear.Feature) structures._SparseFeature(structures._SparseFeature)

Example 13 with Feature

use of Classifier.supervised.liblinear.Feature in project IR_Base by Linda-sunshine.

the class Utils method createLibLinearFV.

public static Feature[] createLibLinearFV(HashMap<Integer, Double> spVct) {
    Feature[] node = new Feature[spVct.size()];
    int fid = 0;
    for (_SparseFeature fv : createSpVct(spVct)) // svm's feature index starts from 1
    node[fid++] = new FeatureNode(1 + fv.getIndex(), fv.getValue());
    return node;
}
Also used : FeatureNode(Classifier.supervised.liblinear.FeatureNode) structures._SparseFeature(structures._SparseFeature) Feature(Classifier.supervised.liblinear.Feature) structures._SparseFeature(structures._SparseFeature)

Example 14 with Feature

use of Classifier.supervised.liblinear.Feature in project IR_Base by Linda-sunshine.

the class Utils method createLibLinearFV.

public static Feature[] createLibLinearFV(_SparseFeature[] spVct, int fSize) {
    Feature[] node;
    if (// include bias term in the end
    fSize > 0)
        node = new Feature[1 + spVct.length];
    else
        // ignore bias term
        node = new Feature[spVct.length];
    int fid = 0;
    for (_SparseFeature fv : spVct) // svm's feature index starts from 1
    node[fid++] = new FeatureNode(1 + fv.getIndex(), fv.getValue());
    if (fSize > 0)
        node[fid] = new FeatureNode(1 + fSize, 1.0);
    return node;
}
Also used : FeatureNode(Classifier.supervised.liblinear.FeatureNode) structures._SparseFeature(structures._SparseFeature) Feature(Classifier.supervised.liblinear.Feature) structures._SparseFeature(structures._SparseFeature)

Aggregations

Feature (Classifier.supervised.liblinear.Feature)14 structures._SparseFeature (structures._SparseFeature)10 FeatureNode (Classifier.supervised.liblinear.FeatureNode)7 Parameter (Classifier.supervised.liblinear.Parameter)6 Problem (Classifier.supervised.liblinear.Problem)6 ArrayList (java.util.ArrayList)6 structures._Review (structures._Review)3 Model (Classifier.supervised.liblinear.Model)2 SolverType (Classifier.supervised.liblinear.SolverType)2 Classifier.supervised.modelAdaptation._AdaptStruct (Classifier.supervised.modelAdaptation._AdaptStruct)2 structures._Doc (structures._Doc)2 LambdaRank (Ranker.LambdaRank)1 LambdaRankParallel (Ranker.LambdaRankParallel)1 RankNet (Ranker.RankNet)1 File (java.io.File)1 structures._ChildDoc (structures._ChildDoc)1 structures._Query (structures._Query)1 structures._Word (structures._Word)1