STClassifier is an implementation of K Nearest Neighbour written in Swift.
This is still pretty rough - it needs more error checking, comments, and a testing framework needs to be written!
Simply download the zip file and copy STClassifier.swift
into your project and you're good to go!
After you've imported STClassifier.swift
into your project, you'll need to do a few things to get going:
-
The class you want to be able to classify must conform to the
STClassifierClass
protocol. It must havetype
as a class variable and it must implement thegetVector() -> Array<Double>
method. -
Create a new classifier with
STClassifier<STClassifierClass>()
and add some training examples withfunc addTrainingExamples examples:STClassifierClass...)
. -
Create a new example with an empty
type
and pass it tofunc classify(c:STClassifierClass) -> String
to get the predicted class.
If you get stuck, check out the example XCode project. A good way to play around is to copy the code into an XCode playground!