简体   繁体   English

OpenCV中的决策树

[英]Decision trees in OpenCV

I have a table with information about the weather and the number of forest fires in several days in a region: 我有一张表格,其中包含有关该地区几天内的天气和森林大火数量的信息:

 Temperature | Humidity | Wind | ... | Number of forest fires

All data are presented as numbers. 所有数据均以数字表示。

It is necessary to use these data to build a decision tree, and using it and input parameters (temperature, humidity, wind speed, etc.) to predict the number of forest fires. 必须使用这些数据来构建决策树,并使用它和输入参数(温度,湿度,风速等)来预测森林火灾的次数。

For working with decision trees in OpenCV there is a class CvDTree . 为了在OpenCV中使用决策树,有一个类CvDTree To construct a decision tree in this class has a method of train : 在此类中构造决策树有一种train方法:

boolean train (Mat trainData, int tflag, Mat responses)

How to convert my input (temperature, humidity, wind, ..., number of forest fires) in Mat trainData ? 如何在Mat trainData转换我的输入(温度,湿度,风,...,森林火灾的次数)?

Added: 添加:

I get an error during compiling this code: 在编译此代码时出现错误:

package com.selw;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.ml.DTrees;
import org.opencv.ml.Ml;

public class Main {

static{ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }

public static void main(String[] args) {
    Mat data = new Mat(5, 3, CvType.CV_32FC1, new Scalar(0));
    data.put(0, 0, new float[]{1.69f, 1, 0});
    data.put(1, 0, new float[]{1.76f, 0, 0});
    data.put(2, 0, new float[]{1.80f, 0, 0});
    data.put(3, 0, new float[]{1.77f, 0, 0});
    data.put(4, 0, new float[]{1.83f, 0, 1});

    Mat responses = new Mat(5, 1, CvType.CV_32SC1, new Scalar(0));
    responses.put(0, 0, new int[]{0, 1, 1, 0, 1});

    DTrees tree = DTrees.create();
    tree.train(data, Ml.ROW_SAMPLE, responses);
    }
}

Error (in line tree.train(data, Ml.ROW_SAMPLE, responses); ): 错误(在tree.train(data, Ml.ROW_SAMPLE, responses); ):

Exception in thread "main" java.lang.Exception: std::exception: vector too long at org.opencv.ml.StatModel.train_0(Native Method) at org.opencv.ml.StatModel.train(StatModel.java:95) at com.selw.Main.main(Main.java:35) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140) 线程“主”中的异常java.lang.Exception:std :: exception:org.opencv.ml.StatModel.train(StatMethod.java:95处org.opencv.ml.StatModel.train_0(本机方法)处的向量太长),位于com.selw.Main.main(Main.java:35),位于sun.reflect.NativeMethodAccessorImpl.invoke0(本地方法),位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62),位于sun.reflect.DelegatingMethodAccessorImpl。在com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)处java.lang.reflect.Method.invoke(Method.java:497)处invoke(DelegatingMethodAccessorImpl.java:43)

The Mat data object contains the sample data that the tree will train on. Mat数据对象包含树将在其上训练的样本数据。 One row corresponds to one sample. 一行对应于一个样本。 The more samples you have, the better trained your tree will be. 您拥有的样本越多,您的树训练得越好。 You can have as many columns as you want in your Mat data object. Mat数据对象中可以具有任意数量的列。 Each column corresponds to a different sample feature. 每列对应一个不同的示例特征。

The Mat responses object contains values that the tree aims to predict. Mat响应对象包含树旨在预测的值。 This is a one column matrix with the same number of rows as your Mat data object. 这是一列矩阵,其行数与Mat数据对象相同。 Each row number in your Mat responses object corresponds to the sample with the same row number in your Mat data object. Mat响应对象中的每个行号对应于Mat数据对象中具有相同行号的样本。

For your example of forest fires being predicted based on a region's measurements of Temperature, Humidity, and Wind... each of the region's measurements need to be converted into floating point values. 例如,根据区域的温度,湿度和风向预测森林火灾的示例...该区域的每个度量都需要转换为浮点值。 Each region will have one row in your Mat data object, and one row in your Data responses object. 每个区域在Mat数据对象中将有一行,而在Data Response对象中将有一行。 The Mat data row will be filled with the region's measurements expressed as floating point values. Mat数据行将填充以浮点值表示的区域测量值。 The Mat responses row will be the number of forest fires (or as Nicholas pointed out in your comments, it might be better to place the number of forest fires into indexed histograms). 垫响应行将是森林火灾的数量(或正如Nicholas在您的评论中指出的那样,最好将森林火灾的数量放入索引的直方图中。

The current error in your code can be fixed by changing these settings: 您可以通过更改以下设置来修复代码中的当前错误:

    tree.setCVFolds(1); // opencv has not implemented pruning with cross-validation yet
    tree.setMaxDepth(10); // there is currently a bug that will run the depth of the tree to infinity if you do not set a max

I was able to compile and run without errors after changing those. 更改这些代码后,我能够编译并运行而没有错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM