Отчет по лабораторной работе №2.
library(caret)
## Загрузка требуемого пакета: ggplot2
## Загрузка требуемого пакета: lattice
set.seed(123)
x <- matrix(rnorm(50*5), ncol=5)
y <- factor(rep(c("A", "B"), 25))
featurePlot(x = x, y = y, plot = "pairs")
jpeg("featurePlot_pairs.jpg")
featurePlot(x = x, y = y, plot = "pairs")
dev.off()
## png
## 2
featurePlot(x = x, y = y, plot = "density")
jpeg("featurePlot_box.jpg")
featurePlot(x = x, y = y, plot = "density")
dev.off()
## png
## 2
featurePlot(x = x, y = y, plot = "box")
jpeg("featurePlot_box.jpg")
featurePlot(x = x, y = y, plot = "box")
dev.off()
## png
## 2
Разные типы графиков позволяют визуально оценить распределение признаков и различия между классами.
library(FSelector)
data(iris)
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
library(arules)
## Загрузка требуемого пакета: Matrix
##
## Присоединяю пакет: 'arules'
## Следующие объекты скрыты от 'package:base':
##
## abbreviate, write
data(iris)
iris$Sepal.Length_interval <-
discretize(iris$Sepal.Length, method = "interval", breaks = 3)
iris$Sepal.Length_frequency <-
discretize(iris$Sepal.Length, method = "frequency", breaks = 3)
iris$Sepal.Length_cluster <-
discretize(iris$Sepal.Length, method = "cluster", breaks = 3)
iris$Sepal.Length_fixed <-
discretize(iris$Sepal.Length, method = "fixed", breaks = c(-Inf, 5, 6, Inf))
print(head(iris))
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
## Sepal.Length_interval Sepal.Length_frequency Sepal.Length_cluster
## 1 [4.3,5.5) [4.3,5.4) [4.3,5.33)
## 2 [4.3,5.5) [4.3,5.4) [4.3,5.33)
## 3 [4.3,5.5) [4.3,5.4) [4.3,5.33)
## 4 [4.3,5.5) [4.3,5.4) [4.3,5.33)
## 5 [4.3,5.5) [4.3,5.4) [4.3,5.33)
## 6 [4.3,5.5) [5.4,6.3) [5.33,6.27)
## Sepal.Length_fixed
## 1 [5,6)
## 2 [-Inf,5)
## 3 [-Inf,5)
## 4 [-Inf,5)
## 5 [5,6)
## 6 [5,6)
Метод interval делит диапазон значений на равные по ширине интервалы. Это удобно и просто, однако при неравномерном распределении данных может приводить к тому, что в одних категориях будет значительно больше наблюдений, чем в других.
Метод frequency формирует интервалы так, чтобы в каждом находилось примерно одинаковое число объектов.
Метод cluster учитывает внутреннюю структуру данных. Такой подход позволяет лучше адаптироваться к естественным группировкам в данных и может давать более информативное разбиение.
Метод fixed использует заранее заданные границы. Этот способ наиболее интерпретируем, так как интервалы определяются экспертно.
если важна простота - подходит interval, если важна сбалансированность - frequency, если важна адаптация к данным - cluster, если есть экспертные знания - fixed.
library(Boruta)
library(mlbench)
data("Ozone")
Ozone <- na.omit(Ozone)
boruta_output <- Boruta(Ozone$V4 ~ ., data = Ozone, doTrace = 2)
## 1. run of importance source...
## 2. run of importance source...
## 3. run of importance source...
## 4. run of importance source...
## 5. run of importance source...
## 6. run of importance source...
## 7. run of importance source...
## 8. run of importance source...
## 9. run of importance source...
## 10. run of importance source...
## 11. run of importance source...
## After 11 iterations, +0.98 secs:
## confirmed 10 attributes: V1, V10, V11, V12, V13 and 5 more;
## rejected 1 attribute: V3;
## still have 2 attributes left.
## 12. run of importance source...
## 13. run of importance source...
## 14. run of importance source...
## 15. run of importance source...
## 16. run of importance source...
## 17. run of importance source...
## 18. run of importance source...
## After 18 iterations, +1.6 secs:
## rejected 2 attributes: V2, V6;
## no more attributes left.
print(boruta_output)
## Boruta performed 18 iterations in 1.552491 secs.
## 10 attributes confirmed important: V1, V10, V11, V12, V13 and 5 more;
## 3 attributes confirmed unimportant: V2, V3, V6;
boxplot(Ozone$V4 ~ Ozone$V1,
main="Boxplot of Ozone levels by V1", xlab="V1", ylab="Ozone")
С помощью пакета Boruta можно определить, какие признаки действительно важны для предсказания, а какие почти не влияют на результат. Алгоритм сравнивает реальные признаки со случайными и оставляет только те, которые показывают значимый вклад.