Skip to content

Instantly share code, notes, and snippets.

View z1nc0r3's full-sized avatar
🏠
Work From Home

Lasith Manujitha z1nc0r3

🏠
Work From Home
View GitHub Profile
@z1nc0r3
z1nc0r3 / invertData.m
Created September 5, 2023 23:38
Invert Data for SVM
function data = invertData(data, class)
indices = (data(:, end) == class);
data(indices, end) = 1;
data(~indices, end) = -1;
@z1nc0r3
z1nc0r3 / WinnerTakesAll.m
Last active September 5, 2023 23:28
Winner Takes All algorithm using Matlab
function accuracy = WinnerTakesAll(actual, predict, class)
[m, n] = size(actual);
[maxP, classP] = max(predict, [], 2);
accuracy = 100 * sum(actual(:, n) == class(classP)') / m;
@z1nc0r3
z1nc0r3 / ovasvm.m
Created September 5, 2023 04:51
One vs All SVM algorithm using Matlab
function accuracy = ovasvm(data, n)
data = shuffleData(data);
[train, test] = splitData(data);
[trainSet, valSet] = splitData(train);
[trainSet, valSet] = scaleData(trainSet, valSet);
A = 1:n;
C = [2^-10 2^-9 2^-8 2^-7 2^-6 2^-5 2^-4 2^-3 2^-2 2^-1 2^0 2^1 2^2 2^3 2^4 2^5 2^6 2^7 2^8 2^9 2^10];
accuracy = [];
@z1nc0r3
z1nc0r3 / shuffleData.m
Created September 4, 2023 16:50
Shuffle data using Matlab
function data = shuffleData(data)
[m, n] = size(data);
indices = randperm(m);
data = data(indices, :);
@z1nc0r3
z1nc0r3 / splitData.m
Created September 4, 2023 16:50
Split Data into Train and Test set using Matlab
function [train, test] = splitData(data)
[m, n] = size(data);
train = [];
test = [];
nTrain = round(m * 0.7);
train = data(1:nTrain, :);
test = data(nTrain + 1:m, :);
@z1nc0r3
z1nc0r3 / scaleData.m
Created September 4, 2023 16:49
Scale Data using Matlab
function [train, test] = scaleData(train, test)
[row, col] = size(train);
normalMax = 1;
normalMin = -1;
for i=1:(col-1)
realMax = max(train(:, i));
realMin = min(train(:, i));
@z1nc0r3
z1nc0r3 / knn.m
Created September 4, 2023 16:49
K-NN algorithm using Matlab
function rate = knn(trainSet, testSet, k)
m = size(trainSet, 1);
n = size(testSet, 1);
predict = [];
for test=1:n
for train=1:m
Eulidist(train) = norm(testSet(test, 1:end-1) - trainSet(train, 1:end-1));
end
@z1nc0r3
z1nc0r3 / bresenhamLine.cpp
Created February 6, 2023 19:13
Bresenham's Line Algorithm
void bresenhamLine(int x0, int y0, int x1, int y1) {
int dx = abs(x1 - x0);
int dy = abs(y1 - y0);
int x, y;
if (dx >= dy) {
int d = 2 * dy - dx;
int ds = 2 * dy;
int dt = 2 * (dy - dx);
@z1nc0r3
z1nc0r3 / ddaLine.cpp
Last active February 6, 2023 19:14
DDA Line Algorithm
void ddaLine(float xa, float ya, float xb, float yb) {
int dx = xb - xa, dy = yb - ya;
int k, steps;
float xinc, yinc, x = xa, y = ya;
if (abs(dx) > abs(dy)) {
steps = abs(dx);
}
else {
steps = abs(dy);
@z1nc0r3
z1nc0r3 / BresenhamCircle.cpp
Created February 1, 2023 03:09
Bresenham's Circle Algorithm
#include <glut.h>
#include <math.h>
#include <cmath>
int width = 600, height = 600;
int xOg = 200, yOg = 200;
int xa, ya, xb, yb;
void putPixel(int x, int y) {
glColor3f(1.0f, 1.0f, 1.0f);