1
$\begingroup$

I have training arff file, and also I have instances data at hand. Now I want to add the instances data onto another file, called test file, and this test file has the same relation header and attribute with previous training file. How to realize that?

$\endgroup$
7
  • $\begingroup$ This question isn't clear to me. Are you asking for code that will let you add to a certain type of file? If so, this Q would be off-topic for CV (see our help center). Are you asking how to determine if, or ensure that, the relations & attributes are the same? That might be a statistical / machine learning question, but we would need more information before we could help you. $\endgroup$ Commented Mar 14, 2014 at 19:10
  • $\begingroup$ Do you use R? If so, I have code that can do that. $\endgroup$
    – Underminer
    Commented Mar 14, 2014 at 19:50
  • $\begingroup$ @underminer,I use java, but it is great if you can share the code to me. $\endgroup$
    – Logon
    Commented Mar 14, 2014 at 20:25
  • $\begingroup$ @gung, is that unclear to you? yes, the relation and attributes should same $\endgroup$
    – Logon
    Commented Mar 14, 2014 at 20:26
  • 1
    $\begingroup$ @gung As I understand it, it is simply a data file manipulation question better suited for StackOverflow. $\endgroup$
    – Underminer
    Commented Mar 14, 2014 at 21:02

1 Answer 1

1
$\begingroup$

If you use R, you can read the training and testing sets into a single data.frame, and then rewrite the training and testing arff files from the single data.frame. Since the data.frame stores the possible factor levels common between the training and testing set, all possible values will show up in the header of both arff files. See the code below for an example. Here, I assume you have the current training and testing sets saved as "trainingInput.arff" and "testingInput.arff", which have 1000 and 2000 instances respectively. This will generate "trainingOutput.arff" and "testingOutput.arff" which will agree with each other in terms of possible factor values.

# install and load the foreign package
library(foreign)

# read in your training data -- assume we have 1000 instances here
train = read.arff("trainingInput.arff")

# read in your testing data -- assume we have 2000 instances here
test = read.arff("testingInput.arff")

# combine training and testing into single data.frame
combined = rbind(train, test)

# then nrow(train) == 1000 and nrow(test) == 2000 and nrow(combined) == 3000

# write the new training and testing sets from the combined dataset
write.arff(combined[1:1000,], "trainingOutput.arff")
write.arff(combined[1001:3000,], "trainingOutput.arff")

I hope this helps.

$\endgroup$
1
  • $\begingroup$ Thank you underminer, I would like to look at your code but I don't know R. Anyway, thank you. $\endgroup$
    – Logon
    Commented Mar 14, 2014 at 20:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.