Machine learning

This library integrates OpenCV. What I needed was implemented and not much more; if you want something you’ll probably have to implement it yourself using OpenCV.

Docs

This is a file that describes classes.

class machine_learning.SVM[source]

Implemented through OpenCV’s SVM

example:

var svm = SVM()

# our responses. 
var responses = reshape(ones(2*3), (2, 3))

#/ OpenCV can fail for too similar inputs
for i in 0..3{
    responses[1][i] = -1
}

#/ our targets.
var targets = ones(2); targets[1] = -1

#/ train with responses
svm.train(responses, targets: targets)

#/ predict
var y = -1 * ones(3) // must have same size!
var result = svm.predict(y)
M = 'number of responses'[source]

The number of responses.

N = 'number of variables in a response'[source]

The number of variables.

kernel_type = 'kernel type'[source]

Useful in mapping to a nonlinear function.

predict(responses)[source]
Parameters:responses (matrix, vector) – The response to some unknown variable.
Return type:vector, Double
setParams(svm_type, kernel, nu=0.5)[source]
Parameters:
  • svm_type (String) – Assumed to be either C_SVC, ONE_CLASS, NU_SVC or NU_SVR
  • kernel (String) – Assumed to be one of LIENAR or SIGMOID

Sets the parameters for the SVM.

svm_type = 'SVM type'[source]

Can drastically change performance.

train(responses, targets)[source]
Parameters:
  • resonses – The matrix of responses to some (known!) variable.
  • targets (vector) – The known variables a response is associated with.
class machine_learning.kNN[source]

Warning

My simple test failed.

Note

Actually called “kNearestNeighbors”. To change very soon.

M = 'responses'[source]

the number of variables in a response

N = 'variables'[source]

the number of variables you train for

predict(x, k)[source]
Parameters:
  • x (vector) – The reponse you want to train for.
  • k (Int) – The number of neighest neighbors you want to train against.
Return type:

Double. The predicted response.

train(responses, targets)[source]
Parameters:
  • responses (matrix) – The responses.
  • targets (vector) – The targets you know exist.

Examples

SVM

var svm = SVM()

// our responses.
var responses = reshape(ones(2*3), (2, 3))

// OpenCV can fail for too similar inputs
for i in 0..<3{
    responses[1][i] = -1
}

// our targets.
var targets = ones(2); targets[1] = -1

// train with responses
svm.train(responses, targets: targets)

// predict
var y = -1 * ones(3) // must have same size!
var result = svm.predict(y)

kNN

I haven’t and don’t have a use case for this; my simple test failed although I believe it’s correct.

var knn = kNearestNeighbors()
var responses = ones((4,6))
var targets = arange(4)

knn.train(responses, targets:targets)
var result = knn.predict(y, k:3)

SVD or PCA

var x = ones((2,4))
var (u, s, v) = svd(x)
x = ones((4,2))
(u, s, v) = svd(x)
println("    var (u, s, v) = svd(x) works. matches python exactly (checked by hand)")
var x_train:matrix = read_csv("python_testing/csvs/x_train.csv")
var y_train:vector = read_csv("python_testing/csvs/y_train.csv")

var x_test:matrix = read_csv("python_testing/csvs/x_test.csv")

var svm = SVM()
svm.train(x_train, y_train)
var yhat = svm.predict(x_test)

// how accurate are we?
var y_test:vector = read_csv("python_testing/csvs/y_test.csv")
var percent_correct:Double = argwhere(abs(y_test - yhat) < 0.5).n / yhat.n
println(percent_correct) // about 94% accurate