从数据框中生成垃圾箱

使用Python我已经创build了以下数据框,其中包含相似性值:

cosinFcolor cosinEdge cosinTexture histoFcolor histoEdge histoTexture jaccard 1 0.770 0.489 0.388 0.57500000 0.5845137 0.3920000 0.00000000 2 0.067 0.496 0.912 0.13865546 0.6147309 0.6984127 0.00000000 3 0.514 0.426 0.692 0.36440678 0.4787535 0.5198413 0.05882353 4 0.102 0.430 0.739 0.11297071 0.5288008 0.5436508 0.00000000 5 0.560 0.735 0.554 0.48148148 0.8168083 0.4603175 0.00000000 6 0.029 0.302 0.558 0.08547009 0.3928234 0.4603175 0.00000000 

我正在尝试编写一个R脚本来生成反映垃圾箱的另一个数据框,但是如果值超过0.5,我的分箱条件将适用

伪代码:

 if (cosinFcolor > 0.5 & cosinFcolor <= 0.6) bin = 1 if (cosinFcolor > 0.6 & cosinFcolor <= 0.7) bin = 2 if (cosinFcolor > 0.7 & cosinFcolor =< 0.8) bin = 3 if (cosinFcolor > 0.8 & cosinFcolor <=0.9) bin = 4 if (cosinFcolor > 0.9 & cosinFcolor <= 1.0) bin = 5 else bin = 0 

基于上述逻辑,我想构build一个数据框架

  cosinFcolor cosinEdge cosinTexture histoFcolor histoEdge histoTexture jaccard 1 3 0 0 1 1 0 0 

我怎样才能开始作为一个脚本,或者我应该这样做在Python? 我想知道它是多么强大/机器学习软件包的数量后,熟悉R。 我的目标是build立一个分类器,但首先我需要熟悉R 🙂

考虑到极值的另一个切点答案是:

 dat <- read.table("clipboard", header=TRUE) cuts <- apply(dat, 2, cut, c(-Inf,seq(0.5, 1, 0.1), Inf), labels=0:6) cuts[cuts=="6"] <- "0" cuts <- as.data.frame(cuts) cosinFcolor cosinEdge cosinTexture histoFcolor histoEdge histoTexture jaccard 1 3 0 0 1 1 0 0 2 0 0 5 0 2 2 0 3 1 0 2 0 0 1 0 4 0 0 3 0 1 1 0 5 1 3 1 0 4 0 0 6 0 0 1 0 0 0 0 

说明

切割function根据您指定的切割分割为多个区域。 所以我们先1:10分三,五,七吧。

 cut(1:10, c(3, 5, 7)) [1] <NA> <NA> <NA> (3,5] (3,5] (5,7] (5,7] <NA> <NA> <NA> Levels: (3,5] (5,7] 

你可以看到它是如何成为一个因素的水平是在rest之间的水平。 另外注意它不包括3(有一个include.lowest参数将包括它)。 但是这些对于团体来说是很糟糕的名字,我们称他们为第一组和第二组。

 cut(1:10, c(3, 5, 7), labels=1:2) [1] <NA> <NA> <NA> 1 1 2 2 <NA> <NA> <NA> 

更好,但新来的是什么? 他们超出我们的界限,不计算在内。 要计算它们,在我的解决scheme中,我添加了无穷大和无穷大,所以包括所有点。 请注意,由于我们有更多的rest时间,我们需要更多的标签:

 x <- cut(1:10, c(-Inf, 3, 5, 7, Inf), labels=1:4) [1] 1 1 1 2 2 3 3 4 4 4 Levels: 1 2 3 4 

好的,但我们不想要4(根据你的问题)。 我们希望所有的4都在组1中。所以让我们摆脱标记为'4'的条目。

 x[x=="4"] <- "1" [1] 1 1 1 2 2 3 3 1 1 1 Levels: 1 2 3 4 

这和我之前所做的略有不同,注意我之前拿走了所有最后的标签,但是我已经在这里完成了,所以你可以更好地看到cut是如何工作的。

