File: test_base_normalizeFeatures.R

package info (click to toggle)
r-cran-mlr 2.19.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 8,392 kB
  • sloc: ansic: 65; sh: 13; makefile: 5
file content (29 lines) | stat: -rwxr-xr-x 1,130 bytes parent folder | download | duplicates (2)
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
test_that("normalizeFeatures", {
  df = data.frame(x1 = c(0, -1, 4, 2, 3), x2 = letters[1:5],
    target = letters[1:5], stringsAsFactors = TRUE)
  task = makeClassifTask(data = df, target = "target")

  normalized = normalizeFeatures(df, method = "range", range = c(-1, 4))
  expect_equal(normalized, df)

  normalized = normalizeFeatures(task, method = "range", range = c(-1, 4))
  normed.x = getTaskData(normalizeFeatures(task, method = "center"))$x1
  expect_equal(mean(normed.x), 0)
  # comparing envs leads to test failures
  normalized$env = NULL
  task$env = NULL
  expect_equal(normalized, task)


  normed.task = normalizeFeatures(multiclass.task, method = "range")
  expect_equal(normed.task$task.desc, multiclass.task$task.desc)
  expect_equal(range(getTaskData(normed.task)$Sepal.Width), c(0, 1))

  err.message = "Assertion on 'cols' failed: Must be a subset of {'x1'}"

  expect_error(normalizeFeatures(df, cols = "x2"), err.message, fixed = TRUE)

  expect_error(normalizeFeatures(df, cols = "target"), err.message, fixed = TRUE)

  expect_error(normalizeFeatures(df, cols = "x100"), err.message, fixed = TRUE)
})