Interface functions
| 2400 | // Interface functions |
| 2401 | // |
| 2402 | svm_model* |
| 2403 | svm_train(const svm_problem* prob, const svm_parameter* param) |
| 2404 | { |
| 2405 | auto* model = Malloc(svm_model, 1); |
| 2406 | model->param = *param; |
| 2407 | model->free_sv = 0; // XXX |
| 2408 | model->probA = nullptr; |
| 2409 | model->probB = nullptr; |
| 2410 | |
| 2411 | if (param->svm_type == ONE_CLASS || param->svm_type == EPSILON_SVR || |
| 2412 | param->svm_type == NU_SVR) { |
| 2413 | // regression or one-class-svm |
| 2414 | model->nr_class = 2; |
| 2415 | model->label = nullptr; |
| 2416 | model->nSV = nullptr; |
| 2417 | model->probA = nullptr; |
| 2418 | model->probB = nullptr; |
| 2419 | model->sv_coef = Malloc(double*, 1); |
| 2420 | |
| 2421 | if (param->probability && |
| 2422 | (param->svm_type == EPSILON_SVR || param->svm_type == NU_SVR)) { |
| 2423 | model->probA = Malloc(double, 1); |
| 2424 | model->probA[0] = svm_svr_probability(prob, param); |
| 2425 | } |
| 2426 | |
| 2427 | decision_function f = svm_train_one(prob, param, 0, 0); |
| 2428 | |
| 2429 | model->rho = Malloc(double, 1); |
| 2430 | model->rho[0] = f.rho; |
| 2431 | |
| 2432 | int nSV = 0; |
| 2433 | |
| 2434 | for (int i = 0; i < prob->l; i++) |
| 2435 | if (std::abs(f.alpha[i]) > 0) |
| 2436 | ++nSV; |
| 2437 | |
| 2438 | model->l = nSV; |
| 2439 | |
| 2440 | model->SV = Malloc(svm_node*, nSV); |
| 2441 | |
| 2442 | model->sv_coef[0] = Malloc(double, nSV); |
| 2443 | |
| 2444 | int j = 0; |
| 2445 | |
| 2446 | for (int i = 0; i < prob->l; i++) |
| 2447 | if (std::abs(f.alpha[i]) > 0) { |
| 2448 | model->SV[j] = prob->x[i]; |
| 2449 | model->sv_coef[0][j] = f.alpha[i]; |
| 2450 | ++j; |
| 2451 | } |
| 2452 | |
| 2453 | free(f.alpha); |
| 2454 | } |
| 2455 | else { |
| 2456 | // classification |
| 2457 | int l = prob->l; |
| 2458 | int nr_class; |
| 2459 | int* label = nullptr; |
no test coverage detected