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
See also
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)
-
predict
(responses)[source]¶ Parameters: responses (matrix, vector) – The response to some unknown variable. Return type: vector, Double
-
-
class
machine_learning.
kNN
[source]¶ See also
Warning
My simple test failed.
Note
Actually called “kNearestNeighbors”. To change very soon.
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