Your approach certainly looks correct. Regarding efficiency: are you seeing a bottleneck in this operation? If so, there might be faster approaches (unsure yet), but I would generally not optimize code which won’t be visible in the entire model training later.