task是包含数据和附加的一些信息的对象。比如目标变量,数据类型等等信息
使用mtcars数据集,建立一个回归任务,使用两个特征预测目标变量mpg(每公里油耗)的值
# 导入数据
data("mtcars", package = "datasets")
# 选择变量
data = mtcars[, 1:3]
# 查看数据类型
str(data)
## 'data.frame': 32 obs. of 3 variables:
## $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
## $ cyl : num 6 6 4 6 8 6 8 4 4 6 ...
## $ disp: num 160 160 108 258 360 ...
接下来建立回归任务,同时要给予这个任务一下的信息
id:任务的id,随便给 backend:数据 target:目标变量
library("mlr3")
# 使用的类的new方法
task_mtcars = TaskRegr$new(id = "cars", backend = data, target = "mpg")
print(task_mtcars)
## <TaskRegr:cars> (32 x 3)
## * Target: mpg
## * Properties: -
## * Features (2):
## - dbl (2): cyl, disp
print()方法给出了任务的简短总结:它有32个观察结果和3列。其中2列是特性,1列是目标。
使用mlr3viz包,绘制任务图 返回的是数据的相关图、分布图
library("mlr3viz")
autoplot(task_mtcars, type = "pairs")
## Registered S3 method overwritten by 'GGally':
## method from
## +.gg ggplot2
mlr3中有一些已经定义好的任务,用来学习
mlr_tasks
## <DictionaryTask> with 10 stored values
## Keys: boston_housing, breast_cancer, german_credit, iris, mtcars, pima,
## sonar, spam, wine, zoo
# 转换为table,可以有更多的信息
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: breast_cancer classif 683 10 0 0 0 0 0 9 0
## 3: german_credit classif 1000 21 0 3 0 0 14 3 0
## 4: iris classif 150 5 0 0 4 0 0 0 0
## 5: mtcars regr 32 11 0 0 10 0 0 0 0
## 6: pima classif 768 9 0 0 8 0 0 0 0
## 7: sonar classif 208 61 0 0 60 0 0 0 0
## 8: spam classif 4601 58 0 0 57 0 0 0 0
## 9: wine classif 178 14 0 2 11 0 0 0 0
## 10: zoo classif 101 17 15 1 0 0 0 0 0
使用get方法,选择特定的任务 这里选择鸢尾花的分类数据集
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
# 使用tsk方法,也可以得到鸢尾花的数据集
tsk("iris")
## <TaskClassif:iris> (150 x 5)
## * Target: Species
## * Properties: multiclass
## * Features (4):
## - dbl (4): Petal.Length, Petal.Width, Sepal.Length, Sepal.Width
# 查看个案数
task_iris$nrow
## [1] 150
# 查看变量数
task_iris$ncol
## [1] 5
#直接查看任务的数据
task_iris$data()
# 选择特定的行
task_iris$data(rows = c(1, 51, 101))
# 查询特征和目标变量
task_iris$feature_names
## [1] "Petal.Length" "Petal.Width" "Sepal.Length" "Sepal.Width"
task_iris$target_names
## [1] "Species"
# 要从任务中提取完整的数据,只需将其转换为data.table
summary(as.data.table(task_iris))
为行和列指定角色,这些roles决定着任务的一些特性
比如,前面构造的mtcars任务有以下列roles:
# 查看之前的列角色
print(task_mtcars$col_roles)
## $feature
## [1] "cyl" "disp"
## $target
## [1] "mpg"
## $name
## character(0)
## $order
## character(0)
## $stratum
## character(0)
## $group
## character(0)
## $weight
## character(0)
现在我们将mtcar的行名作为额外的一个特征
# 首先将rownames保存,然后转换为table
data = as.data.table(mtcars[, 1:3], keep.rownames = TRUE)
# 建立task
task = TaskRegr$new(id = "cars", backend = data, target = "mpg")
#此时,特征中就有了rn,也就是rownames一个新的变量
task$feature_names
## [1] "cyl" "disp" "rn"
多出的rn变量为新的特征
# 支持的列角色
names(task$col_roles)
## [1] "feature" "target" "name" "order" "stratum" "group" "weight"
# 将rn变量指定为name
task$set_col_roles("rn", roles = "name")
# 这个时候,特征names中就没有了rn变量
task$feature_names
## [1] "cyl" "disp"
# 同时rn也寸数据中去除
task$data(rows = 1:2)
对于行角色,一般用来指定训练集和测试集
这部分内容其实在建立任务之前进行,但是mlr3也提供了task的修改方法
$filter()
选取数据子集
$select()
选取子特征
task = tsk("iris")
# 选择特定的特征
task$select(c("Sepal.Width", "Sepal.Length"))
# 选择特定的行
task$filter(1:3)
# 查看
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
$rbind() 和 $cbind()
对任务添加额外的行和列
# 添加额外的行
task$cbind(data.table(foo = letters[1:3]))
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
library("mlr3viz")
# 建立任务,使用的为预置任务集
task = tsk("pima")
# 只选择前三个列名,也就是特征
task$select(head(task$feature_names, 3))
# 默认绘制频率图
autoplot(task)
# 绘制配对的图
# 显示的信息较多
autoplot(task, type = "pairs")
以上是对分类任务做的绘图,同时对于回归任务也是一样的,这里就不粘贴绘制的图片了
library("mlr3viz")
task = tsk("mtcars")
task$select(head(task$feature_names, 3))
autoplot(task)
autoplot(task, type = "pairs")
mlr3关于任务的建立更新完毕
love&peace