mirror of
https://github.com/Doctorado-ML/FImdlp.git
synced 2025-08-17 16:35:52 +00:00
Add debug output and precision
This commit is contained in:
@@ -3,35 +3,45 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
namespace CPPFImdlp
|
namespace CPPFImdlp
|
||||||
{
|
{
|
||||||
CPPFImdlp::CPPFImdlp()
|
CPPFImdlp::CPPFImdlp() : debug(false), precision(6)
|
||||||
{
|
{
|
||||||
|
divider = pow(10, precision);
|
||||||
|
}
|
||||||
|
CPPFImdlp::CPPFImdlp(int precision, bool debug) : debug(debug), precision(precision)
|
||||||
|
{
|
||||||
|
divider = pow(10, precision);
|
||||||
}
|
}
|
||||||
CPPFImdlp::~CPPFImdlp()
|
CPPFImdlp::~CPPFImdlp()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
std::vector<double> CPPFImdlp::cutPoints(std::vector<float> &X, std::vector<int> &y)
|
std::vector<float> CPPFImdlp::cutPoints(std::vector<float> &X, std::vector<int> &y)
|
||||||
{
|
{
|
||||||
std::vector<double> cutPts;
|
std::vector<float> cutPts;
|
||||||
double antx;
|
float antx, cutPoint;
|
||||||
// int anty;
|
int anty;
|
||||||
std::vector<size_t> indices = sortIndices(X);
|
std::vector<size_t> indices = sortIndices(X);
|
||||||
antx = X.at(indices[0]);
|
antx = X.at(indices[0]);
|
||||||
// anty = y.at(indices[0]);
|
anty = y.at(indices[0]);
|
||||||
for (auto index = indices.begin(); index != indices.end(); ++index)
|
for (auto index = indices.begin(); index != indices.end(); ++index)
|
||||||
{
|
{
|
||||||
// std::cout << X.at(*index) << " -> " << y.at(*index) << " // ";
|
// std::cout << X.at(*index) << " -> " << y.at(*index) << " // ";
|
||||||
// Definition 2 Cut points are always on boundaries
|
// Definition 2 Cut points are always on boundaries
|
||||||
// if (y.at(*index) != anty && antx < X.at(*index))
|
if (y.at(*index) != anty && antx < X.at(*index))
|
||||||
// Weka implementation
|
// Weka implementation
|
||||||
if (antx < X.at(*index))
|
// if (antx < X.at(*index))
|
||||||
{
|
{
|
||||||
// std::cout << "* (" << X.at(*index) << ", " << antx << ") // ";
|
cutPoint = round((X.at(*index) + antx) / 2 * divider) / divider;
|
||||||
cutPts.push_back((X.at(*index) + antx) / 2);
|
if (debug)
|
||||||
// anty = y.at(*index);
|
{
|
||||||
|
std::cout << "Cut point: " << (antx + X.at(*index)) / 2 << " //";
|
||||||
|
std::cout << X.at(*index) << " -> " << y.at(*index) << " anty= " << anty;
|
||||||
|
std::cout << "* (" << X.at(*index) << ", " << antx << ")=" << ((X.at(*index) + antx) / 2) << std::endl;
|
||||||
|
}
|
||||||
|
cutPts.push_back(cutPoint);
|
||||||
}
|
}
|
||||||
antx = X.at(*index);
|
antx = X.at(*index);
|
||||||
|
anty = y.at(*index);
|
||||||
}
|
}
|
||||||
// std::cout << std::endl;
|
|
||||||
return cutPts;
|
return cutPts;
|
||||||
}
|
}
|
||||||
std::vector<size_t> CPPFImdlp::sortIndices(std::vector<float> &X)
|
std::vector<size_t> CPPFImdlp::sortIndices(std::vector<float> &X)
|
||||||
|
@@ -8,12 +8,17 @@ namespace CPPFImdlp
|
|||||||
class CPPFImdlp
|
class CPPFImdlp
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
std::vector<size_t> sortIndices(std::vector<float> &);
|
bool debug;
|
||||||
|
int precision;
|
||||||
|
float divider;
|
||||||
|
std::vector<size_t>
|
||||||
|
sortIndices(std::vector<float> &);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CPPFImdlp();
|
CPPFImdlp();
|
||||||
|
CPPFImdlp(int, bool debug = false);
|
||||||
~CPPFImdlp();
|
~CPPFImdlp();
|
||||||
std::vector<double> cutPoints(std::vector<float> &, std::vector<int> &);
|
std::vector<float> cutPoints(std::vector<float> &, std::vector<int> &);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
@@ -1,16 +1,18 @@
|
|||||||
# distutils: language = c++
|
# distutils: language = c++
|
||||||
# cython: language_level = 3
|
# cython: language_level = 3
|
||||||
from libcpp.vector cimport vector
|
from libcpp.vector cimport vector
|
||||||
|
from libcpp cimport bool
|
||||||
|
|
||||||
cdef extern from "CPPFImdlp.h" namespace "CPPFImdlp":
|
cdef extern from "CPPFImdlp.h" namespace "CPPFImdlp":
|
||||||
cdef cppclass CPPFImdlp:
|
cdef cppclass CPPFImdlp:
|
||||||
CPPFImdlp() except +
|
CPPFImdlp() except +
|
||||||
vector[double] cutPoints(vector[float]&, vector[int]&)
|
CPPFImdlp(int, bool) except +
|
||||||
|
vector[float] cutPoints(vector[float]&, vector[int]&)
|
||||||
|
|
||||||
cdef class CFImdlp:
|
cdef class CFImdlp:
|
||||||
cdef CPPFImdlp *thisptr
|
cdef CPPFImdlp *thisptr
|
||||||
def __cinit__(self):
|
def __cinit__(self, precision=6, debug=False):
|
||||||
self.thisptr = new CPPFImdlp()
|
self.thisptr = new CPPFImdlp(precision, debug)
|
||||||
def __dealloc__(self):
|
def __dealloc__(self):
|
||||||
del self.thisptr
|
del self.thisptr
|
||||||
def cut_points(self, X, y):
|
def cut_points(self, X, y):
|
||||||
|
Binary file not shown.
@@ -19,9 +19,6 @@ class FImdlp(TransformerMixin, BaseEstimator):
|
|||||||
The number of features of the data passed to :meth:`fit`.
|
The number of features of the data passed to :meth:`fit`.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def _check_params_fit(self, X, y, expected_args, kwargs):
|
def _check_params_fit(self, X, y, expected_args, kwargs):
|
||||||
"""Check the common parameters passed to fit"""
|
"""Check the common parameters passed to fit"""
|
||||||
# Check that X and y have correct shape
|
# Check that X and y have correct shape
|
||||||
@@ -93,7 +90,7 @@ class FImdlp(TransformerMixin, BaseEstimator):
|
|||||||
# during fit.
|
# during fit.
|
||||||
if X.shape[1] != self.n_features_:
|
if X.shape[1] != self.n_features_:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"Shape of input is different from what was seen" "in `fit`"
|
"Shape of input is different from what was seen in `fit`"
|
||||||
)
|
)
|
||||||
print("Cut points for each feature in Iris dataset:")
|
print("Cut points for each feature in Iris dataset:")
|
||||||
yz = self.y_.copy()
|
yz = self.y_.copy()
|
||||||
|
@@ -6,8 +6,8 @@ data = load_iris()
|
|||||||
X = data.data
|
X = data.data
|
||||||
y = data.target
|
y = data.target
|
||||||
features = data.feature_names
|
features = data.feature_names
|
||||||
# test = FImdlp()
|
test = FImdlp()
|
||||||
# Xcutpoints = test.fit(X, y, features=features).transform(X)
|
# Xcutpoints = test.fit(X, y, features=features).transform(X)
|
||||||
clf = CFImdlp()
|
clf = CFImdlp(debug=True)
|
||||||
print("Cut points for feature 0 in Iris dataset:")
|
print("Cut points for feature 0 in Iris dataset:")
|
||||||
print(clf.cut_points(X[:, 0], y))
|
print(clf.cut_points(X[:, 0], y))
|
||||||
|
Reference in New Issue
Block a user