Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Commit

Permalink
Adding Nu-SVMs based on LibSVM's quadratic programming solver.
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarsouza committed Apr 18, 2015
1 parent 813fc86 commit 51914a8
Show file tree
Hide file tree
Showing 9 changed files with 1,208 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
<Compile Include="VectorMachines\MulticlassSupportVectorMachine.cs" />
<Compile Include="VectorMachines\Learning\MulticlassSupportVectorLearning.cs" />
<Compile Include="VectorMachines\Learning\Probabilistic\ProbabilisticOutputCalibration.cs" />
<Compile Include="VectorMachines\Learning\OneclassSupportVectorLearning.cs" />
<Compile Include="VectorMachines\SupportVectorMachine.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion Sources/Accord.MachineLearning/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Scope = "member", Target = "Accord.MachineLearning.VectorMachines.SupportVectorMachine.#FromLogisticRegression(Accord.Statistics.Models.Regression.LogisticRegression)")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Scope = "member", Target = "Accord.MachineLearning.DecisionTrees.DecisionTree.#GetHeight()")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Scope = "member", Target = "Accord.MachineLearning.DecisionTrees.DecisionNode.#GetHeight()")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix", Scope = "type", Target = "Accord.MachineLearning.DecisionTrees.DecisionNode")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix", Scope = "type", Target = "Accord.MachineLearning.DecisionTrees.DecisionNode")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Nu", Scope = "member", Target = "Accord.MachineLearning.VectorMachines.Learning.OneclassSupportVectorLearning.#Nu")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
// Accord Machine Learning Library
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright © César Souza, 2009-2015
// cesarsouza at gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//

