new cutPoints algo complete

This commit is contained in:
2022-11-29 17:26:09 +01:00
parent 36c5930c5e
commit c965d392c4
8 changed files with 66 additions and 46 deletions

View File

@@ -21,48 +21,47 @@ namespace CPPFImdlp
{ {
std::vector<float> cutPts; std::vector<float> cutPts;
std::vector<size_t> cutIdx; std::vector<size_t> cutIdx;
float xPrev, cutPoint, curx; float xPrev, xCur, xPivot, cutPoint;
int yPrev, cury; int yPrev, yCur, yPivot;
size_t idxPrev, idx; size_t idxPrev, idx, numElements;
bool first = true;
std::vector<size_t> indices = sortIndices(X); std::vector<size_t> indices = sortIndices(X);
xPrev = X.at(indices.at(0)); xCur = xPrev = X.at(indices.at(0));
yPrev = y.at(indices.at(0)); yCur = yPrev = y.at(indices.at(0));
idxPrev = indices.at(0); numElements = indices.size() - 1;
// idxPrev = indices.at(0);
idx = 0; idx = 0;
while (idx < indices.size() - 1)
{
if (first)
{
first = false;
curx = X.at(indices.at(idx));
cury = y.at(indices.at(idx));
}
if (debug) if (debug)
printf("<idx=%lu -> (%3.1f, %d) Prev(%3.1f, %d)\n", idx, curx, cury, xPrev, yPrev); printf("*idx=%lu -> (-1, -1) Prev(%3.1f, %d) Elementos: %lu\n", idx, xCur, yCur, numElements);
while (idx < numElements)
{
xPivot = xCur;
yPivot = yCur;
if (debug)
printf("<idx=%lu -> Prev(%3.1f, %d) Pivot(%3.1f, %d) Cur(%3.1f, %d) \n", idx, xPrev, yPrev, xPivot, yPivot, xCur, yCur);
// Read the same values and check class changes // Read the same values and check class changes
while (idx < indices.size() - 1 && curx == xPrev) do
{ {
idx++; idx++;
curx = X.at(indices.at(idx)); xCur = X.at(indices.at(idx));
cury = y.at(indices.at(idx)); yCur = y.at(indices.at(idx));
if (cury != yPrev && curx == xPrev) if (yCur != yPivot && xCur == xPivot)
{ {
yPrev = -1; yPivot = -1;
} }
if (debug) if (debug)
printf(">idx=%lu -> (%3.1f, %d) Prev(%3.1f, %d)\n", idx, curx, cury, xPrev, yPrev); printf(">idx=%lu -> Prev(%3.1f, %d) Pivot(%3.1f, %d) Cur(%3.1f, %d) \n", idx, xPrev, yPrev, xPivot, yPivot, xCur, yCur);
} } while (idx < numElements && xCur == xPivot);
if (yPrev == -1 || yPrev != cury) if (yPivot == -1 || yPrev != yCur)
{ {
cutPoint = (xPrev + curx) / 2; cutPoint = (xPrev + xCur) / 2;
printf("Cutpoint (%3.1f, %d) -> (%3.1f, %d) = %3.1f", xPrev, yPrev, curx, cury, cutPoint); if (debug)
printf("Cutpoint idx=%lu Cur(%3.1f, %d) Prev(%3.1f, %d) Pivot(%3.1f, %d) = %3.1f \n", idx, xCur, yCur, xPrev, yPrev, xPivot, yPivot, cutPoint);
cutPts.push_back(cutPoint); cutPts.push_back(cutPoint);
cutIdx.push_back(idxPrev); // cutIdx.push_back(idxPrev);
} }
yPrev = cury; yPrev = yPivot;
xPrev = curx; xPrev = xPivot;
idxPrev = indices.at(idx); // idxPrev = indices.at(idxPivot);
} }
return cutPts; return cutPts;
} }

View File

@@ -1,7 +1,6 @@
#ifndef CPPFIMDLP_H #ifndef CPPFIMDLP_H
#define CPPFIMDLP_H #define CPPFIMDLP_H
#include <vector> #include <vector>
#include <Python.h>
#include <utility> #include <utility>
namespace CPPFImdlp namespace CPPFImdlp
{ {

View File

@@ -1,8 +1,6 @@
#ifndef METRICS_H #ifndef METRICS_H
#define METRICS_H #define METRICS_H
#include <vector> #include <vector>
#include <Python.h>
#include <utility>
namespace CPPFImdlp namespace CPPFImdlp
{ {
class Metrics class Metrics

View File

@@ -61,7 +61,7 @@ class FImdlp(TransformerMixin, BaseEstimator):
self.n_features_ = X.shape[1] self.n_features_ = X.shape[1]
self.X_ = X self.X_ = X
self.y_ = y self.y_ = y
self.discretizer_ = CFImdlp(debug=True) self.discretizer_ = CFImdlp(debug=False)
return self return self
def transform(self, X): def transform(self, X):
@@ -108,7 +108,13 @@ class FImdlp(TransformerMixin, BaseEstimator):
datax = self.X_[np.argsort(self.X_[:, i]), i] datax = self.X_[np.argsort(self.X_[:, i]), i]
y_ = self.y_[np.argsort(self.X_[:, i])] y_ = self.y_[np.argsort(self.X_[:, i])]
Xcutpoints = self.discretizer_.cut_points(datax, y_) Xcutpoints = self.discretizer_.cut_points(datax, y_)
print(f"New:{self.features_[i]:20s}: {Xcutpoints}") print(
f"New ({len(Xcutpoints)}):{self.features_[i]:20s}: "
f"{Xcutpoints}"
)
Xcutpoints = self.discretizer_.cut_points_ant(datax, y_) Xcutpoints = self.discretizer_.cut_points_ant(datax, y_)
print(f"Ant:{self.features_[i]:20s}: {Xcutpoints}") print(
f"Ant ({len(Xcutpoints)}):{self.features_[i]:20s}: "
f"{Xcutpoints}"
)
return X return X

BIN
fimdlp/test Executable file

Binary file not shown.

18
fimdlp/test.cpp Normal file
View File

@@ -0,0 +1,18 @@
#include "CPPFImdlp.h"
#include <iostream>
using namespace std;
int main(int argc, char *argv[], char *envp[])
{
{
CPPFImdlp::CPPFImdlp fimdlp = CPPFImdlp::CPPFImdlp(true);
vector<float> X = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
vector<int> y = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
vector<float> cutPts = fimdlp.cutPoints(X, y);
for (auto &cutPt : cutPts)
{
cout << cutPt << endl;
}
return 0;
}
}

View File

@@ -8,15 +8,15 @@ X = data.data
y = data.target y = data.target
features = data.feature_names features = data.feature_names
test = FImdlp() test = FImdlp()
# test.fit(X, y, features=features).transform(X) test.fit(X, y, features=features).transform(X)
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) # test.fit(X, y).transform(X)