关于在 R 中使用 sample() 函数为 ML 设置训练和测试集的清晰度。
Clarity regarding the use of sample() function in R to set up training and test sets for ML.
我试图通过 Datacamp 理解 R 中的 KNN 算法示例:Machine Learning in R for beginners
我无法理解他们如何执行抽样来设置训练和测试数据集。
我可以按照代码直到这一行:
ind <- sample(2, nrow(iris), replace=TRUE, prob=c(0.67, 0.33))
据我了解,这会创建一个长度等于 nrow(iris)
的矢量,矢量值为 1
或 2
,并且选择这些值的概率为0.67
和 0.33
。
因此,我们得到以下输出:
> ind
[1] 1 1 2 1 2 2 1 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 1 1 1 1 2 1 2 1 2 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1
[58] 1 1 1 1 2 1 1 1 2 1 2 1 1 2 1 1 1 1 2 2 1 1 1 2 1 1 1 1 1 1 2 2 1 1 1 1 1 1 2 1 2 1 2 1 1 1 1 1 2 1 2 2 1 2 1 1 1
[115] 1 2 1 1 1 2 1 2 1 1 2 1 1 2 1 2 1 2 1 1 2 1 1 1 1 1 2 1 1 1 2 1 1 2 1 1
在下一步中,他们使用以下代码创建训练集:
iris.training <- iris [ind==1, 1:4]
此行可能会生成一个数据框,其中包含 ind == 1
的所有行。
head(iris.training)
Sepal.Length Sepal.Width Petal.Length Petal.Width
1 5.1 3.5 1.4 0.2
2 4.9 3.0 1.4 0.2
4 4.6 3.1 1.5 0.2
7 4.6 3.4 1.4 0.3
9 4.4 2.9 1.4 0.2
10 4.9 3.1 1.5 0.1
我的问题是变量 ind
和 iris
数据集有什么关系。也就是说,R 如何知道从原始 iris
数据集中提取哪些行(哪些行有 ind == 1
),因为 ind
和 [=] 之间似乎没有联系21=]数据集。设置ind
时唯一一次提到iris
数据集是在ind <- sample(2, nrow(iris), replace=TRUE, prob=c(0.67, 0.33))
中使用nrow(iris)
确定样本大小(要选择的样本数)。
My question is how are the variable ind and the iris data set related.
他们不是,但他们不需要。例如,数字 1-5 与 iris 数据集之间没有内在关系,但是
iris[1:5, ]
#> 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
由 reprex package (v0.2.0) 创建于 2018-08-05。
说 ind <- sample(c(TRUE, FALSE), nrow(iris), replace=TRUE, prob=c(0.67, 0.33))
然后 iris[ind, ]
会更清楚一些,以强调 ind
是 select 的行索引而不是比较结果iris
.
内的变量数
其实他们没有关系。我们创建了向量“ind”,目的是将给定的数据集无任何偏差地拆分为训练数据集和测试数据集,从而提高模型的准确性。
如您所知,我们必须将数据集分成两部分。但我们不能简单地将数据集拆分为 iris[1:456] 和 iris[456: ],因为我们忽略了 iris 数据集的最后一个物种,因为数据集是按物种属性顺序排列的。
因此,我们创建了这个“ind”向量来随机拆分数据集,使其包含与每个拆分中的所有物种相关的数据,并且比例相同。
我试图通过 Datacamp 理解 R 中的 KNN 算法示例:Machine Learning in R for beginners
我无法理解他们如何执行抽样来设置训练和测试数据集。
我可以按照代码直到这一行:
ind <- sample(2, nrow(iris), replace=TRUE, prob=c(0.67, 0.33))
据我了解,这会创建一个长度等于 nrow(iris)
的矢量,矢量值为 1
或 2
,并且选择这些值的概率为0.67
和 0.33
。
因此,我们得到以下输出:
> ind
[1] 1 1 2 1 2 2 1 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 1 1 1 1 2 1 2 1 2 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1
[58] 1 1 1 1 2 1 1 1 2 1 2 1 1 2 1 1 1 1 2 2 1 1 1 2 1 1 1 1 1 1 2 2 1 1 1 1 1 1 2 1 2 1 2 1 1 1 1 1 2 1 2 2 1 2 1 1 1
[115] 1 2 1 1 1 2 1 2 1 1 2 1 1 2 1 2 1 2 1 1 2 1 1 1 1 1 2 1 1 1 2 1 1 2 1 1
在下一步中,他们使用以下代码创建训练集:
iris.training <- iris [ind==1, 1:4]
此行可能会生成一个数据框,其中包含 ind == 1
的所有行。
head(iris.training)
Sepal.Length Sepal.Width Petal.Length Petal.Width
1 5.1 3.5 1.4 0.2
2 4.9 3.0 1.4 0.2
4 4.6 3.1 1.5 0.2
7 4.6 3.4 1.4 0.3
9 4.4 2.9 1.4 0.2
10 4.9 3.1 1.5 0.1
我的问题是变量 ind
和 iris
数据集有什么关系。也就是说,R 如何知道从原始 iris
数据集中提取哪些行(哪些行有 ind == 1
),因为 ind
和 [=] 之间似乎没有联系21=]数据集。设置ind
时唯一一次提到iris
数据集是在ind <- sample(2, nrow(iris), replace=TRUE, prob=c(0.67, 0.33))
中使用nrow(iris)
确定样本大小(要选择的样本数)。
My question is how are the variable ind and the iris data set related.
他们不是,但他们不需要。例如,数字 1-5 与 iris 数据集之间没有内在关系,但是
iris[1:5, ]
#> 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
由 reprex package (v0.2.0) 创建于 2018-08-05。
说 ind <- sample(c(TRUE, FALSE), nrow(iris), replace=TRUE, prob=c(0.67, 0.33))
然后 iris[ind, ]
会更清楚一些,以强调 ind
是 select 的行索引而不是比较结果iris
.
其实他们没有关系。我们创建了向量“ind”,目的是将给定的数据集无任何偏差地拆分为训练数据集和测试数据集,从而提高模型的准确性。 如您所知,我们必须将数据集分成两部分。但我们不能简单地将数据集拆分为 iris[1:456] 和 iris[456: ],因为我们忽略了 iris 数据集的最后一个物种,因为数据集是按物种属性顺序排列的。 因此,我们创建了这个“ind”向量来随机拆分数据集,使其包含与每个拆分中的所有物种相关的数据,并且比例相同。