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

Commit

Permalink
Updating sample tutorial applications.
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarsouza committed Dec 8, 2014
1 parent fa1d92b commit 7076f10
Show file tree
Hide file tree
Showing 32 changed files with 1,254 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,37 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Accord, Version=2.13.1.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=MSIL">
<Reference Include="Accord, Version=2.14.0.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Accord.2.13.1\lib\net40\Accord.dll</HintPath>
<HintPath>..\packages\Accord.2.14.0\lib\net40\Accord.dll</HintPath>
</Reference>
<Reference Include="Accord.Controls, Version=2.13.1.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=MSIL">
<Reference Include="Accord.Controls, Version=2.14.0.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Accord.Controls.2.13.1\lib\net40\Accord.Controls.dll</HintPath>
<HintPath>..\packages\Accord.Controls.2.14.0\lib\net40\Accord.Controls.dll</HintPath>
</Reference>
<Reference Include="Accord.Imaging, Version=2.13.1.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=MSIL">
<Reference Include="Accord.Imaging, Version=2.14.0.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Accord.Imaging.2.13.1\lib\net40\Accord.Imaging.dll</HintPath>
<HintPath>..\packages\Accord.Imaging.2.14.0\lib\net40\Accord.Imaging.dll</HintPath>
</Reference>
<Reference Include="Accord.IO">
<HintPath>..\packages\Accord.IO.2.13.1\lib\net40\Accord.IO.dll</HintPath>
<Reference Include="Accord.IO, Version=2.14.0.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Accord.IO.2.14.0\lib\net40\Accord.IO.dll</HintPath>
</Reference>
<Reference Include="Accord.MachineLearning, Version=2.13.1.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=MSIL">
<Reference Include="Accord.MachineLearning, Version=2.14.0.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Accord.MachineLearning.2.13.1\lib\net40\Accord.MachineLearning.dll</HintPath>
<HintPath>..\packages\Accord.MachineLearning.2.14.0\lib\net40\Accord.MachineLearning.dll</HintPath>
</Reference>
<Reference Include="Accord.Math, Version=2.13.1.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=MSIL">
<Reference Include="Accord.Math, Version=2.14.0.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Accord.Math.2.13.1\lib\net40\Accord.Math.dll</HintPath>
<HintPath>..\packages\Accord.Math.2.14.0\lib\net40\Accord.Math.dll</HintPath>
</Reference>
<Reference Include="Accord.Neuro, Version=2.13.1.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=MSIL">
<Reference Include="Accord.Neuro, Version=2.14.0.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Accord.Neuro.2.13.1\lib\net40\Accord.Neuro.dll</HintPath>
<HintPath>..\packages\Accord.Neuro.2.14.0\lib\net40\Accord.Neuro.dll</HintPath>
</Reference>
<Reference Include="Accord.Statistics, Version=2.13.1.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=MSIL">
<Reference Include="Accord.Statistics, Version=2.14.0.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Accord.Statistics.2.13.1\lib\net40\Accord.Statistics.dll</HintPath>
<HintPath>..\packages\Accord.Statistics.2.14.0\lib\net40\Accord.Statistics.dll</HintPath>
</Reference>
<Reference Include="AForge, Version=2.2.5.0, Culture=neutral, PublicKeyToken=c1db6ff4eaa06aeb, processorArchitecture=MSIL">
<HintPath>..\packages\AForge.2.2.5\lib\AForge.dll</HintPath>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Data;
using System;
using System.Data;
using Accord.Controls;
using Accord.IO;
using Accord.MachineLearning.Bayes;
Expand All @@ -7,13 +8,12 @@
using Accord.MachineLearning.VectorMachines;
using Accord.MachineLearning.VectorMachines.Learning;
using Accord.Math;
using Accord.Neuro.Learning;
using Accord.Statistics.Distributions.Univariate;
using Accord.Statistics.Kernels;
using Accord.Statistics.Models.Regression;
using Accord.Statistics.Models.Regression.Fitting;
using System;
using AForge.Neuro;
using Accord.Neuro.Learning;

namespace ClassificationSample
{
Expand Down Expand Up @@ -45,88 +45,56 @@ static void Main(string[] args)
network(inputs, outputs);
}

