36 const char* fields[] = {
"value",
"classIdx",
"parent",
"left",
"right",
37 "defaultDir",
"split"};
39 for (
size_t i = 0; i < nodes.
size(); ++i) {
40 s.
set(
"value", nodes[i].value, i);
41 s.
set(
"classIdx", nodes[i].classIdx, i);
42 s.
set(
"parent", nodes[i].parent, i);
43 s.
set(
"left", nodes[i].left, i);
44 s.
set(
"right", nodes[i].right, i);
45 s.
set(
"defaultDir", nodes[i].defaultDir, i);
53 const char* fields[] = {
"varIdx",
"inversed",
"quality",
"next",
"c",
56 for (
size_t i = 0; i < splits.
size(); ++i) {
57 s.
set(
"varIdx", splits[i].varIdx, i);
58 s.
set(
"inversed", splits[i].inversed, i);
59 s.
set(
"quality", splits[i].quality, i);
60 s.
set(
"next", splits[i].next, i);
61 s.
set(
"c", splits[i].c, i);
62 s.
set(
"subsetOfs", splits[i].subsetOfs, i);
71 const Mat& samples,
const Mat& responses,
77 Mat varIdx, sampleIdx, sampleWeights, varType;
80 double splitRatio = -1.0;
81 bool splitShuffle =
true;
82 for (; first != last; first += 2) {
83 string key(first->toString());
84 const MxArray& val = *(first + 1);
87 else if (key ==
"VarIdx")
89 (val.isUint8() || val.isLogical()) ?
CV_8U :
CV_32S);
90 else if (key ==
"SampleIdx")
91 sampleIdx = val.toMat(
92 (val.isUint8() || val.isLogical()) ?
CV_8U :
CV_32S);
93 else if (key ==
"SampleWeights")
94 sampleWeights = val.toMat(
CV_32F);
95 else if (key ==
"VarType") {
99 for (
size_t idx = 0; idx < vtypes.
size(); idx++)
102 else if (val.isChar()) {
103 string str(val.toString());
105 for (
size_t idx = 0; idx < str.size(); idx++)
108 else if (val.isNumeric())
109 varType = val.toMat(
CV_8U);
113 else if (key ==
"MissingMask")
114 missing = val.toMat(
CV_8U);
115 else if (key ==
"TrainTestSplitCount")
116 splitCount = val.toInt();
117 else if (key ==
"TrainTestSplitRatio")
118 splitRatio = val.toDouble();
119 else if (key ==
"TrainTestSplitShuffle")
120 splitShuffle = val.toBool();
123 "Unrecognized option %s", key.c_str());
126 varIdx, sampleIdx, sampleWeights, varType);
129 else if (splitRatio >= 0)
139 int headerLineCount = 1;
140 int responseStartIdx = -1;
141 int responseEndIdx = -1;
143 char delimiter =
',';
146 double splitRatio = -1.0;
147 bool splitShuffle =
true;
148 for (; first != last; first += 2) {
149 string key(first->toString());
150 const MxArray& val = *(first + 1);
151 if (key ==
"HeaderLineCount")
152 headerLineCount = val.
toInt();
153 else if (key ==
"ResponseStartIdx")
154 responseStartIdx = val.toInt();
155 else if (key ==
"ResponseEndIdx")
156 responseEndIdx = val.toInt();
157 else if (key ==
"VarTypeSpec")
158 varTypeSpec = val.toString();
159 else if (key ==
"Delimiter")
160 delimiter = (!val.isEmpty()) ? val.toString()[0] :
' ';
161 else if (key ==
"Missing")
162 missch = (!val.isEmpty()) ? val.toString()[0] :
'?';
163 else if (key ==
"TrainTestSplitCount")
164 splitCount = val.toInt();
165 else if (key ==
"TrainTestSplitRatio")
166 splitRatio = val.toDouble();
167 else if (key ==
"TrainTestSplitShuffle")
168 splitShuffle = val.toBool();
171 "Unrecognized option %s", key.c_str());
174 responseStartIdx, responseEndIdx, varTypeSpec, delimiter, missch);
177 "Failed to load dataset '%s'", filename.
c_str());
180 else if (splitRatio >= 0)
int toInt() const
Convert MxArray to int.
void split(const Mat &src, Mat *mvbegin)
static Ptr< TrainData > create(InputArray samples, int layout, InputArray responses, InputArray varIdx=noArray(), InputArray sampleIdx=noArray(), InputArray sampleWeights=noArray(), InputArray varType=noArray())
const ConstMap< string, int > VariableTypeMap
Option values for variable types.
MxArray toStruct(const vector< DTrees::Node > &nodes)
Convert tree nodes to struct array.
void set(mwIndex index, const T &value)
Template for numeric array element write accessor.
virtual void setTrainTestSplit(int count, bool shuffle=true)=0
virtual void setTrainTestSplitRatio(double ratio, bool shuffle=true)=0
LIBMWMEX_API_EXTERN_C void mexErrMsgIdAndTxt(const char *identifier, const char *err_msg,...)
Issue formatted error message with corresponding error identifier and return to MATLAB prompt...
mxArray object wrapper for data conversion and manipulation.
void nargchk(bool cond)
Alias for input/output arguments number check.
static MxArray Struct(const char **fields=NULL, int nfields=0, mwSize m=1, mwSize n=1)
Create a new struct array.
static Ptr< TrainData > loadFromCSV(const String &filename, int headerLineCount, int responseStartIdx=-1, int responseEndIdx=-1, const String &varTypeSpec=String(), char delimiter=',', char missch='?')
Ptr< TrainData > createTrainData(const Mat &samples, const Mat &responses, vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of TrainData using options in arguments.
void create(int rows, int cols, int type)
const ConstMap< string, int > SampleTypesMap
Option values for sample layouts.
Ptr< TrainData > loadTrainData(const string &filename, vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Read a dataset from a CSV file.
std::map wrapper with one-line initialization and lookup method.
Common definitions for the ml module.