Feature Selection: Boolean Array Mask to Nifti Image


Is there anyway to transform a boolean array mask into a Nifti mask? After running SelectKBest (through sklearn) and getting two arrays of the selected features via cross validation, I ended up with two different boolean masks. What I’d like to do is transform these arrays into Nifti masks, and further take the average of such masks by using nilearn.masking.intersect_masks

If this isn’t possible, what would be the best approach to finding the ‘optimal’ selected features if you have two masks?


To build a new NiftiImage you have to provide data and affine. Using your original ‘mask’ and your ‘boolean_array’, you can just use nilearn.image.new_img_like that will reuse the affine of the ‘mask’.

smaller_mask = new_img_like(mask, boolean_array)

Then you can indeed use .intersect_masks and keep the union of both masks (threshold=0) or use a higher threshold if you have more images.