好的, apply函数。 到目前为止,我们一直在使用一个vector。 但是您希望将其用于一组vector:数据框的每一列。 这就是apply的第二个参数。 1将函数应用于所有行,2应用于所有列。 将cut函数应用于数据框的每一列。 在apply函数中的所有东西都只是cut参数,我们在上面讨论过。

希望有所帮助。

你也可以使用findInterval

 findInterval(seq(0, 1, l=20), seq(0.5, 1, by=0.1)) ## [1] 0 0 0 0 0 0 0 0 0 1 1 2 2 3 4 4 5 5 

随着剪切,它很容易

 dtf <- read.table( textConnection( "cosinFcolor cosinEdge cosinTexture histoFcolor histoEdge histoTexture jaccard 1 0.770 0.489 0.388 0.57500000 0.5845137 0.3920000 0.00000000 2 0.067 0.496 0.912 0.13865546 0.6147309 0.6984127 0.00000000 3 0.514 0.426 0.692 0.36440678 0.4787535 0.5198413 0.05882353 4 0.102 0.430 0.739 0.11297071 0.5288008 0.5436508 0.00000000 5 0.560 0.735 0.554 0.48148148 0.8168083 0.4603175 0.00000000 6 0.029 0.302 0.558 0.08547009 0.3928234 0.4603175 0.00000000"), sep = " ", header = TRUE) dtf$bin <- cut(dtf$cosinFcolor, breaks = c(0, seq(0.5, 1, by = .1)), labels = 0:5) dtf cosinFcolor cosinEdge cosinTexture histoFcolor histoEdge histoTexture jaccard bin 1 0.770 0.489 0.388 0.57500000 0.5845137 0.3920000 0.00000000 3 2 0.067 0.496 0.912 0.13865546 0.6147309 0.6984127 0.00000000 0 3 0.514 0.426 0.692 0.36440678 0.4787535 0.5198413 0.05882353 1 4 0.102 0.430 0.739 0.11297071 0.5288008 0.5436508 0.00000000 0 5 0.560 0.735 0.554 0.48148148 0.8168083 0.4603175 0.00000000 1 6 0.029 0.302 0.558 0.08547009 0.3928234 0.4603175 0.00000000 0 

这是使用mltools包中的bin_data()函数的另一个解决scheme。

装箱一个vector

 library(mltools) cosinFcolor <- c(0.77, 0.067, 0.514, 0.102, 0.56, 0.029) binned <- bin_data(cosinFcolor, bins=c(0, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0), boundaryType = "[lorc") binned [1] (0.7, 0.8] [0, 0.5] (0.5, 0.6] [0, 0.5] (0.5, 0.6] [0, 0.5] Levels: [0, 0.5] < (0.5, 0.6] < (0.6, 0.7] < (0.7, 0.8] < (0.8, 0.9] < (0.9, 1] # Convert to numbers 0, 1, ... as.integer(binned) - 1L 

对数据框中的每一列进行分箱

 df <- read.table(textConnection( "cosinFcolor cosinEdge cosinTexture histoFcolor histoEdge histoTexture jaccard 0.770 0.489 0.388 0.57500000 0.5845137 0.3920000 0.00000000 0.067 0.496 0.912 0.13865546 0.6147309 0.6984127 0.00000000 0.514 0.426 0.692 0.36440678 0.4787535 0.5198413 0.05882353 0.102 0.430 0.739 0.11297071 0.5288008 0.5436508 0.00000000 0.560 0.735 0.554 0.48148148 0.8168083 0.4603175 0.00000000 0.029 0.302 0.558 0.08547009 0.3928234 0.4603175 0.00000000" ), sep = " ", header = TRUE) for(col in colnames(df)) df[[col]] <- as.integer(bin_data(df[[col]], bins=c(0, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0), boundaryType = "[lorc")) - 1L df cosinFcolor cosinEdge cosinTexture histoFcolor histoEdge histoTexture jaccard 1 3 0 0 1 1 0 0 2 0 0 5 0 2 2 0 3 1 0 2 0 0 1 0 4 0 0 3 0 1 1 0 5 1 3 1 0 4 0 0 6 0 0 1 0 0 0 0