private static void network(double[][] inputs, int[] outputs)
private static void naiveBayes(double[][] inputs, int[] outputs)
{
// Since we would like to learn binary outputs in the form
// [-1,+1], we can use a bipolar sigmoid activation function
IActivationFunction function = new BipolarSigmoidFunction();

// In our problem, we have 2 inputs (x, y pairs), and we will
// be creating a network with 5 hidden neurons and 1 output:
//
var network = new ActivationNetwork(function,
inputsCount: 2, neuronsCount: new[] { 5, 1 });

// Create a Levenberg-Marquardt algorithm
var teacher = new LevenbergMarquardtLearning(network)
{
UseRegularization = true
};
// In our problem, we have 2 classes (samples can be either
// positive or negative), and 2 inputs (x and y coordinates).

var nb = new NaiveBayes<NormalDistribution>(classes: 2,
inputs: 2, initial: new NormalDistribution());

// Because the network is expecting multiple outputs,
// we have to convert our single variable into arrays
// The Naive Bayes expects the class labels to
// range from 0 to k, so we convert -1 to be 0:
//
var y = outputs.ToDouble().ToArray();

// Iterate until stop criteria is met
double error = double.PositiveInfinity;
double previous;

do
{
previous = error;

// Compute one learning iteration
error = teacher.RunEpoch(inputs, y);

} while (Math.Abs(previous - error) < 1e-10 * previous);
outputs = outputs.Apply(x => x < 0 ? 0 : x);

// Estimate the Naive Bayes
double error = nb.Estimate(inputs, outputs);

// Classify the samples using the model
int[] answers = inputs.Apply(network.Compute).GetColumn(0).ToInt32();
int[] answers = inputs.Apply(nb.Compute);

// Plot the results
ScatterplotBox.Show("Expected results", inputs, outputs);
ScatterplotBox.Show("Network results", inputs, answers)
ScatterplotBox.Show("Naive Bayes results", inputs, answers)
.Hold();
}

private static void logistic(double[][] inputs, int[] outputs)
private static void decisionTree(double[][] inputs, int[] outputs)
{
// In our problem, we have 2 inputs (x, y pairs)
var logistic = new LogisticRegression(inputs: 2);

// Create a iterative re-weighted least squares algorithm
var teacher = new IterativeReweightedLeastSquares(logistic);
// In our problem, we have 2 classes (samples can be either
// positive or negative), and 2 continuous-valued inputs.
DecisionTree tree = new DecisionTree(inputs: new[]
{
DecisionVariable.Continuous("X"),
DecisionVariable.Continuous("Y")
}, classes: 2);

C45Learning teacher = new C45Learning(tree);

// Logistic Regression expects the output labels
// to range from 0 to k, so we convert -1 to be 0:
// The C4.5 algorithm expects the class labels to
// range from 0 to k, so we convert -1 to be zero:
//
outputs = outputs.Apply(x => x < 0 ? 0 : x);


// Iterate until stop criteria is met
double error = double.PositiveInfinity;
double previous;

do
{
previous = error;

// Compute one learning iteration
error = teacher.Run(inputs, outputs);

} while (Math.Abs(previous - error) < 1e-10 * previous);

double error = teacher.Run(inputs, outputs);

// Classify the samples using the model
int[] answers = inputs.Apply(logistic.Compute).Apply(Math.Round).ToInt32();
int[] answers = inputs.Apply(tree.Compute);

// Plot the results
ScatterplotBox.Show("Expected results", inputs, outputs);
ScatterplotBox.Show("Logistic Regression results", inputs, answers)
ScatterplotBox.Show("Decision Tree results", inputs, answers)
.Hold();
}

Expand Down Expand Up @@ -197,57 +165,90 @@ private static void kernelSvm(double[][] inputs, int[] outputs)
ScatterplotBox.Show("Support vectors", sv).Hold();
}

private static void decisionTree(double[][] inputs, int[] outputs)
private static void network(double[][] inputs, int[] outputs)
{
// In our problem, we have 2 classes (samples can be either
// positive or negative), and 2 continuous-valued inputs.
DecisionTree tree = new DecisionTree(attributes: new[]
// Since we would like to learn binary outputs in the form
// [-1,+1], we can use a bipolar sigmoid activation function
IActivationFunction function = new BipolarSigmoidFunction();

// In our problem, we have 2 inputs (x, y pairs), and we will
// be creating a network with 5 hidden neurons and 1 output:
//
var network = new ActivationNetwork(function,
inputsCount: 2, neuronsCount: new[] { 5, 1 });

// Create a Levenberg-Marquardt algorithm
var teacher = new LevenbergMarquardtLearning(network)
{
DecisionVariable.Continuous("X"),
DecisionVariable.Continuous("Y")
}, outputClasses: 2);
UseRegularization = true
};

C45Learning teacher = new C45Learning(tree);

// The C4.5 algorithm expects the class labels to
// range from 0 to k, so we convert -1 to be zero:
// Because the network is expecting multiple outputs,
// we have to convert our single variable into arrays
//
outputs = outputs.Apply(x => x < 0 ? 0 : x);
var y = outputs.ToDouble().ToArray();

// Iterate until stop criteria is met
double error = double.PositiveInfinity;
double previous;

