Задание 1.1 CARET и featurePlot()
library(caret)
## Загрузка требуемого пакета: ggplot2
## Загрузка требуемого пакета: lattice
models <- names(getModelInfo())
length(models)
## [1] 239
head(models, 10)
## [1] "ada" "AdaBag" "AdaBoost.M1" "adaboost" "amdai"
## [6] "ANFIS" "avNNet" "awnb" "awtan" "bag"
set.seed(123)
x <- matrix(rnorm(50 * 5), ncol = 5)
y <- factor(rep(c("A", "B"), 25))
colnames(x) <- paste0("X", 1:5)
jpeg("featurePlot_pairs.jpg", width = 800, height = 800)
featurePlot(x = x, y = y, plot = "pairs")
dev.off()
## png
## 2
featurePlot(x = x, y = y, plot = "density")
jpeg("featurePlot_pairs.jpg", width = 800, height = 800) # Сохраняем в файл
featurePlot(x = x, y = y, plot = "pairs")
dev.off() # Закрываем файл
## png
## 2
featurePlot(x = x, y = y, plot = "pairs")
jpeg("featurePlot_box.jpg", width = 800, height = 800)
featurePlot(x = x, y = y, plot = "box")
dev.off()
## png
## 2
featurePlot(x = x, y = y, plot = "box")
Задание 2. Определение важности признаков с помощью FSelector
library(FSelector)
data(iris)
str(iris)
## 'data.frame': 150 obs. of 5 variables:
## $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
weights <- information.gain(Species ~ ., data = iris)
print(weights)
## attr_importance
## Sepal.Length 0.4521286
## Sepal.Width 0.2672750
## Petal.Length 0.9402853
## Petal.Width 0.9554360
best_features <- cutoff.k(weights, 2)
print(best_features)
## [1] "Petal.Width" "Petal.Length"
formula <- as.simple.formula(best_features, "Species")
print(formula)
## Species ~ Petal.Width + Petal.Length
## <environment: 0x0000026fe82b17e8>
Задание 3. Дискретизация непрерывной переменной с помощью arules
library(arules)
## Загрузка требуемого пакета: Matrix
##
## Присоединяю пакет: 'arules'
## Следующие объекты скрыты от 'package:base':
##
## abbreviate, write
x <- iris$Sepal.Length
# 1) Метод "interval" (равная ширина интервала)
# Разобьем на 3 интервала
d_interval <- discretize(x, method = "interval", breaks = 3)
print("Метод: interval (равная ширина)")
## [1] "Метод: interval (равная ширина)"
table(d_interval)
## d_interval
## [4.3,5.5) [5.5,6.7) [6.7,7.9]
## 52 70 28
# 2) Метод "frequency" (равная частота)
# Постарается сделать по ~50 наблюдений в каждой группе
d_frequency <- discretize(x, method = "frequency", breaks = 3)
print("Метод: frequency (равная частота)")
## [1] "Метод: frequency (равная частота)"
table(d_frequency)
## d_frequency
## [4.3,5.4) [5.4,6.3) [6.3,7.9]
## 46 53 51
# 3) Метод "cluster" (кластеризация k-means)
d_cluster <- discretize(x, method = "cluster", breaks = 3)
print("Метод: cluster (кластеризация)")
## [1] "Метод: cluster (кластеризация)"
table(d_cluster)
## d_cluster
## [4.3,5.42) [5.42,6.39) [6.39,7.9]
## 52 56 42
# 4) Метод "fixed" (фиксированные границы)
# Вручную зададим границы: до 5.5, от 5.5 до 6.5, и больше 6.5
d_fixed <- discretize(x, method = "fixed", breaks = c(-Inf, 5.5, 6.5, Inf))
print("Метод: fixed (фиксированные границы)")
## [1] "Метод: fixed (фиксированные границы)"
table(d_fixed)
## d_fixed
## [-Inf,5.5) [5.5,6.5) [6.5, Inf]
## 52 63 35
Задание 4. Отбор признаков с помощью пакета Boruta
library(Boruta)
library(mlbench)
data(Ozone)
ozone_clean <- na.omit(Ozone)
set.seed(1)
boruta_output <- Boruta(V4 ~ ., data = ozone_clean, doTrace = 0)
print(attStats(boruta_output))
## meanImp medianImp minImp maxImp normHits decision
## V1 9.421103 9.385010 7.41255937 10.97348865 1.00000000 Confirmed
## V2 1.218360 1.121869 -0.60597411 3.85947931 0.20000000 Rejected
## V3 -1.688569 -2.102193 -3.38979396 0.08985086 0.00000000 Rejected
## V5 9.236169 9.266195 7.86852899 10.60991358 1.00000000 Confirmed
## V6 1.072832 1.161104 -0.03611639 2.49945980 0.06666667 Rejected
## V7 11.814686 11.971285 10.11379823 13.11057689 1.00000000 Confirmed
## V8 17.056010 17.151122 15.79266823 18.19135482 1.00000000 Confirmed
## V9 19.372264 19.473118 17.96293301 21.03182151 1.00000000 Confirmed
## V10 10.150333 10.111378 8.91430957 11.56744652 1.00000000 Confirmed
## V11 12.061917 12.025537 10.09678496 13.02013521 1.00000000 Confirmed
## V12 14.899516 14.794931 13.54416208 16.21840365 1.00000000 Confirmed
## V13 9.359732 9.259294 8.29690055 10.98103696 1.00000000 Confirmed
plot(boruta_output, cex.axis = 0.7, las = 2, main = "Отбор признаков Boruta для набора Ozone")
jpeg("boruta_ozone_boxplot.jpg", width = 1000, height = 600)
plot(boruta_output, cex.axis = 0.7, las = 2, main = "Отбор признаков Boruta для набора Ozone")
dev.off()
## png
## 2