diff --git a/fimdlp/CPPFImdlp.cpp b/fimdlp/CPPFImdlp.cpp index 416e101..5bd7829 100644 --- a/fimdlp/CPPFImdlp.cpp +++ b/fimdlp/CPPFImdlp.cpp @@ -32,17 +32,17 @@ namespace mdlp { { return xDiscretized; } - void CPPFImdlp::fit(samples& X_, labels& y_) + CPPFImdlp& CPPFImdlp::fit(samples& X_, labels& y_) { X = X_; y = y_; if (X.size() != y.size()) { std::cerr << "X and y must have the same size" << std::endl; - return; + return *this; } if (X.size() == 0) { std::cerr << "X and y must have at least one element" << std::endl; - return; + return *this; } this->indices = sortIndices(X_); this->xDiscretized = labels(X.size(), -1); @@ -55,6 +55,7 @@ namespace mdlp { } filterCutPoints(); applyCutPoints(); + return *this; } labels& CPPFImdlp::transform(samples& X_) { diff --git a/fimdlp/CPPFImdlp.h b/fimdlp/CPPFImdlp.h index 9dc01ad..08ac0ff 100644 --- a/fimdlp/CPPFImdlp.h +++ b/fimdlp/CPPFImdlp.h @@ -31,7 +31,7 @@ namespace mdlp { cutPoints_t getCutPoints(); labels getDiscretizedValues(); void debugPoints(samples&, labels&); - void fit(samples&, labels&); + CPPFImdlp& fit(samples&, labels&); labels& transform(samples&); }; } diff --git a/fimdlp/cfimdlp.pyx b/fimdlp/cfimdlp.pyx index 61534df..b4d553b 100644 --- a/fimdlp/cfimdlp.pyx +++ b/fimdlp/cfimdlp.pyx @@ -11,7 +11,7 @@ cdef extern from "CPPFImdlp.h" namespace "mdlp": cdef cppclass CPPFImdlp: CPPFImdlp() except + CPPFImdlp(bool, int, bool) except + - void fit(vector[float]&, vector[int]&) + CPPFImdlp& fit(vector[float]&, vector[int]&) vector[int] transform(vector[float]&) vector[int] getDiscretizedValues() vector[CutPointBody] getCutPoints() @@ -34,12 +34,13 @@ cdef class CFImdlp: del self.thisptr def fit(self, X, y): self.thisptr.fit(X, y) + return self def transform(self, X): return self.thisptr.transform(X) def get_discretized_values(self): return self.thisptr.getDiscretizedValues() def get_cut_points(self): - return self.thisptr.getCutPoints() + return self.thisptr.getCutPoints() def debug_points(self, X, y): return self.thisptr.debugPoints(X, y) \ No newline at end of file diff --git a/fimdlp/cppfimdlp.cpython-310-darwin.so b/fimdlp/cppfimdlp.cpython-310-darwin.so index 314839f..4cef9e9 100755 Binary files a/fimdlp/cppfimdlp.cpython-310-darwin.so and b/fimdlp/cppfimdlp.cpython-310-darwin.so differ diff --git a/sample.py b/sample.py index cf2f926..ce2db6c 100644 --- a/sample.py +++ b/sample.py @@ -66,28 +66,28 @@ features = data.feature_names # test.transform(X) # test.get_cut_points() -test = CFImdlp(debug=True, proposed=False) +test = CFImdlp(debug=False, proposed=False) # # k = test.cut_points(X[:, 0], y) # # print(k) # # k = test.cut_points_ant(X[:, 0], y) # # print(k) # # test.debug_points(X[:, 0], y) -X = [5.7, 5.3, 5.2, 5.1, 5.0, 5.6, 5.1, 6.0, 5.1, 5.9] -indices = [4, 3, 6, 8, 2, 1, 5, 0, 9, 7] -y = [1, 1, 1, 1, 1, 2, 2, 2, 2, 2] +# X = [5.7, 5.3, 5.2, 5.1, 5.0, 5.6, 5.1, 6.0, 5.1, 5.9] +# indices = [4, 3, 6, 8, 2, 1, 5, 0, 9, 7] +# y = [1, 1, 1, 1, 1, 2, 2, 2, 2, 2] # # To check # indices2 = np.argsort(X) # Xs = np.array(X)[indices2] # ys = np.array(y)[indices2] -# test.fit(X[:, 0], y) -test.fit(X, y) +test.fit(X[:, 0], y) +# test.fit(X, y) result = test.get_cut_points() -for item in result: - print( - f"*Class={item['classNumber']} - ({item['start']:3d}, {item['end']:3d})" - f" -> ({item['fromValue']:3.1f}, {item['toValue']:3.1f}]" - ) +# for item in result: +# print( +# f"Class={item['classNumber']} - ({item['start']:3d}, {item['end']:3d})" +# f" -> ({item['fromValue']:3.1f}, {item['toValue']:3.1f}]" +# ) print(test.get_discretized_values()) # print(Xs, ys) @@ -102,13 +102,13 @@ print(test.get_discretized_values()) # print(indices) # print(np.array(X)[indices]) -# X = np.array( -# [ -# [5.1, 3.5, 1.4, 0.2], -# [5.2, 3.0, 1.4, 0.2], -# [5.3, 3.2, 1.3, 0.2], -# [5.3, 3.1, 1.5, 0.2], -# ] -# ) -# y = np.array([0, 0, 0, 1]) -# test.fit(X, y).transform(X) +X = np.array( + [ + [5.1, 3.5, 1.4, 0.2], + [5.2, 3.0, 1.4, 0.2], + [5.3, 3.2, 1.3, 0.2], + [5.3, 3.1, 1.5, 0.2], + ] +) +y = np.array([0, 0, 0, 1]) +print(test.fit(X[:, 0], y).transform(X[:, 0]))