-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_preprocessing.m
81 lines (67 loc) · 2.1 KB
/
data_preprocessing.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
clear; clc;
% load the dataset
data = readtable("abalone.data","FileType","text");
% select input feature
features = ["gender", "length", "diameter", "height", "total_weight", "flesh_weight", "gut_weight", "shell_weight", "age"];
data.Properties.VariableNames = features;
% split into training and testing (you can change the ratio and input data)
input_features = []; % insert your own features!!
outputs = ["age"];
train_ratio = 0.80; % the remaining will be used for testing
[trainX, trainY, testX, testY] = data_split(data, input_features, outputs, train_ratio);
%%
function [trainX, trainY, testX, testY] = data_split(data, input_features, outputs, train_ratio)
Y = [];
X = [];
X_gend=[];
Y_gend=[];
delete_i = 0;
for i = 1:length(input_features)
feature = input_features(i);
if feature == "gender"
X_gend = encode_gender(data);
delete_i = i;
else
X_temp = data{:, feature};
X(:,i) = X_temp;
end
end
if ~isempty(X_gend)
col = size(X, 2);
X(:,col+1:col+3) = X_gend;
if col > 0
X(:,delete_i) = [];
end
end
for j = 1:length(outputs)
output = outputs(j);
if output == "gender"
Y_gend = encode_gender(data);
delete_i = j;
else
Y_temp = data{:, output};
Y(:,j) = Y_temp;
end
end
if ~isempty(Y_gend)
col = size(Y, 2);
Y(:,col+1:col+3) = Y_gend;
if col > 0
Y(:,delete_i) = [];
end
end
rr = randperm(height(data));
trainIdx = floor(height(data)*train_ratio);
trainX = X(rr(1:trainIdx), :)';
trainY = Y(rr(1:trainIdx), :)';
testX = X(rr(1+trainIdx:end), :)';
testY = Y(rr(1+trainIdx:end), :)';
end
%%
function [res] = encode_gender(data)
labels = cellstr(data.gender);
labels = categorical(labels);
%categories(labels)
% result will be in this order: F, I, M
res = onehotencode(labels, 2);
end