I'm looking for algorithms to create bins of variables in order to reduce the noise. I have found several libraries for that, one if the chi2 library:


The documentation has the following example:


#--discretized dataset using Chi2 algorithm

This works for this data, but if I train a model after transforming this data in order to make prediction over new records I will have to use the same cuts that were used here. My question is, is there any method or library that stored the cuts of the bins in a way that can be easily applied to new data similarly to a predict method? without any custom function


1 Answer 1


It provides the cuts and you have to apply it back. Reading the code in this package function it assumes the last column is the dependent variable and the other columns can be discretized. My point, not much documentation or warnings, I am not very sure about how to best use this library.

For example if you look at how the data is cut:

disc = chi2(iris,0.5,0.05)

[1] 4.3 5.4

[1] 5.5 5.7

[1] 5.8 7.0

[1] 7.1 7.9

You can see the range of the discrete labels don't quite agree with cutp, making it very difficult to map back:

> disc$cutp
[1] 3.5 4.5 6.5

One option if you are tied to this package is do the discretization over all your data, split them into train and test, and do the training, for example, we make the data into train and test, like you normally get in kaggle:

idx = sample(nrow(iris),100)
traindf = iris[idx,]
testdf = iris[-idx,]

You perform the discretization over the combined dataset:

disc = chi2(rbind(traindf,testdf),0.5,0.05)

And you fit over the train subset, not clear how you want to use the discrete data, because the result is given as an integer, so below I just treat it as continuous, i would consider using it as catgeorical:

fit = nnet(Species ~ .,data=disc$Disc.data[1:nrow(traindf),],size=2)

Then predict over the other subset:


You can also try using a better documented library arules.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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