svm_predict(y, x, m [, options]) -> (p_labels, p_acc, p_vals) Predict data (y, x) with the SVM model m. options: -b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0); for one-class SVM only 0 is supported. -q : quiet mode (no outputs). T
(y, x, m, options="")
| 165 | return m |
| 166 | |
| 167 | def svm_predict(y, x, m, options=""): |
| 168 | """ |
| 169 | svm_predict(y, x, m [, options]) -> (p_labels, p_acc, p_vals) |
| 170 | |
| 171 | Predict data (y, x) with the SVM model m. |
| 172 | options: |
| 173 | -b probability_estimates: whether to predict probability estimates, |
| 174 | 0 or 1 (default 0); for one-class SVM only 0 is supported. |
| 175 | -q : quiet mode (no outputs). |
| 176 | |
| 177 | The return tuple contains |
| 178 | p_labels: a list of predicted labels |
| 179 | p_acc: a tuple including accuracy (for classification), mean-squared |
| 180 | error, and squared correlation coefficient (for regression). |
| 181 | p_vals: a list of decision values or probability estimates (if '-b 1' |
| 182 | is specified). If k is the number of classes, for decision values, |
| 183 | each element includes results of predicting k(k-1)/2 binary-class |
| 184 | SVMs. For probabilities, each element contains k values indicating |
| 185 | the probability that the testing instance is in each class. |
| 186 | Note that the order of classes here is the same as 'model.label' |
| 187 | field in the model structure. |
| 188 | """ |
| 189 | |
| 190 | def info(s): |
| 191 | print(s) |
| 192 | |
| 193 | predict_probability = 0 |
| 194 | argv = options.split() |
| 195 | i = 0 |
| 196 | while i < len(argv): |
| 197 | if argv[i] == '-b': |
| 198 | i += 1 |
| 199 | predict_probability = int(argv[i]) |
| 200 | elif argv[i] == '-q': |
| 201 | info = print_null |
| 202 | else: |
| 203 | raise ValueError("Wrong options") |
| 204 | i+=1 |
| 205 | |
| 206 | svm_type = m.get_svm_type() |
| 207 | is_prob_model = m.is_probability_model() |
| 208 | nr_class = m.get_nr_class() |
| 209 | pred_labels = [] |
| 210 | pred_values = [] |
| 211 | |
| 212 | if predict_probability: |
| 213 | if not is_prob_model: |
| 214 | raise ValueError("Model does not support probabiliy estimates") |
| 215 | |
| 216 | if svm_type in [NU_SVR, EPSILON_SVR]: |
| 217 | info("Prob. model for test data: target value = predicted value + z,\n" |
| 218 | "z: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=%g" % m.get_svr_probability()); |
| 219 | nr_class = 0 |
| 220 | |
| 221 | prob_estimates = (c_double * nr_class)() |
| 222 | for xi in x: |
| 223 | xi, idx = gen_svm_nodearray(xi, isKernel=(m.param.kernel_type == PRECOMPUTED)) |
| 224 | label = libsvm.svm_predict_probability(m, xi, prob_estimates) |
nothing calls this directly
no test coverage detected
searching dependent graphs…