-
-
Save MetlHedd/d6f5f19ca95aca9750cb70c534b917d2 to your computer and use it in GitHub Desktop.
KNN - Lua
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
-- Vamos guardar as nossas features | |
X={} | |
-- Petal length | |
X[1]={1.4,1.4,1.3,1.5,1.4,1.7,1.4,1.5,1.4,1.5,1.5,1.6,1.4,1.1,1.2,1.5,1.3,1.4,1.7,1.5,1.7,1.5,1.0,1.7,1.9,1.6,1.6,1.5,1.4,1.6,1.6,1.5,1.5,1.4,1.5,1.2,1.3,1.4,1.3,1.5,1.3,1.3,1.3,1.6,1.9,1.4,1.6,1.4,1.5,1.4,4.7,4.5,4.9,4.0,4.6,4.5,4.7,3.3,4.6,3.9,3.5,4.2,4.0,4.7,3.6,4.4,4.5,4.1,4.5,3.9,4.8,4.0,4.9,4.7,4.3,4.4,4.8,5.0,4.5,3.5,3.8,3.7,3.9,5.1,4.5,4.5,4.7,4.4,4.1,4.0,4.4,4.6,4.0,3.3,4.2,4.2,4.2,4.3,3.0,4.1,6.0,5.1,5.9,5.6,5.8,6.6,4.5,6.3,5.8,6.1,5.1,5.3,5.5,5.0,5.1,5.3,5.5,6.7,6.9,5.0,5.7,4.9,6.7,4.9,5.7,6.0,4.8,4.9,5.6,5.8,6.1,6.4,5.6,5.1,5.6,6.1,5.6,5.5,4.8,5.4,5.6,5.1,5.1,5.9,5.7,5.2,5.0,5.2,5.4,5.1} | |
-- Petal width | |
X[2]={0.2,0.2,0.2,0.2,0.2,0.4,0.3,0.2,0.2,0.1,0.2,0.2,0.1,0.1,0.2,0.4,0.4,0.3,0.3,0.3,0.2,0.4,0.2,0.5,0.2,0.2,0.4,0.2,0.2,0.2,0.2,0.4,0.1,0.2,0.2,0.2,0.2,0.1,0.2,0.2,0.3,0.3,0.2,0.6,0.4,0.3,0.2,0.2,0.2,0.2,1.4,1.5,1.5,1.3,1.5,1.3,1.6,1.0,1.3,1.4,1.0,1.5,1.0,1.4,1.3,1.4,1.5,1.0,1.5,1.1,1.8,1.3,1.5,1.2,1.3,1.4,1.4,1.7,1.5,1.0,1.1,1.0,1.2,1.6,1.5,1.6,1.5,1.3,1.3,1.3,1.2,1.4,1.2,1.0,1.3,1.2,1.3,1.3,1.1,1.3,2.5,1.9,2.1,1.8,2.2,2.1,1.7,1.8,1.8,2.5,2.0,1.9,2.1,2.0,2.4,2.3,1.8,2.2,2.3,1.5,2.3,2.0,2.0,1.8,2.1,1.8,1.8,1.8,2.1,1.6,1.9,2.0,2.2,1.5,1.4,2.3,2.4,1.8,1.8,2.1,2.4,2.3,1.9,2.3,2.5,2.3,1.9,2.0,2.3,1.8} | |
-- Guardando os nossas target values (species): 0 = setosa, 1 = versicolor, 2 = virginica | |
Y={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2} | |
-- Vamos fazer o nosso modelo | |
model = { | |
['test'] = {}, | |
['train'] = {}, | |
['split'] = 20, -- Definimos em quantos % vamos fazer 'split' no nosso modelo, | |
["k"] = 7 | |
} | |
function split(X, Y, model) | |
local splitOnI = #Y * ((100 - model.split) / 100) | |
local testI = 0 | |
for i in pairs(Y) do | |
local tableLocation = 'train' | |
local currentI = i | |
if i >= splitOnI then | |
currentI = (i - #model.train) | |
tableLocation = 'test' | |
end | |
model[tableLocation][currentI] = { | |
['label'] = Y[i], | |
['coord'] = {} | |
} | |
for k in pairs(X) do | |
table.insert(model[tableLocation][currentI]['coord'], X[k][i]) | |
end | |
end | |
return model | |
end | |
function value_to_label(value) | |
if value == 0 then | |
return 'setosa' | |
elseif value == 1 then | |
return 'versicolor' | |
end | |
return 'virginica' | |
end | |
function KNN_predict(input, model) | |
local distances = {} | |
local predicted = 0 | |
for i in pairs(model.train) do | |
local currentDistance = 0 | |
local coords = model.train[i]['coord'] | |
for _,v in pairs(input) do | |
currentDistance = currentDistance + ((v - coords[_]) * (v - coords[_])) | |
end | |
currentDistance = math.sqrt(currentDistance) | |
table.insert(distances, { | |
['value'] = currentDistance, | |
['label'] = model.train[i]['label'] | |
}) | |
end | |
table.sort(distances, function(a, b) return a.value < b.value end) | |
local knn = {} | |
for i = 1,model.k do | |
if knn[distances[i].label] then | |
knn[distances[i].label] = knn[distances[i].label] + 1 | |
else | |
knn[distances[i].label] = 1 | |
end | |
end | |
local knnMax = -1 | |
local index = 1 | |
for i in pairs(knn) do | |
if knn[i] > knnMax then | |
knnMax = knn[i] | |
index = i | |
end | |
end | |
return index | |
end | |
function KNN_score(inputs, model) | |
local tries = #inputs | |
local correntPredictions = 0 | |
for i,v in pairs(inputs) do | |
local predictedValue = KNN_predict(v.coord, model) | |
print(predictedValue, v.label) | |
if predictedValue == v.label then | |
correntPredictions = correntPredictions + 1 | |
end | |
end | |
return correntPredictions / tries | |
end | |
model = split(X, Y, model) | |
toPredict = {1.4, 0.2} | |
print('Predição de {1.4, 0.2}: ' .. KNN_predict(toPredict, model) .. '') | |
print('Acurácia do modelo: ' .. KNN_score(model.test, model)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment