.arff格式的json怎么写
How do you write json in .arff format
我有以下 json 文件。
[
{
"y": 1.544937286376953,
"x": 0.0736468505859375,
"z": 10.19739440917969,
"timestamp": 1413232199331.14
},
{
"y": 2.492466888427734,
"x": 0.7253915405273438,
"z": 11.33457962036133,
"timestamp": 1413232199831.21
}
]
并且此列表中的两个对象都转化为高价值。
类似于下面的天气示例,例如这两个对象是:
{
"y": 1.544937286376953,
"x": 0.0736468505859375,
"z": 10.19739440917969,
"timestamp": 1413232199331.14
}
和
{
"y": 2.492466888427734,
"x": 0.7253915405273438,
"z": 11.33457962036133,
"timestamp": 1413232199831.21
}
如果它们同时出现,则将另一个 属性 设置为例如 Velocity 到 High。
我怎样才能把它写成类似下面的天气示例:
@relation weather
@attribute outlook {sunny, overcast, rainy}
@attribute temperature numeric
@attribute humidity numeric
@attribute windy {TRUE, FALSE}
@attribute play {yes, no}
@data
sunny,85,85,FALSE,no
sunny,80,90,TRUE,no
我的属性在哪里是对象列表。
我的属性是这样的
@attribute accelerator [{numeric,numeric,numeric},{numeric, numeric,numeric}]
有谁知道我该怎么办?我的问题真的有意义吗?
在我看来你想做两件事:
- 将 JSON 数据转换为 .arff
- 将复合属性写入arff
文件
不知道arff文件是否支持#2
这里有一些代码可以将您的 JSON 转换为 arff (#1)
在 R:
library(RWeka)
library(rjson)
json = rjson::fromJSON('[{
"y": 1.544937286376953,
"x": 0.0736468505859375,
"z": 10.19739440917969,
"timestamp": 1413232199331.14
},
{
"y": 2.492466888427734,
"x": 0.7253915405273438,
"z": 11.33457962036133,
"timestamp": 1413232199831.21
}]')
str(json) # show internal representation
# replace nulls, optional
json <- lapply(json, function(x) {
x[sapply(x, is.null)] <- NA
unlist(x)
})
# convert to data frame
mydf <- data.frame(do.call("rbind", json))
# add some more attributes. I've just made up this business logic
mydf["accelerator"] = sqrt(mydf$x^2 + mydf$y^2 + mydf$z^2)
# here the new "accelerator" attribute is high if it is higher than 11
mydf["accelerator_high"] = ifelse(mydf["accelerator"]<=11,"No","Yes")
RWeka::write.arff(mydf, "myfile.arff")
生成的 arff 文件:
@relation R_data_frame
@attribute y numeric
@attribute x numeric
@attribute z numeric
@attribute timestamp numeric
@attribute accelerator numeric
@attribute accelerator_high string
@data
1.544937,0.073647,10.197394,1413232199331.13984,10.314025,No
2.492467,0.725392,11.33458,1413232199831.209984,11.628038,Yes
我有以下 json 文件。
[
{
"y": 1.544937286376953,
"x": 0.0736468505859375,
"z": 10.19739440917969,
"timestamp": 1413232199331.14
},
{
"y": 2.492466888427734,
"x": 0.7253915405273438,
"z": 11.33457962036133,
"timestamp": 1413232199831.21
}
]
并且此列表中的两个对象都转化为高价值。 类似于下面的天气示例,例如这两个对象是:
{
"y": 1.544937286376953,
"x": 0.0736468505859375,
"z": 10.19739440917969,
"timestamp": 1413232199331.14
}
和
{
"y": 2.492466888427734,
"x": 0.7253915405273438,
"z": 11.33457962036133,
"timestamp": 1413232199831.21
}
如果它们同时出现,则将另一个 属性 设置为例如 Velocity 到 High。
我怎样才能把它写成类似下面的天气示例:
@relation weather
@attribute outlook {sunny, overcast, rainy}
@attribute temperature numeric
@attribute humidity numeric
@attribute windy {TRUE, FALSE}
@attribute play {yes, no}
@data
sunny,85,85,FALSE,no
sunny,80,90,TRUE,no
我的属性在哪里是对象列表。
我的属性是这样的 @attribute accelerator [{numeric,numeric,numeric},{numeric, numeric,numeric}]
有谁知道我该怎么办?我的问题真的有意义吗?
在我看来你想做两件事:
- 将 JSON 数据转换为 .arff
- 将复合属性写入arff 文件
不知道arff文件是否支持#2
这里有一些代码可以将您的 JSON 转换为 arff (#1) 在 R:
library(RWeka)
library(rjson)
json = rjson::fromJSON('[{
"y": 1.544937286376953,
"x": 0.0736468505859375,
"z": 10.19739440917969,
"timestamp": 1413232199331.14
},
{
"y": 2.492466888427734,
"x": 0.7253915405273438,
"z": 11.33457962036133,
"timestamp": 1413232199831.21
}]')
str(json) # show internal representation
# replace nulls, optional
json <- lapply(json, function(x) {
x[sapply(x, is.null)] <- NA
unlist(x)
})
# convert to data frame
mydf <- data.frame(do.call("rbind", json))
# add some more attributes. I've just made up this business logic
mydf["accelerator"] = sqrt(mydf$x^2 + mydf$y^2 + mydf$z^2)
# here the new "accelerator" attribute is high if it is higher than 11
mydf["accelerator_high"] = ifelse(mydf["accelerator"]<=11,"No","Yes")
RWeka::write.arff(mydf, "myfile.arff")
生成的 arff 文件:
@relation R_data_frame
@attribute y numeric
@attribute x numeric
@attribute z numeric
@attribute timestamp numeric
@attribute accelerator numeric
@attribute accelerator_high string
@data
1.544937,0.073647,10.197394,1413232199331.13984,10.314025,No
2.492467,0.725392,11.33458,1413232199831.209984,11.628038,Yes