Là phần meta-data gồm các thông tin như: dataset, target variable, loại mô hình TaskClassif hoặc TaskRegr
Để tạo 1 task từ a data.frame() or data.table() object, the task type cần phải được chỉ rõ:
Classification Task: The target is a label (stored as character()orfactor()) with only few distinct values. → mlr3::TaskClassif
Regression Task: The target is a numeric quantity (stored as integer() or double()). → mlr3::TaskRegr
Survival Task: The target is the (right-censored) time to an event. → mlr3proba::TaskSurv in add-on package mlr3proba
Ordinal Regression Task: The target is ordinal. → mlr3ordinal::TaskOrdinal in add-on package mlr3ordinal
Ví dụ tạo 1 task class sử dụng dữ liệu germancredit của package scorecard, biến mục tiêu là “creditability” gồm 2 phân nhóm là good và bad
Đầu tiên, load và prepare the data
data("germancredit", package = "scorecard")
str(germancredit)
## 'data.frame': 1000 obs. of 21 variables:
## $ status.of.existing.checking.account : Factor w/ 4 levels "... < 0 DM","0 <= ... < 200 DM",..: 1 2 4 1 1 4 4 2 4 2 ...
## $ duration.in.month : num 6 48 12 42 24 36 24 36 12 30 ...
## $ credit.history : Factor w/ 5 levels "no credits taken/ all credits paid back duly",..: 5 3 5 3 4 3 3 3 3 5 ...
## $ purpose : chr "radio/television" "radio/television" "education" "furniture/equipment" ...
## $ credit.amount : num 1169 5951 2096 7882 4870 ...
## $ savings.account.and.bonds : Factor w/ 5 levels "... < 100 DM",..: 5 1 1 1 1 5 3 1 4 1 ...
## $ present.employment.since : Factor w/ 5 levels "unemployed","... < 1 year",..: 5 3 4 4 3 3 5 3 4 1 ...
## $ installment.rate.in.percentage.of.disposable.income : num 4 2 2 2 3 2 3 2 2 4 ...
## $ personal.status.and.sex : Factor w/ 5 levels "male : divorced/separated",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ other.debtors.or.guarantors : Factor w/ 3 levels "none","co-applicant",..: 1 1 1 3 1 1 1 1 1 1 ...
## $ present.residence.since : num 4 2 3 4 4 4 4 2 4 2 ...
## $ property : Factor w/ 4 levels "real estate",..: 1 1 1 2 4 4 2 3 1 3 ...
## $ age.in.years : num 67 22 49 45 53 35 53 35 61 28 ...
## $ other.installment.plans : Factor w/ 3 levels "bank","stores",..: 3 3 3 3 3 3 3 3 3 3 ...
## $ housing : Factor w/ 3 levels "rent","own","for free": 2 2 2 3 3 3 2 1 2 2 ...
## $ number.of.existing.credits.at.this.bank : num 2 1 1 1 2 1 1 1 1 2 ...
## $ job : Factor w/ 4 levels "unemployed/ unskilled - non-resident",..: 3 3 2 3 3 2 3 4 2 4 ...
## $ number.of.people.being.liable.to.provide.maintenance.for: num 1 1 2 2 2 2 1 1 1 1 ...
## $ telephone : Factor w/ 2 levels "none","yes, registered under the customers name": 2 1 1 1 1 2 1 2 1 1 ...
## $ foreign.worker : Factor w/ 2 levels "yes","no": 1 1 1 1 1 1 1 1 1 1 ...
## $ creditability : Factor w/ 2 levels "bad","good": 2 1 2 2 1 2 2 2 2 1 ...
sel_dt <- germancredit[, c(1:5, 21)]
Tiếp tục ta tạo task theo câu lệnh TaskClassif$new
id: Định danh tùy ý, sử dụng cho các summary. backend: Tham số này cho phép định nghĩa cách sử dụng tập dữ liệu. Ở đây tôi đơn giản cung cấp dataset và nó tự động chuyển sang DataBackendDataTable. target: Tên của biến mục tiêu
library(mlr3verse)
tsk_sc <- TaskClassif$new(id = "sc", backend = sel_dt, target = "creditability")
print(tsk_sc)
## <TaskClassif:sc> (1000 x 6)
## * Target: creditability
## * Properties: twoclass
## * Features (5):
## - dbl (2): credit.amount, duration.in.month
## - fct (2): credit.history, status.of.existing.checking.account
## - chr (1): purpose
print() cung cấp những thông tin cơ bản của task: số biến, số quan sát …
Ngoài ra, ta cũng có thể sử dụng package mlr3viz để đưa ra những graph về các thuộc tính của các features
library(mlr3viz)
autoplot(tsk_sc, type = "pairs")
## Registered S3 method overwritten by 'GGally':
## method from
## +.gg ggplot2
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
mlr3 cung cấp vài tasks định nghĩa trước. Tất cả task được lưu trong R6 Dictionary tên là mlr_tasks.
mlr_tasks
## <DictionaryTask> with 9 stored values
## Keys: boston_housing, german_credit, iris, mtcars, pima, sonar, spam,
## wine, zoo
Ta cũng có thể chuyển đổi 1 task thành dataset object bằng cách
library(data.table)
as.data.table(mlr_tasks)
## key task_type nrow ncol lgl int dbl chr fct ord pxc
## 1: boston_housing regr 506 19 0 3 13 0 2 0 0
## 2: german_credit classif 1000 21 0 0 7 0 12 1 0
## 3: iris classif 150 5 0 0 4 0 0 0 0
## 4: mtcars regr 32 11 0 0 10 0 0 0 0
## 5: pima classif 768 9 0 0 8 0 0 0 0
## 6: sonar classif 208 61 0 0 60 0 0 0 0
## 7: spam classif 4601 58 0 0 57 0 0 0 0
## 8: wine classif 178 14 0 2 11 0 0 0 0
## 9: zoo classif 101 17 15 1 0 0 0 0 0
Để lấy 1 task tử dictionary, có thể sử dụng phương thức $get() từ mlr_tasks và gán vào object mới. Ví dụ
task_iris <- mlr_tasks$get("iris")
print(task_iris)
## <TaskClassif:iris> (150 x 5)
## * Target: Species
## * Properties: multiclass
## * Features (4):
## - dbl (4): Petal.Length, Petal.Width, Sepal.Length, Sepal.Width
Cách khác, có thể dụng hàm tsk(), cũng tạo được 1 task từ dictionary
tsk("iris")
## <TaskClassif:iris> (150 x 5)
## * Target: Species
## * Properties: multiclass
## * Features (4):
## - dbl (4): Petal.Length, Petal.Width, Sepal.Length, Sepal.Width
Tất cả thuộc tính và đặc điểm của task có thể được truy vấn sử dụng thuộc tính (fields) public và phương thức
Dữ liệu được lưu trong 1 task có thể được lấy trực tiếp từ fields, ví dụ:
task_iris$nrow
## [1] 150
task_iris$ncol
## [1] 5
Thông tin thêm có thể có được qua phương thức của object, ví dụ:
task_iris$data()
## Species Petal.Length Petal.Width Sepal.Length Sepal.Width
## 1: setosa 1.4 0.2 5.1 3.5
## 2: setosa 1.4 0.2 4.9 3.0
## 3: setosa 1.3 0.2 4.7 3.2
## 4: setosa 1.5 0.2 4.6 3.1
## 5: setosa 1.4 0.2 5.0 3.6
## ---
## 146: virginica 5.2 2.3 6.7 3.0
## 147: virginica 5.0 1.9 6.3 2.5
## 148: virginica 5.2 2.0 6.5 3.0
## 149: virginica 5.4 2.3 6.2 3.4
## 150: virginica 5.1 1.8 5.9 3.0
Trong mlr3, mỗi dòng là đơn nhất và được xác định băng 1 giá trị integer
iris sư dụng giá trị nguyên cho row_ids
head(task_iris$row_ids)
## [1] 1 2 3 4 5 6
lấy các dòng 1, 51, 101
task_iris$data(rows = c(1, 51, 101))
## Species Petal.Length Petal.Width Sepal.Length Sepal.Width
## 1: setosa 1.4 0.2 5.1 3.5
## 2: versicolor 4.7 1.4 7.0 3.2
## 3: virginica 6.0 2.5 6.3 3.3
mtcars task sử dụng character làm row_ids
task_mtcars <- tsk("mtcars")
head(task_mtcars$row_ids)
## [1] "AMC Javelin" "Cadillac Fleetwood" "Camaro Z28"
## [4] "Chrysler Imperial" "Datsun 710" "Dodge Challenger"
task_mtcars$data(rows = "Datsun 710")
## mpg am carb cyl disp drat gear hp qsec vs wt
## 1: 22.8 1 1 4 108 3.85 4 93 18.61 1 2.32
Chú ý, method $data() chỉ cho phép đọc, không cho phép modify
Tương tự, mỗi cột cũng được xác định bằng giá trị nguyên hoặc character, tên được lưu thành feature_names và target_names
task_iris$feature_names
## [1] "Petal.Length" "Petal.Width" "Sepal.Length" "Sepal.Width"
task_iris$target_names
## [1] "Species"
Có thể kết hợp row_ids và column names để chọn tập con của dữ liệu
task_iris$data(rows = c(1, 51, 101), cols = "Species")
## Species
## 1: setosa
## 2: versicolor
## 3: virginica
Để xuất toàn bộ dữ liệu từ task, đơn giản chỉ việc convert sang dạng data.table
summary(as.data.table(task_iris))
## Species Petal.Length Petal.Width Sepal.Length
## setosa :50 Min. :1.000 Min. :0.100 Min. :4.300
## versicolor:50 1st Qu.:1.600 1st Qu.:0.300 1st Qu.:5.100
## virginica :50 Median :4.350 Median :1.300 Median :5.800
## Mean :3.758 Mean :1.199 Mean :5.843
## 3rd Qu.:5.100 3rd Qu.:1.800 3rd Qu.:6.400
## Max. :6.900 Max. :2.500 Max. :7.900
## Sepal.Width
## Min. :2.000
## 1st Qu.:2.800
## Median :3.000
## Mean :3.057
## 3rd Qu.:3.300
## Max. :4.400
Có thể gán vai trò cho dòng và cột. Những vai trò ảnh hưởng tới hoạt động của task. Rộng hơn, roles cung cấp thêm thông tin meta-data
Ví dụ
print(task_mtcars$col_roles)
## $feature
## [1] "am" "carb" "cyl" "disp" "drat" "gear" "hp" "qsec" "vs" "wt"
##
## $target
## [1] "mpg"
##
## $name
## character(0)
##
## $order
## character(0)
##
## $stratum
## character(0)
##
## $group
## character(0)
##
## $weight
## character(0)
Để thêm dòng như 1 tính năng bổ sung, t thêm chúng vào data table và tạo lại task
# with `keep.rownames`, data.table stores the row names in an extra column "rn"
data <- as.data.table(mtcars[, 1:3], keep.rownames = TRUE)
task <- TaskRegr$new(id = "cars", backend = data, target = "mpg")
row_ids của task
task$row_ids
## [1] 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] 26 27 28 29 30 31 32
task$feature_names
## [1] "cyl" "disp" "rn"
Feature mới tên là “rn”. Feature này chỉ sử dụng để hiểu thêm về cách add roles, không có nhiều ý nghĩa trong việc dự báo. Để bỏ nó ra khỏi roles feature làm như sau:
task$col_roles$name = "rn"
task$col_roles$feature = setdiff(task$col_roles$feature, "rn")
“rn” không năm trong list feature nữa
task$feature_names
## [1] "cyl" "disp"
Và cũng không xuất hiện khi tiếp cận data
task$data(rows = 1:2)
## mpg cyl disp
## 1: 21 6 160
## 2: 21 6 160
task$head(2)
## mpg cyl disp
## 1: 21 6 160
## 2: 21 6 160
Thay đổi vai trò không làm thay đổi nền tảng dữ liệu, sự thay đổi chỉ làm thay đổi khi view nó. Tức là chỉ thay đổi task
Giống như cột, cũng có thể đặt các roles cho hàng, hàng có thể có 2 loại roles:
Role sử dụng ROWS được sử dụng cho model fitting (mặc dù có thể sử dụng như tập test khi resampling). Role này là role mặc định
Role validation Rows cái mà không sử dụng cho training. Dòng mà bị missing giá trị của biến target trong khi tạo task sẽ tự động chuyển thảnh role validation.
Như ở trên, việc modifying $col_roles hay $row_roles thay đổi view on the data. Các phương pháp tiện tích $filter(), $select(), chọn dòng hoặc cột
task = tsk("iris")
task$select(c("Sepal.Width", "Sepal.Length")) # keep only these features
task$filter(1:3) # keep only these rows
task$head()
## Species Sepal.Length Sepal.Width
## 1: setosa 5.1 3.5
## 2: setosa 4.9 3.0
## 3: setosa 4.7 3.2
Ngoài phương pháp subset data đã được thảo luận ở trên, phương pháp $rbind() và $cbind() có phép thêm dòng và cột vào task. Original data không thay đổi, thêm dòng hay cột chỉ ảnh hưởng tới view data trong task
task$cbind(data.table(foo = letters[1:3])) # add column foo
task$head()
## Species Sepal.Length Sepal.Width foo
## 1: setosa 5.1 3.5 a
## 2: setosa 4.9 3.0 b
## 3: setosa 4.7 3.2 c
mlr3viz cung cấp các biểu đồ cơ bản để thực hiện mlr3. Loại plot phụ thuộc vào inherited class, nhưng tất cả plot được trả về dưới dạng ggplot2 object, rất dễ để customized
Với classification task (kế thừa từ TaskClassif), xem hướng dẫn mlr3viz::autoplot.TaskClassif. Dưới đây là một vài ví dụ
library(mlr3viz)
# get the pima indians task
task = tsk("pima")
# subset task to only use the 3 first features
task$select(head(task$feature_names, 3))
# default plot: class frequencies
autoplot(task)
# pairs plot (requires package GGally)
autoplot(task, type = "pairs")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
# duo plot (requires package GGally)
autoplot(task, type = "duo")
Tất nhiên, với regression task (inheriting from TaskRegr) xem hướng dẫn mlr3viz::autoplot.TaskRegr
library(mlr3viz)
# get the boston housing task
task = tsk("mtcars")
# subset task to only use the 3 first features
task$select(head(task$feature_names, 3))
# default plot: boxplot of target variable
autoplot(task)
# pairs plot (requires package GGally)
autoplot(task, type = "pairs")