123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- //#include "svm/svm.h"
- //#include "svm/data.h"
- //#include <stdio.h>
- //#include <stdlib.h>
- //#include <math.h>
- //
- //void svm_train_and_predict() {
- // struct svm_problem prob{};
- // struct svm_parameter param{};
- // struct svm_model *model;
- // struct svm_node *x_space;
- //
- // // 设置SVM参数
- // param.svm_type = ONE_CLASS;
- // param.kernel_type = RBF; // 使用径向基函数核
- // param.nu = 0.1; // nu参数,用于控制支持向量的数量
- // param.gamma = 0.1; // nu参数,用于控制支持向量的数量
- //
- // // 初始化问题
- // prob.l = HaveCount;
- // prob.y = (double *) malloc(prob.l * sizeof(double));
- // prob.x = (struct svm_node **) malloc(prob.l * sizeof(struct svm_node *));
- // x_space = (struct svm_node *) malloc((Features + 1) * prob.l * sizeof(struct svm_node));
- //
- // // 设置训练数据
- // for (int i = 0; i < prob.l; ++i) {
- // prob.x[i] = &x_space[(Features + 1) * i];
- // for (int j = 0; j < Features; ++j) {
- // prob.x[i][j].index = j + 1;
- // prob.x[i][j].value = Have[i][j];
- // }
- // prob.x[i][Features].index = -1;
- // prob.y[i] = 1; // 所有点标记为1,因为这是一个单分类问题
- // }
- //
- // // 训练模型
- // model = svm_train(&prob, ¶m);
- // int res = svm_save_model("test.model", model);
- // printf("save res: %d\n", res);
- //
- //
- // // 预测
- // for (int i = 0; i < TestCount; ++i) {
- // struct svm_node test[Features + 1];
- // for (int j = 0; j < Features; ++j) {
- // test[j].index = j + 1;
- // test[j].value = Test[i][j];
- // }
- // test[Features].index = -1;
- //
- // double predicted_label = svm_predict(model, test);
- // printf("Predicted Label = %.0lf\n", predicted_label);
- // }
- //
- // // 释放资源
- // free(prob.y);
- // free(prob.x);
- // free(x_space);
- // svm_free_and_destroy_model(&model);
- //}
- //
- //void load_and_test() {
- // svm_model *model = svm_load_model("test.model");
- //
- // // 预测 have
- // int success = 0;
- // for (auto &line : Have) {
- // struct svm_node node[Features + 1];
- // for (int j = 0; j < Features; ++j) {
- // node[j].index = j + 1;
- // node[j].value = line[j];
- // }
- // node[Features].index = -1;
- //
- // if (svm_predict(model, node) > 0) success++;
- // }
- // printf("Accuracy Have: [%d/%d]\n", success, HaveCount);
- //
- // // 预测 test
- // success = 0;
- // for (auto &line : Test) {
- // struct svm_node node[Features + 1];
- // for (int j = 0; j < Features; ++j) {
- // node[j].index = j + 1;
- // node[j].value = line[j];
- // }
- // node[Features].index = -1;
- //
- // if (svm_predict(model, node) > 0) success++;
- // }
- // printf("Accuracy Test: [%d/%d]\n", success, TestCount);
- //
- // // 预测 none
- // success = 0;
- // for (auto &line : None) {
- // struct svm_node node[Features + 1];
- // for (int j = 0; j < Features; ++j) {
- // node[j].index = j + 1;
- // node[j].value = line[j];
- // }
- // node[Features].index = -1;
- //
- // if (svm_predict(model, node) < 0) success++;
- // }
- // printf("Accuracy None: [%d/%d]\n", success, NoneCount);
- //}
- #include "my-svm/svm.h"
- #include "stdio.h"
- int main() {
- svm_init();
- Node *x = input();
- int8_t res = svm_predict(x);
- printf("result: %d\n", res);
- return 0;
- }
|