Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Svm #27

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open

Svm #27

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
314 changes: 314 additions & 0 deletions Notebooks/SupportVectorMachineTutorial.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,314 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"kernelspec": {
"display_name": "Swift",
"language": "swift",
"name": "swift"
},
"language_info": {
"file_extension": ".swift",
"mimetype": "text/x-swift",
"name": "swift",
"version": ""
},
"colab": {
"name": "SupportVectorMachineTutorial.ipynb",
"version": "0.3.2",
"provenance": [],
"collapsed_sections": []
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "hN2Xlkfgd4vU",
"colab_type": "text"
},
"source": [
"# Support Vector Machine\n",
"\n",
"***\n",
"### `class SVM(settings: String, dataFilePath: String, saveModelAt: String)`\n",
"***\n",
"\n",
"## Parameters:\n",
" #### `settings` : *String, default: empty string*\n",
" For svm train as per libSVM format\n",
" #### `dataFilePath`: *String*\n",
" Path to the data file to be trained on in libSVM format\n",
" #### `saveModelAt`: *String*\n",
" Path to where to save model at\n",
"\n",
"\n",
"## Attributes: \n",
" #### `fittedModel`: UnsafeMutablePointer` <svm_model> `\n",
" Model fitted by init during trainOn\n",
"\n",
"***\n",
"\n",
"## Methods\n",
"\n",
"***\n",
"\n",
" ### `boost()` : Fits a Gradient Boost Regressor.\n",
"\n",
"\n",
"***\n",
"\n",
" ### `predict(testingData: String, settings: String, saveAt: String)` : Predicts value for an example by getting all tree decisions.\n",
"\n",
"### Parameters:\n",
" #### `dataFilePath`: *String*\n",
" Path to the test data file in libSVM format\n",
" #### `settings` : *String, default: empty string*\n",
" For svm train as per libSVM format\n",
" #### `saveAt`: *String*\n",
" Path to where to save predictions at\n",
"### Returns:\n",
" Float array of predictions\n",
"\n",
"***\n",
"## Helper Function\n",
"***\n",
" ### `tensorToFile(path: String, data: [[Float]], label: [Float])`: Returns a libSVM file with data from a data & label tensor\n",
"\n",
" ### parameters:\n",
" #### `path`: String\n",
" To the where file to be saved\n",
" #### `data`: [[String]]\n",
" 2D array of float data\n",
" #### `label`: [String]\n",
" Float array of label\n",
" ### Returns:\n",
" Returns nothing\n",
"***"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "LWF2kIiid4vW",
"colab_type": "text"
},
"source": [
"# Example"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Iep64439d4vX",
"colab_type": "text"
},
"source": [
"## Install the swiftML package that's in the local directory."
]
},
{
"cell_type": "code",
"metadata": {
"id": "npVl0Xxlk9oW",
"colab_type": "code",
"colab": {}
},
"source": [
"//path to the local directory\n",
"%install '.package(path: \"/home/victora/GSOC19/github/swiftML\")' swiftML"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "kcDTSCQcd4vc",
"colab_type": "text"
},
"source": [
"## Install the swiftML package from GitHub."
]
},
{
"cell_type": "code",
"metadata": {
"id": "wxCyCTsKd4vd",
"colab_type": "code",
"colab": {}
},
"source": [
"%install '.package(url: \"https://github.com/param087/swiftML\", from: \"0.0.1\")' swiftML"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "oI4v2EDQd4vh",
"colab_type": "text"
},
"source": [
"## Import Swift packages"
]
},
{
"cell_type": "code",
"metadata": {
"id": "rsVtX_Ihd4vi",
"colab_type": "code",
"colab": {}
},
"source": [
"import TensorFlow\n",
"import swiftML"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "mv-ulegjd4vt",
"colab_type": "code",
"colab": {}
},
"source": [
"let booster = GradientBoostRegressor(data: patientDataTrain, target: 0, till: 200, learningRate: 0.1, using: \"gini\")\n",
"booster.boost()"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "5LiHKutErQqz"
},
"source": [
"## Load dataset"
]
},
{
"cell_type": "code",
"metadata": {
"id": "SxTeLuzArWBj",
"colab_type": "code",
"colab": {}
},
"source": [
"let x : [[Float]] = [[-7.15602562e-02, 2.29553900e-01, 2.16543447e-01, 6.52739792e-02],\n",
" [-4.03257104e-01, 2.00618406e+00, 2.02751248e+00, 8.50891944e-01],\n",
" [-1.13058206e+00, -2.02959251e-02, -7.10233633e-01,-1.44099108e+00],\n",
" [ 1.83324682e-01, -7.74610354e-01, -7.66054695e-01,-2.93668633e-01],\n",
" [-2.86920000e-01, -7.16952984e-01, -9.86585088e-01,-8.48214735e-01],\n",
" [-2.56042975e+00, 4.02232337e-01, -1.10074198e+00,-2.95958826e+00],\n",
" [ 4.22341441e-01, -2.03911440e+00, -2.05321581e+00,-8.49123046e-01],\n",
" [-5.09792713e-01, 4.92658944e-01, 2.48206729e-01,-3.09590732e-01],\n",
" [ 7.21669496e-01, -1.12155664e+00, -8.31839865e-01, 1.50742097e-01],\n",
" [-9.86132567e-01, 1.71053103e+00, 1.33828180e+00,-8.53410903e-02],\n",
" [-7.23168038e-02, -7.73668334e-01, -9.20383253e-01,-6.15748704e-01],\n",
" [ 4.55946498e-01, -4.57868762e-01, -2.41527100e-01, 2.65199620e-01],\n",
" [ 3.93906076e-01, 3.90247346e-01, 6.81520677e-01, 7.61804328e-01],\n",
" [ 2.03196825e+00, 8.40398655e-01, 2.18718140e+00, 3.13482383e+00],\n",
" [ 4.58931008e-02, -2.61403392e-01, -2.68225264e-01,-1.19266118e-01],\n",
" [ 3.73436160e-01, -1.07382634e+00, -9.89449288e-01,-2.56507783e-01],\n",
" [-1.20114435e+00, 1.17241061e-01, -5.97321845e-01,-1.43683401e+00],\n",
" [-1.30819171e+00, -4.32659559e-01, -1.28532883e+00,-1.94473774e+00],\n",
" [ 5.56269743e-01, -5.84264226e-01, -3.23726922e-01, 3.06165066e-01],\n",
" [-1.79506345e+00, 3.92904728e+00, 3.35973253e+00, 3.97369747e-01],\n",
" [-1.30813451e+00, 1.51697242e+00, 9.23281451e-01,-6.23042516e-01],\n",
" [-2.38076394e+00, 1.13712778e+00, -1.59027524e-01,-2.23460699e+00],\n",
" [-4.86612462e-01, -1.92161720e+00, -2.47263494e+00,-1.91692582e+00],\n",
" [ 4.32994532e-01, 2.20346371e-01, 5.12814562e-01, 6.95976074e-01],\n",
" [ 1.26780440e+00, -1.80170793e+00, -1.27034986e+00, 3.79112828e-01],\n",
" [ 1.10502647e+00, -1.94989387e+00, -1.53716448e+00, 7.31712082e-02],\n",
" [ 1.34057624e+00, -8.14593363e-01, -1.07894567e-01, 1.14012761e+00],\n",
" [-6.79598011e-01, 1.03943907e+00, 7.64389529e-01,-1.53297707e-01],\n",
" [-1.82896522e-01, -1.01290187e+00, -1.25860859e+00,-9.17515596e-01],\n",
" [ 1.10698637e+00, -1.06701573e+00, -5.35834091e-01, 6.74133734e-01],\n",
" [ 5.44818813e-01, -1.33330476e+00, -1.17921312e+00,-2.16052537e-01],\n",
" [ 1.55525060e+00, 2.56253272e-01, 1.23567148e+00, 2.13703895e+00],\n",
" [-3.45538051e-01, -2.29672334e+00, -2.81180710e+00,-1.99311342e+00],\n",
" [ 1.12073484e+00, -3.24744191e-01, 3.13380666e-01, 1.19466313e+00],\n",
" [-9.07197428e-01, 3.87813199e-01, -1.12132974e-01,-8.82342712e-01],\n",
" [ 1.87834887e+00, 5.77288519e-01, 1.79574591e+00, 2.76253865e+00],\n",
" [ 1.43370121e+00, -1.75423984e+00, -1.11573423e+00, 6.20716743e-01],\n",
" [ 3.80251566e-01, 2.02956697e+00, 2.53026908e+00, 1.85583449e+00],\n",
" [-1.25732069e+00, -2.14861012e+00, -3.19826339e+00,-3.04373307e+00],\n",
" [ 6.87661760e-01, -1.36592018e+00, -1.12933108e+00,-5.78388150e-02],\n",
" [-1.10453952e+00, 2.10141172e+00, 1.70910242e+00, 3.01549448e-02],\n",
" [-7.93470192e-01, 1.26662420e+00, 9.52529622e-01,-1.43043530e-01],\n",
" [-1.24378126e+00, 2.80821112e-01, -4.37933163e-01,-1.37977014e+00],\n",
" [ 4.17180364e-01, -1.16786115e+00, -1.06938289e+00,-2.65030274e-01],\n",
" [-4.22761581e-01, -7.69213504e-01, -1.12836011e+00,-1.05512600e+00],\n",
" [-2.47553402e-03, 3.99799271e-01, 4.51394467e-01, 2.67892177e-01],\n",
" [ 4.86681188e-01, -1.54740567e+00, -1.45709006e+00,-4.34580195e-01],\n",
" [-6.05415797e-01, -2.95970599e-01, -7.03291920e-01,-9.64902950e-01],\n",
" [-1.66130052e+00, 2.39203665e+00, 1.69989125e+00,-4.75683472e-01],\n",
" [-1.40246886e+00, -1.12856503e-01, -9.80358459e-01,-1.84696289e+00],\n",
" [ 1.77624479e+00, 8.01478699e-01, 1.98764670e+00, 2.78561851e+00]]\n",
"\n",
"let y : [Float] = [1,1,-1,-1,-1,-1,-1,1,-1,1,-1,1,1,1,1,-1,-1,-1,1,1,1,-1,-1,1,-1,1,1,1,-1,1,1,1,-1,1,1,1,-1,\n",
" 1,-1,-1,1,-1,-1,-1,-1,1,-1,-1,1,-1,1]\n",
"\n",
"\n",
"tensorToFile(path: \"/Users/victora/Desktop/holder/fulltest/heart_trainData\", data: x, label: y)\n"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "WjJB6Wkdd4vw",
"colab_type": "text"
},
"source": [
"## Fit a linear model"
]
},
{
"cell_type": "code",
"metadata": {
"id": "E5YrvnHBd4vy",
"colab_type": "code",
"colab": {}
},
"source": [
"let svm = SVM(settings : \"-s 0 -c 5 -t 2 -g 0.5 -e 0.1\",\n",
" filePath: \"/Users/victora/Desktop/holder/fulltest/heart_trainData\",\n",
" saveAt: \"/Users/victora/Downloads/libsvmTest/\")"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "SQZBKwpZkZuW",
"colab_type": "text"
},
"source": [
"### Getting predictions "
]
},
{
"cell_type": "code",
"metadata": {
"id": "i7ToWBYlrAzg",
"colab_type": "code",
"colab": {}
},
"source": [
"let predictions = svm.predict(testingData: \"/Users/victora/Downloads/libsvmTest/heart_testData\",\n",
" saveAt: \"/Users/victora/Downloads/libsvmTest/\")"
],
"execution_count": 0,
"outputs": []
}
]
}
8 changes: 8 additions & 0 deletions Sources/swiftML/SVM-Bridging-Header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//

#include "svm.h"
#include "svm-train.h"
#include "svm-scale.h"
#include "svm-predict.h"
Loading