@CodeReview(reviewer="Kevin R. Dixon", date="2008-07-23", changesNeeded=false, comments={"Added PublicationReference to Wikiepedia article.","Minor changes to javadoc.","Looks fine."}) @PublicationReference(author="Wikipedia", title="Perceptron Learning algorithm", type=WebPage, year=2008, url="http://en.wikipedia.org/wiki/Perceptron#Learning_algorithm") public class Perceptron extends AbstractAnytimeSupervisedBatchLearner<Vectorizable,java.lang.Boolean,LinearBinaryCategorizer> implements MeasurablePerformanceAlgorithm, VectorFactoryContainer
Perceptron
class implements the standard Perceptron learning
algorithm that learns a binary classifier based on vector input. This
implementation also allows for margins to be defined in learning in order to
find a hyperplane.Modifier and Type | Field and Description |
---|---|
static double |
DEFAULT_MARGIN_NEGATIVE
The default negative margin, 0.0.
|
static double |
DEFAULT_MARGIN_POSITIVE
The default positive margin, 0.0.
|
static int |
DEFAULT_MAX_ITERATIONS
The default maximum number of iterations, 100.
|
data, keepGoing
maxIterations
DEFAULT_ITERATION, iteration
Constructor and Description |
---|
Perceptron()
Creates a new instance of Perceptron.
|
Perceptron(int maxIterations)
Creates a new instance of Perceptron with the given maximum number of
iterations.
|
Perceptron(int maxIterations,
double marginPositive,
double marginNegative)
Creates a new instance of Perceptron with the given parameters
|
Perceptron(int maxIterations,
double marginPositive,
double marginNegative,
VectorFactory<?> vectorFactory)
Creates a new instance of Perceptron with the given parameters
|
Modifier and Type | Method and Description |
---|---|
protected void |
cleanupAlgorithm()
Called to clean up the learning algorithm's state after learning has
finished.
|
Perceptron |
clone()
This makes public the clone method on the
Object class and
removes the exception that it throws. |
int |
getErrorCount()
Gets the error count of the most recent iteration.
|
double |
getMarginNegative()
Gets the negative margin that is enforced.
|
double |
getMarginPositive()
Gets the positive margin that is enforced.
|
NamedValue<java.lang.Integer> |
getPerformance()
Gets the name-value pair that describes the current performance of the
algorithm.
|
LinearBinaryCategorizer |
getResult()
Gets the current result of the algorithm.
|
VectorFactory<?> |
getVectorFactory()
Gets the vector factory the object to use to create new vectors.
|
protected boolean |
initializeAlgorithm()
Called to initialize the learning algorithm's state based on the
data that is stored in the data field.
|
protected void |
setErrorCount(int errorCount)
Sets the error count of the most recent iteration.
|
void |
setMargin(double margin)
Sets both the positive and negative margin to the same value.
|
void |
setMarginNegative(double marginNegative)
Sets the negative margin that is enforced.
|
void |
setMarginPositive(double marginPositive)
Sets the positive margin that is enforced.
|
protected void |
setResult(LinearBinaryCategorizer result)
Sets the object currently being result.
|
void |
setVectorFactory(VectorFactory<?> vectorFactory)
Sets the VectorFactory used to create the weight vector.
|
protected boolean |
step()
Called to take a single step of the learning algorithm.
|
getData, getKeepGoing, learn, setData, setKeepGoing, stop
getMaxIterations, isResultValid, setMaxIterations
addIterativeAlgorithmListener, fireAlgorithmEnded, fireAlgorithmStarted, fireStepEnded, fireStepStarted, getIteration, getListeners, removeIterativeAlgorithmListener, setIteration, setListeners
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
learn
getMaxIterations, setMaxIterations
addIterativeAlgorithmListener, getIteration, removeIterativeAlgorithmListener
isResultValid
public static final int DEFAULT_MAX_ITERATIONS
public static final double DEFAULT_MARGIN_POSITIVE
public static final double DEFAULT_MARGIN_NEGATIVE
public Perceptron()
public Perceptron(int maxIterations)
maxIterations
- The maximum number of iterations.public Perceptron(int maxIterations, double marginPositive, double marginNegative)
maxIterations
- The maximum number of iterations.marginPositive
- The positive margin to enforce.marginNegative
- The negative margin to enforce.public Perceptron(int maxIterations, double marginPositive, double marginNegative, VectorFactory<?> vectorFactory)
maxIterations
- The maximum number of iterations.marginPositive
- The positive margin to enforce.marginNegative
- The negative margin to enforce.vectorFactory
- The VectorFactory to use to create the weight
vector.public Perceptron clone()
AbstractCloneableSerializable
Object
class and
removes the exception that it throws. Its default behavior is to
automatically create a clone of the exact type of object that the
clone is called on and to copy all primitives but to keep all references,
which means it is a shallow copy.
Extensions of this class may want to override this method (but call
super.clone()
to implement a "smart copy". That is, to target
the most common use case for creating a copy of the object. Because of
the default behavior being a shallow copy, extending classes only need
to handle fields that need to have a deeper copy (or those that need to
be reset). Some of the methods in ObjectUtil
may be helpful in
implementing a custom clone method.
Note: The contract of this method is that you must use
super.clone()
as the basis for your implementation.clone
in interface CloneableSerializable
clone
in class AbstractAnytimeBatchLearner<java.util.Collection<? extends InputOutputPair<? extends Vectorizable,java.lang.Boolean>>,LinearBinaryCategorizer>
protected boolean initializeAlgorithm()
AbstractAnytimeBatchLearner
initializeAlgorithm
in class AbstractAnytimeBatchLearner<java.util.Collection<? extends InputOutputPair<? extends Vectorizable,java.lang.Boolean>>,LinearBinaryCategorizer>
protected boolean step()
AbstractAnytimeBatchLearner
step
in class AbstractAnytimeBatchLearner<java.util.Collection<? extends InputOutputPair<? extends Vectorizable,java.lang.Boolean>>,LinearBinaryCategorizer>
protected void cleanupAlgorithm()
AbstractAnytimeBatchLearner
cleanupAlgorithm
in class AbstractAnytimeBatchLearner<java.util.Collection<? extends InputOutputPair<? extends Vectorizable,java.lang.Boolean>>,LinearBinaryCategorizer>
public void setMargin(double margin)
margin
- The new value for both the positive and negative margins.public double getMarginPositive()
public void setMarginPositive(double marginPositive)
marginPositive
- The positive margin that is enforced.public double getMarginNegative()
public void setMarginNegative(double marginNegative)
marginNegative
- The negative margin that is enforced.public VectorFactory<?> getVectorFactory()
VectorFactoryContainer
getVectorFactory
in interface VectorFactoryContainer
public void setVectorFactory(VectorFactory<?> vectorFactory)
vectorFactory
- The VectorFactory used to create the weight vector.public LinearBinaryCategorizer getResult()
AnytimeAlgorithm
getResult
in interface AnytimeAlgorithm<LinearBinaryCategorizer>
protected void setResult(LinearBinaryCategorizer result)
result
- The object currently being result.public int getErrorCount()
protected void setErrorCount(int errorCount)
errorCount
- The current error count.public NamedValue<java.lang.Integer> getPerformance()
MeasurablePerformanceAlgorithm
getPerformance
in interface MeasurablePerformanceAlgorithm