Refactor some methods

This commit is contained in:
2022-12-02 23:36:18 +01:00
parent f28465a64f
commit b1f5d337fc
5 changed files with 29 additions and 27 deletions

View File

@@ -32,17 +32,17 @@ namespace mdlp {
{ {
return xDiscretized; return xDiscretized;
} }
void CPPFImdlp::fit(samples& X_, labels& y_) CPPFImdlp& CPPFImdlp::fit(samples& X_, labels& y_)
{ {
X = X_; X = X_;
y = y_; y = y_;
if (X.size() != y.size()) { if (X.size() != y.size()) {
std::cerr << "X and y must have the same size" << std::endl; std::cerr << "X and y must have the same size" << std::endl;
return; return *this;
} }
if (X.size() == 0) { if (X.size() == 0) {
std::cerr << "X and y must have at least one element" << std::endl; std::cerr << "X and y must have at least one element" << std::endl;
return; return *this;
} }
this->indices = sortIndices(X_); this->indices = sortIndices(X_);
this->xDiscretized = labels(X.size(), -1); this->xDiscretized = labels(X.size(), -1);
@@ -55,6 +55,7 @@ namespace mdlp {
} }
filterCutPoints(); filterCutPoints();
applyCutPoints(); applyCutPoints();
return *this;
} }
labels& CPPFImdlp::transform(samples& X_) labels& CPPFImdlp::transform(samples& X_)
{ {

View File

@@ -31,7 +31,7 @@ namespace mdlp {
cutPoints_t getCutPoints(); cutPoints_t getCutPoints();
labels getDiscretizedValues(); labels getDiscretizedValues();
void debugPoints(samples&, labels&); void debugPoints(samples&, labels&);
void fit(samples&, labels&); CPPFImdlp& fit(samples&, labels&);
labels& transform(samples&); labels& transform(samples&);
}; };
} }

View File

@@ -11,7 +11,7 @@ cdef extern from "CPPFImdlp.h" namespace "mdlp":
cdef cppclass CPPFImdlp: cdef cppclass CPPFImdlp:
CPPFImdlp() except + CPPFImdlp() except +
CPPFImdlp(bool, int, bool) 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] transform(vector[float]&)
vector[int] getDiscretizedValues() vector[int] getDiscretizedValues()
vector[CutPointBody] getCutPoints() vector[CutPointBody] getCutPoints()
@@ -34,12 +34,13 @@ cdef class CFImdlp:
del self.thisptr del self.thisptr
def fit(self, X, y): def fit(self, X, y):
self.thisptr.fit(X, y) self.thisptr.fit(X, y)
return self
def transform(self, X): def transform(self, X):
return self.thisptr.transform(X) return self.thisptr.transform(X)
def get_discretized_values(self): def get_discretized_values(self):
return self.thisptr.getDiscretizedValues() return self.thisptr.getDiscretizedValues()
def get_cut_points(self): def get_cut_points(self):
return self.thisptr.getCutPoints() return self.thisptr.getCutPoints()
def debug_points(self, X, y): def debug_points(self, X, y):
return self.thisptr.debugPoints(X, y) return self.thisptr.debugPoints(X, y)

View File

@@ -66,28 +66,28 @@ features = data.feature_names
# test.transform(X) # test.transform(X)
# test.get_cut_points() # test.get_cut_points()
test = CFImdlp(debug=True, proposed=False) test = CFImdlp(debug=False, proposed=False)
# # k = test.cut_points(X[:, 0], y) # # k = test.cut_points(X[:, 0], y)
# # print(k) # # print(k)
# # k = test.cut_points_ant(X[:, 0], y) # # k = test.cut_points_ant(X[:, 0], y)
# # print(k) # # print(k)
# # test.debug_points(X[:, 0], y) # # 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] # 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] # indices = [4, 3, 6, 8, 2, 1, 5, 0, 9, 7]
y = [1, 1, 1, 1, 1, 2, 2, 2, 2, 2] # y = [1, 1, 1, 1, 1, 2, 2, 2, 2, 2]
# # To check # # To check
# indices2 = np.argsort(X) # indices2 = np.argsort(X)
# Xs = np.array(X)[indices2] # Xs = np.array(X)[indices2]
# ys = np.array(y)[indices2] # ys = np.array(y)[indices2]
# test.fit(X[:, 0], y) test.fit(X[:, 0], y)
test.fit(X, y) # test.fit(X, y)
result = test.get_cut_points() result = test.get_cut_points()
for item in result: # for item in result:
print( # print(
f"*Class={item['classNumber']} - ({item['start']:3d}, {item['end']:3d})" # f"Class={item['classNumber']} - ({item['start']:3d}, {item['end']:3d})"
f" -> ({item['fromValue']:3.1f}, {item['toValue']:3.1f}]" # f" -> ({item['fromValue']:3.1f}, {item['toValue']:3.1f}]"
) # )
print(test.get_discretized_values()) print(test.get_discretized_values())
# print(Xs, ys) # print(Xs, ys)
@@ -102,13 +102,13 @@ print(test.get_discretized_values())
# print(indices) # print(indices)
# print(np.array(X)[indices]) # print(np.array(X)[indices])
# X = np.array( X = np.array(
# [ [
# [5.1, 3.5, 1.4, 0.2], [5.1, 3.5, 1.4, 0.2],
# [5.2, 3.0, 1.4, 0.2], [5.2, 3.0, 1.4, 0.2],
# [5.3, 3.2, 1.3, 0.2], [5.3, 3.2, 1.3, 0.2],
# [5.3, 3.1, 1.5, 0.2], [5.3, 3.1, 1.5, 0.2],
# ] ]
# ) )
# y = np.array([0, 0, 0, 1]) y = np.array([0, 0, 0, 1])
# test.fit(X, y).transform(X) print(test.fit(X[:, 0], y).transform(X[:, 0]))