-
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c7e1439
commit ba54acc
Showing
7 changed files
with
477 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# k Nearest Neighbours | ||
For a walk through, please refer to the article on Medium. [Machine Learning with Javascript : Part 2 (k Nearest Neighbours)](https://hackernoon.com/machine-learning-with-javascript-part-1-9b97f3ed4fe5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
const KNN = require('ml-knn'); | ||
const csv = require('csvtojson'); | ||
const prompt = require('prompt'); | ||
const knn = new KNN(); | ||
|
||
const csvFilePath = 'iris.csv'; // Data | ||
const names = ['sepalLength', 'sepalWidth', 'petalLength', 'petalWidth', 'type']; // For header | ||
|
||
let seperationSize; // To seperate training and test data | ||
|
||
let data = [], X = [], y = []; | ||
|
||
let trainingSetX = [], trainingSetY = [], testSetX = [], testSetY = []; | ||
|
||
csv({noheader: true, headers: names}) | ||
.fromFile(csvFilePath) | ||
.on('json', (jsonObj) => { | ||
data.push(jsonObj); // Push each object to data Array | ||
}) | ||
.on('done', (error) => { | ||
seperationSize = 0.7 * data.length; | ||
data = shuffleArray(data); | ||
dressData(); | ||
}); | ||
|
||
function dressData() { | ||
|
||
/** | ||
* There are three different types of Iris flowers | ||
* that this dataset classifies. | ||
* | ||
* 1. Iris Setosa (Iris-setosa) | ||
* 2. Iris Versicolor (Iris-versicolor) | ||
* 3. Iris Virginica (Iris-virginica) | ||
* | ||
* We are going to change these classes from Strings to numbers. | ||
* Such that, a value of type equal to | ||
* 0 would mean setosa, | ||
* 1 would mean versicolor, and | ||
* 3 would mean virginica | ||
*/ | ||
|
||
let types = new Set(); // To gather UNIQUE classes | ||
|
||
data.forEach((row) => { | ||
types.add(row.type); | ||
}); | ||
|
||
typesArray = [...types]; // To save the different types of classes. | ||
|
||
data.forEach((row) => { | ||
let rowArray, typeNumber; | ||
|
||
rowArray = Object.keys(row).map(key => parseFloat(row[key])).slice(0, 4); | ||
|
||
typeNumber = typesArray.indexOf(row.type); // Convert type(String) to type(Number) | ||
|
||
X.push(rowArray); | ||
y.push(typeNumber); | ||
}); | ||
|
||
trainingSetX = X.slice(0, seperationSize); | ||
trainingSetY = y.slice(0, seperationSize); | ||
testSetX = X.slice(seperationSize); | ||
testSetY = y.slice(seperationSize); | ||
|
||
train(); | ||
} | ||
|
||
function train() { | ||
knn.train(trainingSetX, trainingSetY, { k:7 }); | ||
test(); | ||
} | ||
|
||
function test() { | ||
const result = knn.predict(testSetX); | ||
const testSetLength = testSetX.length; | ||
const predictionError = error(result, testSetY); | ||
console.log(`Test Set Size = ${testSetLength} and number of Misclassifications = ${predictionError}`); | ||
predict(); | ||
} | ||
|
||
function error(predicted, expected) { | ||
let misclassifications = 0; | ||
for (var index = 0; index < predicted.length; index++) { | ||
if (predicted[index] !== expected[index]) { | ||
misclassifications++; | ||
} | ||
} | ||
return misclassifications; | ||
} | ||
|
||
function predict() { | ||
let temp = []; | ||
prompt.start(); | ||
|
||
prompt.get(['Sepal Length', 'Sepal Width', 'Petal Length', 'Petal Width'], function (err, result) { | ||
if (!err) { | ||
for (var key in result) { | ||
temp.push(parseFloat(result[key])); | ||
} | ||
console.log(`With ${temp} -- type = ${knn.getSinglePrediction(temp)}`); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* https://stackoverflow.com/a/12646864 | ||
* Randomize array element order in-place. | ||
* Using Durstenfeld shuffle algorithm. | ||
*/ | ||
function shuffleArray(array) { | ||
for (var i = array.length - 1; i > 0; i--) { | ||
var j = Math.floor(Math.random() * (i + 1)); | ||
var temp = array[i]; | ||
array[i] = array[j]; | ||
array[j] = temp; | ||
} | ||
return array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
5.1,3.5,1.4,0.2,Iris-setosa | ||
4.9,3.0,1.4,0.2,Iris-setosa | ||
4.7,3.2,1.3,0.2,Iris-setosa | ||
4.6,3.1,1.5,0.2,Iris-setosa | ||
5.0,3.6,1.4,0.2,Iris-setosa | ||
5.4,3.9,1.7,0.4,Iris-setosa | ||
4.6,3.4,1.4,0.3,Iris-setosa | ||
5.0,3.4,1.5,0.2,Iris-setosa | ||
4.4,2.9,1.4,0.2,Iris-setosa | ||
4.9,3.1,1.5,0.1,Iris-setosa | ||
5.4,3.7,1.5,0.2,Iris-setosa | ||
4.8,3.4,1.6,0.2,Iris-setosa | ||
4.8,3.0,1.4,0.1,Iris-setosa | ||
4.3,3.0,1.1,0.1,Iris-setosa | ||
5.8,4.0,1.2,0.2,Iris-setosa | ||
5.7,4.4,1.5,0.4,Iris-setosa | ||
5.4,3.9,1.3,0.4,Iris-setosa | ||
5.1,3.5,1.4,0.3,Iris-setosa | ||
5.7,3.8,1.7,0.3,Iris-setosa | ||
5.1,3.8,1.5,0.3,Iris-setosa | ||
5.4,3.4,1.7,0.2,Iris-setosa | ||
5.1,3.7,1.5,0.4,Iris-setosa | ||
4.6,3.6,1.0,0.2,Iris-setosa | ||
5.1,3.3,1.7,0.5,Iris-setosa | ||
4.8,3.4,1.9,0.2,Iris-setosa | ||
5.0,3.0,1.6,0.2,Iris-setosa | ||
5.0,3.4,1.6,0.4,Iris-setosa | ||
5.2,3.5,1.5,0.2,Iris-setosa | ||
5.2,3.4,1.4,0.2,Iris-setosa | ||
4.7,3.2,1.6,0.2,Iris-setosa | ||
4.8,3.1,1.6,0.2,Iris-setosa | ||
5.4,3.4,1.5,0.4,Iris-setosa | ||
5.2,4.1,1.5,0.1,Iris-setosa | ||
5.5,4.2,1.4,0.2,Iris-setosa | ||
4.9,3.1,1.5,0.1,Iris-setosa | ||
5.0,3.2,1.2,0.2,Iris-setosa | ||
5.5,3.5,1.3,0.2,Iris-setosa | ||
4.9,3.1,1.5,0.1,Iris-setosa | ||
4.4,3.0,1.3,0.2,Iris-setosa | ||
5.1,3.4,1.5,0.2,Iris-setosa | ||
5.0,3.5,1.3,0.3,Iris-setosa | ||
4.5,2.3,1.3,0.3,Iris-setosa | ||
4.4,3.2,1.3,0.2,Iris-setosa | ||
5.0,3.5,1.6,0.6,Iris-setosa | ||
5.1,3.8,1.9,0.4,Iris-setosa | ||
4.8,3.0,1.4,0.3,Iris-setosa | ||
5.1,3.8,1.6,0.2,Iris-setosa | ||
4.6,3.2,1.4,0.2,Iris-setosa | ||
5.3,3.7,1.5,0.2,Iris-setosa | ||
5.0,3.3,1.4,0.2,Iris-setosa | ||
7.0,3.2,4.7,1.4,Iris-versicolor | ||
6.4,3.2,4.5,1.5,Iris-versicolor | ||
6.9,3.1,4.9,1.5,Iris-versicolor | ||
5.5,2.3,4.0,1.3,Iris-versicolor | ||
6.5,2.8,4.6,1.5,Iris-versicolor | ||
5.7,2.8,4.5,1.3,Iris-versicolor | ||
6.3,3.3,4.7,1.6,Iris-versicolor | ||
4.9,2.4,3.3,1.0,Iris-versicolor | ||
6.6,2.9,4.6,1.3,Iris-versicolor | ||
5.2,2.7,3.9,1.4,Iris-versicolor | ||
5.0,2.0,3.5,1.0,Iris-versicolor | ||
5.9,3.0,4.2,1.5,Iris-versicolor | ||
6.0,2.2,4.0,1.0,Iris-versicolor | ||
6.1,2.9,4.7,1.4,Iris-versicolor | ||
5.6,2.9,3.6,1.3,Iris-versicolor | ||
6.7,3.1,4.4,1.4,Iris-versicolor | ||
5.6,3.0,4.5,1.5,Iris-versicolor | ||
5.8,2.7,4.1,1.0,Iris-versicolor | ||
6.2,2.2,4.5,1.5,Iris-versicolor | ||
5.6,2.5,3.9,1.1,Iris-versicolor | ||
5.9,3.2,4.8,1.8,Iris-versicolor | ||
6.1,2.8,4.0,1.3,Iris-versicolor | ||
6.3,2.5,4.9,1.5,Iris-versicolor | ||
6.1,2.8,4.7,1.2,Iris-versicolor | ||
6.4,2.9,4.3,1.3,Iris-versicolor | ||
6.6,3.0,4.4,1.4,Iris-versicolor | ||
6.8,2.8,4.8,1.4,Iris-versicolor | ||
6.7,3.0,5.0,1.7,Iris-versicolor | ||
6.0,2.9,4.5,1.5,Iris-versicolor | ||
5.7,2.6,3.5,1.0,Iris-versicolor | ||
5.5,2.4,3.8,1.1,Iris-versicolor | ||
5.5,2.4,3.7,1.0,Iris-versicolor | ||
5.8,2.7,3.9,1.2,Iris-versicolor | ||
6.0,2.7,5.1,1.6,Iris-versicolor | ||
5.4,3.0,4.5,1.5,Iris-versicolor | ||
6.0,3.4,4.5,1.6,Iris-versicolor | ||
6.7,3.1,4.7,1.5,Iris-versicolor | ||
6.3,2.3,4.4,1.3,Iris-versicolor | ||
5.6,3.0,4.1,1.3,Iris-versicolor | ||
5.5,2.5,4.0,1.3,Iris-versicolor | ||
5.5,2.6,4.4,1.2,Iris-versicolor | ||
6.1,3.0,4.6,1.4,Iris-versicolor | ||
5.8,2.6,4.0,1.2,Iris-versicolor | ||
5.0,2.3,3.3,1.0,Iris-versicolor | ||
5.6,2.7,4.2,1.3,Iris-versicolor | ||
5.7,3.0,4.2,1.2,Iris-versicolor | ||
5.7,2.9,4.2,1.3,Iris-versicolor | ||
6.2,2.9,4.3,1.3,Iris-versicolor | ||
5.1,2.5,3.0,1.1,Iris-versicolor | ||
5.7,2.8,4.1,1.3,Iris-versicolor | ||
6.3,3.3,6.0,2.5,Iris-virginica | ||
5.8,2.7,5.1,1.9,Iris-virginica | ||
7.1,3.0,5.9,2.1,Iris-virginica | ||
6.3,2.9,5.6,1.8,Iris-virginica | ||
6.5,3.0,5.8,2.2,Iris-virginica | ||
7.6,3.0,6.6,2.1,Iris-virginica | ||
4.9,2.5,4.5,1.7,Iris-virginica | ||
7.3,2.9,6.3,1.8,Iris-virginica | ||
6.7,2.5,5.8,1.8,Iris-virginica | ||
7.2,3.6,6.1,2.5,Iris-virginica | ||
6.5,3.2,5.1,2.0,Iris-virginica | ||
6.4,2.7,5.3,1.9,Iris-virginica | ||
6.8,3.0,5.5,2.1,Iris-virginica | ||
5.7,2.5,5.0,2.0,Iris-virginica | ||
5.8,2.8,5.1,2.4,Iris-virginica | ||
6.4,3.2,5.3,2.3,Iris-virginica | ||
6.5,3.0,5.5,1.8,Iris-virginica | ||
7.7,3.8,6.7,2.2,Iris-virginica | ||
7.7,2.6,6.9,2.3,Iris-virginica | ||
6.0,2.2,5.0,1.5,Iris-virginica | ||
6.9,3.2,5.7,2.3,Iris-virginica | ||
5.6,2.8,4.9,2.0,Iris-virginica | ||
7.7,2.8,6.7,2.0,Iris-virginica | ||
6.3,2.7,4.9,1.8,Iris-virginica | ||
6.7,3.3,5.7,2.1,Iris-virginica | ||
7.2,3.2,6.0,1.8,Iris-virginica | ||
6.2,2.8,4.8,1.8,Iris-virginica | ||
6.1,3.0,4.9,1.8,Iris-virginica | ||
6.4,2.8,5.6,2.1,Iris-virginica | ||
7.2,3.0,5.8,1.6,Iris-virginica | ||
7.4,2.8,6.1,1.9,Iris-virginica | ||
7.9,3.8,6.4,2.0,Iris-virginica | ||
6.4,2.8,5.6,2.2,Iris-virginica | ||
6.3,2.8,5.1,1.5,Iris-virginica | ||
6.1,2.6,5.6,1.4,Iris-virginica | ||
7.7,3.0,6.1,2.3,Iris-virginica | ||
6.3,3.4,5.6,2.4,Iris-virginica | ||
6.4,3.1,5.5,1.8,Iris-virginica | ||
6.0,3.0,4.8,1.8,Iris-virginica | ||
6.9,3.1,5.4,2.1,Iris-virginica | ||
6.7,3.1,5.6,2.4,Iris-virginica | ||
6.9,3.1,5.1,2.3,Iris-virginica | ||
5.8,2.7,5.1,1.9,Iris-virginica | ||
6.8,3.2,5.9,2.3,Iris-virginica | ||
6.7,3.3,5.7,2.5,Iris-virginica | ||
6.7,3.0,5.2,2.3,Iris-virginica | ||
6.3,2.5,5.0,1.9,Iris-virginica | ||
6.5,3.0,5.2,2.0,Iris-virginica | ||
6.2,3.4,5.4,2.3,Iris-virginica | ||
5.9,3.0,5.1,1.8,Iris-virginica |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Linear Regression | ||
For a walk through, please refer to the article on Medium. [Machine Learning with Javascript : Part 1 (Linear Regression)](https://hackernoon.com/machine-learning-with-javascript-part-1-9b97f3ed4fe5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.