add*_*ons 31 r binning dataframe summarize
使用python我创建了包含相似度值的以下数据框:
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
Run Code Online (Sandbox Code Playgroud)
我正在尝试编写一个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
Run Code Online (Sandbox Code Playgroud)
基于以上逻辑,我想构建一个数据框
cosinFcolor cosinEdge cosinTexture histoFcolor histoEdge histoTexture jaccard
1 3 0 0 1 1 0 0
Run Code Online (Sandbox Code Playgroud)
我如何以脚本开头,或者我应该在python中这样做?我发现它有多么强大/它有多少机器学习包,我试图熟悉R. 我的目标是建立一个分类器,但首先我需要熟悉R :)
seb*_*n-c 56
另一个考虑到极值的答案:
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
Run Code Online (Sandbox Code Playgroud)
剪切功能根据您指定的剪切分割成垃圾箱.所以让我们以1:10的比分将它分成3,5和7.
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]
Run Code Online (Sandbox Code Playgroud)
你可以看到它是如何产生一个因素,其中水平是休息之间的水平.还要注意它不包括3(有一个include.lowest参数将包含它).但这些对于团体来说是可怕的名字,我们称之为第1组和第2组.
cut(1:10, c(3, 5, 7), labels=1:2)
[1] <NA> <NA> <NA> 1 1 2 2 <NA> <NA> <NA>
Run Code Online (Sandbox Code Playgroud)
更好,但是NAs有什么用?它们超出了我们的界限,不计算在内.为了计算它们,在我的解决方案中,我添加了-infinity和infinity,因此所有点都将被包含在内.请注意,由于我们有更多休息时间,我们需要更多标签:
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
Run Code Online (Sandbox Code Playgroud)
好的,但我们不想要4(根据你的问题).我们希望所有的4都在第1组中.所以让我们摆脱标记为'4'的条目.
x[x=="4"] <- "1"
[1] 1 1 1 2 2 3 3 1 1 1
Levels: 1 2 3 4
Run Code Online (Sandbox Code Playgroud)
这跟我以前做的略有不同,请注意我之前拿走了所有最后的标签,但是我已经这样做了,所以你可以更好地看看它是如何cut工作的.
好的,apply功能.到目前为止,我们一直在单个矢量上使用cut.但是你希望它用在一组向量上:数据框的每一列.这就是第二个论点的apply作用.1将函数应用于所有行,2适用于所有列.将该cut函数应用于数据框的每一列.之后一切都cut在应用功能都只是参数cut,这是我们上面讨论.
希望有所帮助.
mne*_*nel 25
您还可以使用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
Run Code Online (Sandbox Code Playgroud)
Luc*_*zer 14
切割它很容易就像馅饼一样
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
Run Code Online (Sandbox Code Playgroud)