* Permute each categorical variable and predict */
| 44 | * Permute each categorical variable and predict |
| 45 | */ |
| 46 | AnyType |
| 47 | rf_cat_imp_score::run(AnyType &args) { |
| 48 | if (args[0].isNull() || args[7].isNull()) { return Null(); } |
| 49 | Tree dt = args[0].getAs<ByteString>(); |
| 50 | MutableNativeIntegerVector cat_features; |
| 51 | NativeColumnVector con_features; |
| 52 | try { |
| 53 | if (args[1].isNull()){ |
| 54 | // no cat features |
| 55 | return Null(); |
| 56 | } |
| 57 | else { |
| 58 | MutableNativeIntegerVector xx_cat = args[1].getAs<MutableNativeIntegerVector>(); |
| 59 | cat_features.rebind(xx_cat.memoryHandle(), xx_cat.size()); |
| 60 | } |
| 61 | if (args[2].isNull()){ |
| 62 | con_features.rebind(this->allocateArray<double>(0)); |
| 63 | } |
| 64 | else { |
| 65 | NativeColumnVector xx_con = args[2].getAs<NativeColumnVector>(); |
| 66 | con_features.rebind(xx_con.memoryHandle(), xx_con.size()); |
| 67 | } |
| 68 | } catch (const ArrayWithNullException &e) { |
| 69 | // not expect to reach here |
| 70 | // if max_surr = 0, nulls are filtered |
| 71 | // otherwise, mapped to -1 or NaN |
| 72 | return Null(); |
| 73 | } |
| 74 | |
| 75 | MappedIntegerVector cat_n_levels = args[3].getAs<MappedIntegerVector>(); |
| 76 | |
| 77 | int n_permutations = args[4].getAs<int>(); |
| 78 | double y = args[5].getAs<double>(); |
| 79 | bool is_classification = args[6].getAs<bool>(); |
| 80 | MappedMatrix distributions = args[7].getAs<MappedMatrix>(); |
| 81 | |
| 82 | // returning |
| 83 | MutableNativeColumnVector permuted_predictions( |
| 84 | this->allocateArray<double>(cat_n_levels.size())); |
| 85 | |
| 86 | // permute each and predict |
| 87 | NativeRandomNumberGenerator generator; |
| 88 | for (int p = 0; p < n_permutations; p ++) { |
| 89 | for (Index i = 0; i < cat_n_levels.size(); i ++) { |
| 90 | int orig_i = cat_features(i); |
| 91 | discrete_distribution<> ddist(distributions.col(i).data(), |
| 92 | distributions.col(i).data() + cat_n_levels(i) + 1); |
| 93 | variate_generator<NativeRandomNumberGenerator, discrete_distribution<> > |
| 94 | rvt(generator, ddist); |
| 95 | |
| 96 | cat_features(i) = rvt() - 1; |
| 97 | |
| 98 | // calling NativeIntegerVector for a const cast |
| 99 | // see EigenIntegration_impl.hpp in ports for details |
| 100 | double prediction = dt.predict_response( |
| 101 | NativeIntegerVector(cat_features.memoryHandle()), con_features); |
| 102 | double score = 0.; |
| 103 | if (is_classification) { |
nothing calls this directly
no test coverage detected