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;
}
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;
}
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;
}
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;
}
Aggregations