Feedback on first ML code

Hello, I’m trying to learn machine learning and it’s my first time building machine learning code without any help and also my first time doing something like this in lua. (previously in R)
I’d appreciate feedback, thanks.

P.S this code predicts gender based on the given height

local trainingSet = {
	male = {1.8,1.7,1.9,1.85,1.69,1.73,1.68,1.71},
	female = {1.6,1.55,1.65,1.49,1.56,1.64,1.7,1.68}
}

local trainingData = {boundary = {}, weight = {}}

local bestBoundary = {boundary = 0, weight = 0}

for n = 1.505, 1.805, 0.05  do
	local maleCount = 0 -- Males which are taller than the given height
	local femaleCount = 0 -- Females which are lower than the given height
	
	for _, height in pairs(trainingSet["male"]) do
		if height > n then
			maleCount += 1
		end
	end
	
	for _, height in pairs(trainingSet["female"]) do
		if height < n then
			femaleCount += 1
		end
	end
	
	local weight = maleCount + femaleCount
	
	table.insert(trainingData.boundary,n)
	table.insert(trainingData.weight, weight)
end

for k, weight in pairs(trainingData.weight) do
	if weight > bestBoundary.weight then
		bestBoundary.boundary = trainingData.boundary[k]
		bestBoundary.weight = weight
	end
end

print(bestBoundary)

local testingSet = {
	male = {1.71,1.72,1.98,1.82,1.69},
	female = {1.55,1.65,1.59,1.66,1.71}
}

local prediction = {}

for k, height in pairs(testingSet.male) do
	if height > bestBoundary.boundary then
		table.insert(prediction,{height,"male",true})
	else
		table.insert(prediction,{height,"female",false})
	end
end

for k, height in pairs(testingSet.female) do
	if height < bestBoundary.boundary then
		table.insert(prediction,{height,"female",true})
	else
		table.insert(prediction,{height,"male",false})
	end
end

print(prediction)

local fault = 0
local correct = 0

for _, v in pairs(prediction) do
	if v[3] then
		correct +=1
	else
		fault +=1
	end
end

local accuracy = 100-fault/correct

print("faults: "..fault)
print("correct: "..correct)
print("accuracy: "..accuracy)

Output:

  17:31:36.603   ▼  {
                    ["boundary"] = 1.655,
                    ["weight"] = 14
                 }  -  Client
  17:31:36.604   ▼  {
                    [1] =  ▼  {
                       [1] = 1.71,
                       [2] = "male",
                       [3] = true
                    },
                    [2] =  ▼  {
                       [1] = 1.72,
                       [2] = "male",
                       [3] = true
                    },
                    [3] =  ▼  {
                       [1] = 1.98,
                       [2] = "male",
                       [3] = true
                    },
                    [4] =  ▼  {
                       [1] = 1.82,
                       [2] = "male",
                       [3] = true
                    },
                    [5] =  ▼  {
                       [1] = 1.69,
                       [2] = "male",
                       [3] = true
                    },
                    [6] =  ▼  {
                       [1] = 1.55,
                       [2] = "female",
                       [3] = true
                    },
                    [7] =  ▼  {
                       [1] = 1.65,
                       [2] = "female",
                       [3] = true
                    },
                    [8] =  ▼  {
                       [1] = 1.59,
                       [2] = "female",
                       [3] = true
                    },
                    [9] =  ▼  {
                       [1] = 1.66,
                       [2] = "male",
                       [3] = false
                    },
                    [10] =  ▼  {
                       [1] = 1.71,
                       [2] = "male",
                       [3] = false
                    }
                 }  -  Client
  17:31:36.604  faults: 2  -  Client  -  LocalScript:79
  17:31:36.604  correct: 8  -  Client  -  LocalScript:80
  17:31:36.604  accuracy: 99.75  -  Client  -  LocalScript:81
1 Like

Looks good so far! Really only found one thing;

trainingSet["male"] can be replaced by trainingSet.male.

1 Like