do
{
previous = error;

// Compute one learning iteration
error = teacher.RunEpoch(inputs, y);

} while (Math.Abs(previous - error) < 1e-10 * previous);

double error = teacher.Run(inputs, outputs);

// Classify the samples using the model
int[] answers = inputs.Apply(tree.Compute);
int[] answers = inputs.Apply(network.Compute).GetColumn(0).ToInt32();

// Plot the results
ScatterplotBox.Show("Expected results", inputs, outputs);
ScatterplotBox.Show("Decision Tree results", inputs, answers)
ScatterplotBox.Show("Network results", inputs, answers)
.Hold();
}

private static void naiveBayes(double[][] inputs, int[] outputs)
private static void logistic(double[][] inputs, int[] outputs)
{
// In our problem, we have 2 classes (samples can be either
// positive or negative), and 2 inputs (x and y coordinates).
// In our problem, we have 2 inputs (x, y pairs)
var logistic = new LogisticRegression(inputs: 2);

var nb = new NaiveBayes<NormalDistribution>(classes: 2,
inputs: 2, prior: new NormalDistribution());
// Create a iterative re-weighted least squares algorithm
var teacher = new IterativeReweightedLeastSquares(logistic);

// The Naive Bayes expects the class labels to
// range from 0 to k, so we convert -1 to be 0:

// Logistic Regression expects the output labels
// to range from 0 to k, so we convert -1 to be 0:
//
outputs = outputs.Apply(x => x < 0 ? 0 : x);

// Estimate the Naive Bayes
double error = nb.Estimate(inputs, outputs);

// Iterate until stop criteria is met
double error = double.PositiveInfinity;
double previous;

do
{
previous = error;

// Compute one learning iteration
error = teacher.Run(inputs, outputs);

} while (Math.Abs(previous - error) < 1e-10 * previous);


// Classify the samples using the model
int[] answers = inputs.Apply(nb.Compute);
int[] answers = inputs.Apply(logistic.Compute).Apply(Math.Round).ToInt32();

// Plot the results
ScatterplotBox.Show("Expected results", inputs, outputs);
ScatterplotBox.Show("Naive Bayes results", inputs, answers)
ScatterplotBox.Show("Logistic Regression results", inputs, answers)
.Hold();
}

}
}
31 changes: 31 additions & 0 deletions Samples/Tutorials/Classification/0. Introduction/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="AForge" publicKeyToken="c1db6ff4eaa06aeb" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.2.5.0" newVersion="2.2.5.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="AForge.Math" publicKeyToken="abba2e25397ee8c9" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.2.5.0" newVersion="2.2.5.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Accord" publicKeyToken="fa1a88e29555ccf7" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.14.0.0" newVersion="2.14.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Accord.Math" publicKeyToken="fa1a88e29555ccf7" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.14.0.0" newVersion="2.14.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Accord.MachineLearning" publicKeyToken="fa1a88e29555ccf7" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.14.0.0" newVersion="2.14.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Accord.Statistics" publicKeyToken="fa1a88e29555ccf7" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.14.0.0" newVersion="2.14.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Accord" version="2.13.1" targetFramework="net40-Client" />
<package id="Accord.Controls" version="2.13.1" targetFramework="net40-Client" />
<package id="Accord.Imaging" version="2.13.1" targetFramework="net40-Client" />
<package id="Accord.IO" version="2.13.1" targetFramework="net40-Client" />
<package id="Accord.MachineLearning" version="2.13.1" targetFramework="net40-Client" />
<package id="Accord.Math" version="2.13.1" targetFramework="net40-Client" />
<package id="Accord.Neuro" version="2.13.1" targetFramework="net40-Client" />
<package id="Accord.Statistics" version="2.13.1" targetFramework="net40-Client" />
<package id="Accord" version="2.14.0" targetFramework="net40-Client" />
<package id="Accord.Controls" version="2.14.0" targetFramework="net40-Client" />
<package id="Accord.Imaging" version="2.14.0" targetFramework="net40-Client" />
<package id="Accord.IO" version="2.14.0" targetFramework="net40-Client" />
<package id="Accord.MachineLearning" version="2.14.0" targetFramework="net40-Client" />
<package id="Accord.Math" version="2.14.0" targetFramework="net40-Client" />
<package id="Accord.Neuro" version="2.14.0" targetFramework="net40-Client" />
<package id="Accord.Statistics" version="2.14.0" targetFramework="net40-Client" />
<package id="AForge" version="2.2.5" targetFramework="net40-Client" />
<package id="AForge.Genetic" version="2.2.5" targetFramework="net40-Client" />
<package id="AForge.Imaging" version="2.2.5" targetFramework="net40-Client" />
Expand Down
Loading

0 comments on commit 7076f10

Please sign in to comment.