I have standard data: where rows are observations, and columns are features.
target colum_1 colum_2 colum_10 colum_100110 colum_499999999
[1,] 1 -0.35 -1.58 1.26 1.08 0.30
[2,] 1 -1.21 2.05 -0.95 1.59 -0.59
[3,] 1 -0.15 -1.63 0.63 -0.74 0.60
[4,] 0 0.78 0.55 -1.31 0.24 -0.22
[5,] 0 0.68 0.36 0.25 -0.23 1.73
[6,] 1 -0.32 1.07 -0.13 -0.31 -1.26
[7,] 1 -0.37 0.47 1.11 -1.14 -0.43
[8,] 1 -0.85 0.96 -1.61 0.62 0.06
[9,] 1 0.19 0.62 -1.28 1.31 0.30
[10,] 1 0.16 1.35 -0.11 1.14 -2.03
The problem is that there are so many features that I cannot run any learning algorithm. I'm familiar with dimensionality reduction algorithms, but for some reason I don't want to use them.
If I convert my data to a more compact form and create a new feature as a column id n_colum
, something like this:
target n_colum val
1 1 colum_1 -0.35
2 1 colum_2 -1.58
3 1 colum_10 1.26
4 1 colum_100110 1.08
5 1 colum_499999999 0.3
6 1 colum_1 -1.21
7 1 colum_2 2.05
8 1 colum_10 -0.95
9 1 colum_100110 1.59
10 1 colum_499999999 -0.59
11 1 colum_1 -0.15
12 1 colum_2 -1.63
13 1 colum_10 0.63
14 1 colum_100110 -0.74
15 1 colum_499999999 0.6
16 0 colum_1 0.78
17 0 colum_2 0.55
18 0 colum_10 -1.31
19 0 colum_100110 0.24
20 0 colum_499999999 -0.22
21 0 colum_1 0.68
22 0 colum_2 0.36
23 0 colum_10 0.25
24 0 colum_100110 -0.23
25 0 colum_499999999 1.73
26 1 colum_1 -0.32
27 1 colum_2 1.07
28 1 colum_10 -0.13
29 1 colum_100110 -0.31
30 1 colum_499999999 -1.26
31 1 colum_1 -0.37
32 1 colum_2 0.47
33 1 colum_10 1.11
34 1 colum_100110 -1.14
35 1 colum_499999999 -0.43
36 1 colum_1 -0.85
37 1 colum_2 0.96
38 1 colum_10 -1.61
39 1 colum_100110 0.62
40 1 colum_499999999 0.06
41 1 colum_1 0.19
42 1 colum_2 0.62
43 1 colum_10 -1.28
44 1 colum_100110 1.31
45 1 colum_499999999 0.3
46 1 colum_1 0.16
47 1 colum_2 1.35
48 1 colum_10 -0.11
49 1 colum_100110 1.14
50 1 colum_499999999 -2.03
If I train an algorithm on compact data, will I lose performance or something, or is it equivalent to the first option?
================UPD===================
Wrote a small prototype of the idea.. sorry for the messy code, i haven't had coffee yet
so I remade the dataset into a compact form
ir <- iris[ sample(1:150,150) , ]
# make data
dat <- as.data.frame(matrix(ncol = 3,nrow = 0))
for(i in 1:nrow(ir)){
nc <- ncol(ir)-1
tmp_dat <- cbind.data.frame(
target = rep( ir$Species[i] , nc),
n_colum = 1:nc,
value = unlist(ir[i,-5])
)
dat <- rbind.data.frame(dat, tmp_dat)
}
row.names(dat) <- NULL
head(ir)
head(dat,20)
...
target n_colum value
1 setosa 1 4.8
2 setosa 2 3.4
3 setosa 3 1.6
4 setosa 4 0.2
5 setosa 1 5.7
6 setosa 2 4.4
7 setosa 3 1.5
8 setosa 4 0.4
9 versicolor 1 5.6
10 versicolor 2 3.0
11 versicolor 3 4.5
12 versicolor 4 1.5
13 virginica 1 6.9
14 virginica 2 3.1
15 virginica 3 5.1
16 virginica 4 2.3
next I train the model
Y <- dat$target
X <- dat[,-1]
# train model
tr <- 1:500
ts <- 501:nrow(X)
table(Y[tr])
library(randomForest)
rf <- randomForest(Y[tr]~., X[tr,])
then I make a prediction, the prediction is made immediately on four rows, because the original data has four columns
# predict
result <- as.data.frame(matrix(ncol = 2,nrow = 0))
for(i in ts){
if(X$n_colum[i]==4){
X_rows <- X[(i-3):i, ]
# X_rows
# n_colum value
# 597 1 6.7
# 598 2 3.3
# 599 3 5.7
# 600 4 2.1
pr <- predict( rf , X_rows , t="prob")
pr <- cbind.data.frame(
predicted = as.factor(names(which.max(colMeans(pr)))),
original = Y[i]
)
result <- rbind(result , pr)
}
}
print(result)
predicted original
1 versicolor versicolor
2 versicolor versicolor
3 versicolor versicolor
4 setosa setosa
5 versicolor versicolor
6 setosa setosa
7 versicolor versicolor
8 virginica virginica
9 virginica virginica
10 virginica virginica
11 versicolor versicolor
12 setosa setosa
13 virginica virginica
14 virginica virginica
15 virginica virginica
16 versicolor versicolor
17 versicolor versicolor
18 virginica virginica
19 virginica virginica
20 setosa setosa
21 virginica virginica
22 setosa setosa
23 virginica virginica
24 virginica virginica
25 setosa setosa
If I didn’t make any mistakes, then it turns out that you can train the model in this way, and this is very good))