Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for negative examples in one-class added #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -768,3 +768,10 @@ The authors thank their group members and users
for many helpful discussions and comments. They are listed in
http://www.csie.ntu.edu.tw/~cjlin/libsvm/acknowledgements



------------
Derived version (by Lukáš Ondráček):
Added support for learning from both positive and negative examples in one-class classification
according to the "One-class classification" thesis written by door David Martinus Johannes TAX.
Array y should now contain +1s for positive examples and -1s for negative ones.
21 changes: 15 additions & 6 deletions svm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1316,9 +1316,10 @@ class SVC_Q: public Kernel
class ONE_CLASS_Q: public Kernel
{
public:
ONE_CLASS_Q(const svm_problem& prob, const svm_parameter& param)
ONE_CLASS_Q(const svm_problem& prob, const svm_parameter& param, const schar *y_)
:Kernel(prob.l, prob.x, param)
{
clone(y,y_,prob.l);
cache = new Cache(prob.l,(long int)(param.cache_size*(1<<20)));
QD = new double[prob.l];
for(int i=0;i<prob.l;i++)
Expand All @@ -1332,7 +1333,7 @@ class ONE_CLASS_Q: public Kernel
if((start = cache->get_data(i,&data,len)) < len)
{
for(j=start;j<len;j++)
data[j] = (Qfloat)(this->*kernel_function)(i,j);
data[j] = (Qfloat)(y[i]*y[j]*(this->*kernel_function)(i,j));
}
return data;
}
Expand All @@ -1351,10 +1352,12 @@ class ONE_CLASS_Q: public Kernel

~ONE_CLASS_Q()
{
delete[] y;
delete cache;
delete[] QD;
}
private:
schar *y;
Cache *cache;
double *QD;
};
Expand Down Expand Up @@ -1533,7 +1536,7 @@ static void solve_one_class(
{
int l = prob->l;
double *zeros = new double[l];
schar *ones = new schar[l];
schar *y = new schar[l];
int i;

int n = (int)(param->nu*prob->l); // # of alpha's at upper bound
Expand All @@ -1548,15 +1551,21 @@ static void solve_one_class(
for(i=0;i<l;i++)
{
zeros[i] = 0;
ones[i] = 1;
if(prob->y[i]>0)
y[i] = +1;
else
y[i] = -1;
}

Solver s;
s.Solve(l, ONE_CLASS_Q(*prob,*param), zeros, ones,
s.Solve(l, ONE_CLASS_Q(*prob,*param,y), zeros, y,
alpha, 1.0, 1.0, param->eps, si, param->shrinking);

for(i=0; i<l; i++)
alpha[i] *= y[i];

delete[] zeros;
delete[] ones;
delete[] y;
}

static void solve_epsilon_svr(
Expand Down