diff --git a/.gitignore b/.gitignore index b6e4761..cbabd70 100644 --- a/.gitignore +++ b/.gitignore @@ -127,3 +127,5 @@ dmypy.json # Pyre type checker .pyre/ +cfimdlp.cpp +.vscode/* diff --git a/FImdlp.cpp b/FImdlp.cpp new file mode 100644 index 0000000..19241c7 --- /dev/null +++ b/FImdlp.cpp @@ -0,0 +1,25 @@ +#include "FImdlp.h" +namespace FImdlp +{ + FImdlp::FImdlp() + { + } + FImdlp::~FImdlp() + { + } + std::vector FImdlp::cutPoints(std::vector &X, std::vector &y) + { + std::vector cutPts; + int i, ant = X.at(0); + int n = X.size(); + for (i = 1; i < n; i++) + { + if (X.at(i) != ant) + { + cutPts.push_back(float(X.at(i) + ant) / 2); + ant = X.at(i); + } + } + return cutPts; + } +} \ No newline at end of file diff --git a/FImdlp.h b/FImdlp.h new file mode 100644 index 0000000..d15cf8b --- /dev/null +++ b/FImdlp.h @@ -0,0 +1,15 @@ +#ifndef FIMDLP_H +#define FIMDLP_H +#include +#include +namespace FImdlp +{ + class FImdlp + { + public: + FImdlp(); + ~FImdlp(); + std::vector cutPoints(std::vector &, std::vector &); + }; +} +#endif \ No newline at end of file diff --git a/cfimdlp.pyx b/cfimdlp.pyx new file mode 100644 index 0000000..0e5c886 --- /dev/null +++ b/cfimdlp.pyx @@ -0,0 +1,16 @@ +# distutils: language = c++ +# cython: language_level = 3 +from libcpp.vector cimport vector +cdef extern from "FImdlp.h" namespace "FImdlp": + cdef cppclass FImdlp: + FImdlp() except + + vector[float] cutPoints(vector[int]&, vector[int]&) + +cdef class CFImdlp: + cdef FImdlp *thisptr + def __cinit__(self): + self.thisptr = new FImdlp() + def __dealloc__(self): + del self.thisptr + def cut_points(self, X, y): + return self.thisptr.cutPoints(X, y) diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..b80d296 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,8 @@ +# pyproject.toml +[build-system] +requires = ["setuptools", "cython"] +build-backend = "setuptools.build_meta" + +[project] +name = "FImdlp" +version = "0.1.0" diff --git a/sample.py b/sample.py new file mode 100644 index 0000000..43df59e --- /dev/null +++ b/sample.py @@ -0,0 +1,14 @@ +import numpy as np +from sklearn.datasets import load_iris +from fimdlp import CFImdlp + +data = load_iris() +X = data.data +y = data.target +features = data.feature_names +test = CFImdlp() +print("Cut points for each feature in Iris dataset:") +for i in range(0, X.shape[1]): + data = np.sort(X[:, i]) + Xcutpoints = test.cut_points(data, y) + print(f"{features[i]:20s}: {Xcutpoints}") diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..b38b4b4 --- /dev/null +++ b/setup.py @@ -0,0 +1,17 @@ +""" + Calling + $python setup.py build_ext --inplace + will build the extension library in the current file. +""" + +from setuptools import Extension, setup + +setup( + ext_modules=[ + Extension( + name="fimdlp", + sources=["cfimdlp.pyx", "FImdlp.cpp"], + language="c++", + ), + ] +)