在Rstudio中使用python

library(reticulate)

机器学习

mlr3

library(mlr3verse)
## 载入需要的程序包:mlr3
#usethis::edit_r_profile()
#如果您想要任何我们包的最新开发版本,请运行
#remotes::install_gitub('mlr-org/{pkg}')
#其中 {pkg} 替换为您要安装的包名称。您可以在 ("https://github.com/mlr-org/mlr3/wiki/Extension-Packages") 查看我们所有扩展包的最新列表。

mlr3 示例

library(mlr3)
task = tsk('penguins')#在penguins数据集的一个子集训练一个决策树
split = partition(task)
learner = lrn('classif.rpart')

learner$train(task, row_ids = split$train)
learner$model
## n= 231 
## 
## node), split, n, loss, yval, (yprob)
##       * denotes terminal node
## 
## 1) root 231 129 Adelie (0.44155844 0.19913420 0.35930736)  
##   2) flipper_length< 207.5 143  43 Adelie (0.69930070 0.30069930 0.00000000)  
##     4) bill_length< 43.35 99   1 Adelie (0.98989899 0.01010101 0.00000000) *
##     5) bill_length>=43.35 44   2 Chinstrap (0.04545455 0.95454545 0.00000000) *
##   3) flipper_length>=207.5 88   5 Gentoo (0.02272727 0.03409091 0.94318182) *
prediction = learner$predict(task, row_ids = split$test)#在其余数据进行预测,然后使用准确度度量评估这些预测
prediction
## <PredictionClassif> for 113 observations:
##     row_ids     truth  response
##           1    Adelie    Adelie
##           4    Adelie    Adelie
##          10    Adelie    Adelie
## ---                            
##         332 Chinstrap Chinstrap
##         334 Chinstrap Chinstrap
##         335 Chinstrap Chinstrap
prediction$score(msr('classif.acc'))
## classif.acc 
##    0.920354

data.table入门

library(data.table)
#使用as.data.table转换矩阵
as.data.table(matrix(runif(4), 2, 2))
##           V1        V2
##        <num>     <num>
## 1: 0.3819389 0.4319213
## 2: 0.4795237 0.6449404
#使用data.table
dt = data.table(x = 1:6, y = rep(letters[1:3], each = 2))


#按照y分组后给出的组中x列的平均值
dt[, mean(x), by = 'y']
##         y    V1
##    <char> <num>
## 1:      a   1.5
## 2:      b   3.5
## 3:      c   5.5
#使用:=添加新列
dt[, z := x * 3]
dt
##        x      y     z
##    <int> <char> <num>
## 1:     1      a     3
## 2:     2      a     6
## 3:     3      b     9
## 4:     4      b    12
## 5:     5      c    15
## 6:     6      c    18
#可以使用应用语义,具体查看datatable包说明文档
data.table::copy(dt)
##        x      y     z
##    <int> <char> <num>
## 1:     1      a     3
## 2:     2      a     6
## 3:     3      b     9
## 4:     4      b    12
## 5:     5      c    15
## 6:     6      c    18

mlr3的基本操作{mlr3-utilities}

#suger创建对象
#mlr_tasks_mtcars变成了tsk('mtcars')

#dictionnaries

Task

Tasks are objects that contain the data and additional metadata that define a machine learning problem. The metadata contain, for example, the name of the target feature for supervised machine learning problems. This information is extracted(提取) automatically(自动地) when required, so the user does not have to specify(v.明确指出) the prediction target (every time a model is trained).

Constructing Tasks

mlr3 includes a few predefined machine learning tasks in the mlr_tasks.Dictionary.

library(mlr3verse)
mlr_tasks
## <DictionaryTask> with 21 stored values
## Keys: ames_housing, bike_sharing, boston_housing, breast_cancer,
##   german_credit, ilpd, iris, kc_housing, moneyball, mtcars, optdigits,
##   penguins, penguins_simple, pima, ruspini, sonar, spam, titanic,
##   usarrests, wine, zoo

To get a task from the dictionary, use the tsk() function and assign(V.分配,赋值) the return value(n.返回值) to a new variable. Below we retrieve(v.检索), which uses the mtcarsdataset(n.数据集):tsk('mtcars')

tsk_mtcars = tsk('mtcars')
tsk_mtcars
## <TaskRegr:mtcars> (32 x 11): Motor Trends
## * Target: mpg
## * Properties: -
## * Features (10):
##   - dbl (10): am, carb, cyl, disp, drat, gear, hp, qsec, vs, wt