namespace Accord.MachineLearning.VectorMachines.Learning
{
using Accord.Math.Optimization;
using Accord.Statistics.Kernels;
using System;

/// <summary>
/// One-class Support Vector Machine Learning Algorithm.
/// </summary>
///
public class OneclassSupportVectorLearning : ISupportVectorMachineLearning
{
SupportVectorMachine machine;

private double[][] inputs;
private double[] alpha;

private double nu = 0.5;

IKernel kernel;
readonly double[] zeros;
readonly int[] ones;

double eps = 0.001;
bool shrinking = true;


/// <summary>
/// Constructs a new one-class support vector learning algorithm.
/// </summary>
///
/// <param name="machine">A support vector machine.</param>
/// <param name="inputs">The input data points as row vectors.</param>
///
public OneclassSupportVectorLearning(SupportVectorMachine machine, double[][] inputs)
{
// Initial argument checking
if (machine == null)
throw new ArgumentNullException("machine");

if (inputs == null)
throw new ArgumentNullException("inputs");

this.inputs = inputs;
this.machine = machine;

this.zeros = new double[inputs.Length];
this.ones = new int[inputs.Length];
this.alpha = new double[inputs.Length];

for (int i = 0; i < alpha.Length; i++)
alpha[i] = 1;

for (int i = 0; i < ones.Length; i++)
ones[i] = 1;


// Kernel (if applicable)
var ksvm = machine as KernelSupportVectorMachine;

if (ksvm == null)
{
kernel = new Linear(0);
}
else
{
kernel = ksvm.Kernel;
}
}

/// <summary>
/// Gets the value for the Lagrange multipliers
/// (alpha) for every observation vector.
/// </summary>
///
public double[] Lagrange { get { return alpha; } }

/// <summary>
/// Convergence tolerance. Default value is 1e-2.
/// </summary>
///
/// <remarks>
/// The criterion for completing the model training process. The default is 0.01.
/// </remarks>
///
public double Tolerance
{
get { return eps; }
set { eps = value; }
}

/// <summary>
/// Gets or sets a value indicating whether to use
/// shrinking heuristics during learning. Default is true.
/// </summary>
///
/// <value>
/// <c>true</c> to use shrinking; otherwise, <c>false</c>.
/// </value>
///
public bool Shrinking
{
get { return shrinking; }
set { shrinking = value; }
}

/// <summary>
/// Controls the number of outliers accepted by the algorithm. This
/// value provides an upper bound on the fraction of training errors
/// and a lower bound of the fraction of support vectors. Default is 0.5
/// </summary>
///
/// <remarks>
/// The summary description is given in Chang and Lin,
/// "LIBSVM: A Library for Support Vector Machines", 2013.
/// </remarks>
///
public double Nu
{
get { return nu; }
set { nu = value; }
}

/// <summary>
/// Runs the learning algorithm.
/// </summary>
///
/// <param name="computeError">True to compute error after the training
/// process completes, false otherwise.</param>
/// <returns>
/// The misclassification error rate of the resulting support
/// vector machine if <paramref name="computeError" /> is true,
/// returns zero otherwise.
/// </returns>
///
public double Run(bool computeError)
{
int l = inputs.Length;
int n = (int)(nu * l); // # of alpha's at upper bound

for (int i = 0; i < n; i++)
alpha[i] = 1;

if (n < inputs.Length)
alpha[n] = nu * l - n;

for (int i = n + 1; i < l; i++)
alpha[i] = 0;


var s = new FanChenLinQuadraticOptimization(alpha.Length, Q, zeros, ones)
{
Tolerance = eps,
Shrinking = true,
Solution = alpha
};

bool success = s.Minimize();

int sv = 0;
for (int i = 0; i < alpha.Length; i++)
if (alpha[i] > 0) sv++;

machine.SupportVectors = new double[sv][];
machine.Weights = new double[sv];

for (int i = 0, j = 0; i < alpha.Length; i++)
{
if (alpha[i] > 0)
{
machine.SupportVectors[j] = inputs[i];
machine.Weights[j] = alpha[i];
j++;
}
}

machine.Threshold = s.Rho;

if (computeError)
return ComputeError(inputs);
return 0.0;
}

/// <summary>
/// Runs the learning algorithm.
/// </summary>
///
/// <returns>
/// The misclassification error rate of
/// the resulting support vector machine.
/// </returns>
///
public double Run()
{
return Run(true);
}

/// <summary>
/// Computes the error rate for a given set of inputs.
/// </summary>
///
public double ComputeError(double[][] inputs)
{
double error = 0;
for (int i = 0; i < inputs.Length; i++)
error += machine.Compute(inputs[i]);

return error;
}


double Q(int i, int j)
{
return kernel.Function(inputs[i], inputs[j]);
}

}
}
2 changes: 2 additions & 0 deletions Sources/Accord.Math/Accord.Math.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
<Compile Include="IO\Mat\MatNode.cs" />
<Compile Include="IO\Mat\MatReader.cs" />
<Compile Include="IO\Mat\MatSparse.cs" />
<Compile Include="Optimization\Unconstrained\FanChenLinQuadraticOptimization.cs" />
<Compile Include="Random\Generator.cs" />
<Compile Include="Comparers\Accord.Math.Comparers.cs" />
<Compile Include="Convergence\Base\IConvergence.cs" />
Expand Down Expand Up @@ -378,6 +379,7 @@
<Compile Include="Functions\Bessel.cs" />
<Compile Include="Tools.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Jagged.cs" />
<Compile Include="Vector.cs" />
<Compile Include="Wavelets\CDF97.cs" />
<Compile Include="Wavelets\Haar.cs" />
Expand Down
2 changes: 2 additions & 0 deletions Sources/Accord.Math/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "FFT", Scope = "member", Target = "Accord.Math.Transforms.FourierTransform2.#FFT2(AForge.Math.Complex[][],AForge.Math.FourierTransform+Direction)")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix", Scope = "type", Target = "Accord.IO.MatNode")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods", Scope = "member", Target = "Accord.IO.MatNode.#Value")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Q", Scope = "member", Target = "Accord.Math.Optimization.FanChenLinQuadraticOptimization.#.ctor(System.Int32,System.Func`3<System.Int32,System.Int32,System.Double>,System.Double[],System.Int32[])")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Q", Scope = "member", Target = "Accord.Math.Optimization.FanChenLinQuadraticOptimization.#.ctor(System.Int32,System.Func`3<System.Int32,System.Int32,System.Double>)")]

0 comments on commit 51914a8

Please sign in to comment.