Javascript 如何以.arff格式编写json

Javascript 如何以.arff格式编写json,javascript,json,weka,Javascript,Json,Weka,我有以下json文件 [ { "y": 1.544937286376953, "x": 0.0736468505859375, "z": 10.19739440917969, "timestamp": 1413232199331.14 }, { "y": 2.492466888427734, "x": 0.7253915405273438, "z":

我有以下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
}

如果它们同时出现,则将其他属性(例如“速度”)设置为“高”

如何将其写入以下天气示例中:

@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
其中“我的属性”是对象列表

我的属性类似于 @属性加速器[{numeric,numeric,numeric},{numeric,numeric,numeric}]


有人知道我该怎么做吗?我的问题真的有意义吗?

在我看来,你想做两件事:

  • 将JSON数据转换为.arff
  • 将复合属性写入arff 文件
  • 我不知道arff文件是否支持#2

    下面是一些将JSON转换为arff(#1)的代码 在R中:


    你想在javascript中这样做吗?嗨@Amy不,我想把我的json文件转换成.arrf,这样我就可以把它作为训练集加载。我遇到的问题是,每个对象都有x,y,z的对象列表表明我的速度属性是高还是低。
    @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
    
    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")
    
    @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