No, later in your test you're using "num_features" as the number of non-zero-features, which is different from number of dimensions (i.e. max feature index plus one).  That's why I needed to differentiate.