refactor fit parameters
This commit is contained in:
@@ -21,25 +21,19 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
tuple<Tensor, Tensor, vector<string>, string, map<string, vector<int>>> loadDataset(const string& name, bool class_last)
|
tuple<Tensor, Tensor> loadDataset(const string& name, bool class_last)
|
||||||
{
|
{
|
||||||
auto handler = ArffFiles();
|
auto handler = ArffFiles();
|
||||||
handler.load(Paths::datasets() + static_cast<string>(name) + ".arff", class_last);
|
handler.load(Paths::datasets() + static_cast<string>(name) + ".arff", class_last);
|
||||||
// Get Dataset X, y
|
// Get Dataset X, y
|
||||||
vector<vector<float>> X = handler.getX();
|
vector<vector<float>> X = handler.getX();
|
||||||
vector<int> y = handler.getY();
|
vector<int> y = handler.getY();
|
||||||
// Get className & Features
|
|
||||||
auto className = handler.getClassName();
|
|
||||||
vector<string> features;
|
|
||||||
auto attributes = handler.getAttributes();
|
|
||||||
transform(attributes.begin(), attributes.end(), back_inserter(features), [](const auto& pair) { return pair.first; });
|
|
||||||
Tensor Xd;
|
Tensor Xd;
|
||||||
auto states = map<string, vector<int>>();
|
|
||||||
Xd = torch::zeros({ static_cast<int>(X.size()), static_cast<int>(X[0].size()) }, torch::kFloat32);
|
Xd = torch::zeros({ static_cast<int>(X.size()), static_cast<int>(X[0].size()) }, torch::kFloat32);
|
||||||
for (int i = 0; i < features.size(); ++i) {
|
for (int i = 0; i < X.size(); ++i) {
|
||||||
Xd.index_put_({ i, "..." }, torch::tensor(X[i], torch::kFloat32));
|
Xd.index_put_({ i, "..." }, torch::tensor(X[i], torch::kFloat32));
|
||||||
}
|
}
|
||||||
return { Xd, torch::tensor(y, torch::kInt32), features, className, states };
|
return { Xd, torch::tensor(y, torch::kInt32) };
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
@@ -50,7 +44,7 @@ int main(int argc, char* argv[])
|
|||||||
using namespace torch::indexing;
|
using namespace torch::indexing;
|
||||||
auto datasetName = "iris";
|
auto datasetName = "iris";
|
||||||
bool class_last = true;
|
bool class_last = true;
|
||||||
auto [X, y, features, className, states] = loadDataset(datasetName, class_last);
|
auto [X, y] = loadDataset(datasetName, class_last);
|
||||||
auto m = y.size(0);
|
auto m = y.size(0);
|
||||||
int train_split = m * .7;
|
int train_split = m * .7;
|
||||||
auto Xtrain = X.index({ "...", Slice(0, train_split) });
|
auto Xtrain = X.index({ "...", Slice(0, train_split) });
|
||||||
@@ -60,38 +54,57 @@ int main(int argc, char* argv[])
|
|||||||
cout << "Dataset: " << datasetName << endl;
|
cout << "Dataset: " << datasetName << endl;
|
||||||
cout << "X: " << X.sizes() << endl;
|
cout << "X: " << X.sizes() << endl;
|
||||||
cout << "y: " << y.sizes() << endl;
|
cout << "y: " << y.sizes() << endl;
|
||||||
// auto clf = pywrap::STree();
|
cout << "Xtrain: " << Xtrain.sizes() << endl;
|
||||||
// auto stree = pywrap::STree();
|
cout << "ytrain: " << ytrain.sizes() << endl;
|
||||||
// auto hyperparameters = json::parse("{\"C\": 0.7, \"max_iter\": 10000, \"kernel\": \"rbf\", \"random_state\": 17}");
|
cout << "Xtest : " << Xtest.sizes() << endl;
|
||||||
// stree.setHyperparameters(hyperparameters);
|
cout << "ytest : " << ytest.sizes() << endl;
|
||||||
// cout << "STree Version: " << clf.version() << endl;
|
//
|
||||||
// auto svc = pywrap::SVC();
|
// STree
|
||||||
// cout << "SVC with hyperparameters" << endl;
|
//
|
||||||
// svc.fit(X, y, features, className, states);
|
auto clf = pywrap::STree();
|
||||||
// cout << "Graph: " << endl << clf.graph() << endl;
|
clf.fit(X, y);
|
||||||
// double clf_score = clf.fit(X, y, features, className, states).score(X, y);
|
double clf_score = clf.score(X, y);
|
||||||
// double stree_score = stree.fit(X, y, features, className, states).score(X, y);
|
auto stree = pywrap::STree();
|
||||||
// auto prediction = clf.predict(X);
|
auto hyperparameters = json::parse("{\"C\": 0.7, \"max_iter\": 10000, \"kernel\": \"rbf\", \"random_state\": 17}");
|
||||||
// cout << "Prediction: " << endl << "{";
|
stree.setHyperparameters(hyperparameters);
|
||||||
// for (int i = 0; i < prediction.size(0); ++i) {
|
cout << "STree Version: " << clf.version() << endl;
|
||||||
// cout << prediction[i].item<int>() << ", ";
|
auto prediction = clf.predict(X);
|
||||||
// }
|
cout << "Prediction: " << endl << "{";
|
||||||
// cout << "}" << endl;
|
for (int i = 0; i < prediction.size(0); ++i) {
|
||||||
// cout << "Building Random Forest" << endl;
|
cout << prediction[i].item<int>() << ", ";
|
||||||
// auto rf = pywrap::RandomForest();
|
}
|
||||||
// rf.fit(X, y, features, className, states);
|
cout << "}" << endl;
|
||||||
|
//
|
||||||
|
// SVC
|
||||||
|
//
|
||||||
|
auto svc = pywrap::SVC();
|
||||||
|
cout << "SVC with hyperparameters" << endl;
|
||||||
|
svc.fit(X, y);
|
||||||
|
//
|
||||||
|
// Random Forest
|
||||||
|
//
|
||||||
|
cout << "Building Random Forest" << endl;
|
||||||
|
auto rf = pywrap::RandomForest();
|
||||||
|
rf.fit(Xtrain, ytrain);
|
||||||
|
//
|
||||||
|
// XGBoost
|
||||||
|
//
|
||||||
cout << "Building XGBoost" << endl;
|
cout << "Building XGBoost" << endl;
|
||||||
auto xg = pywrap::XGBoost();
|
auto xg = pywrap::XGBoost();
|
||||||
cout << "Fitting XGBoost" << endl;
|
cout << "Fitting XGBoost" << endl;
|
||||||
xg.fit(Xtrain, ytrain, features, className, states);
|
// xg.fit(Xtrain, ytrain);
|
||||||
cout << "Scoring dataset" << endl;
|
// double xg_score = xg.score(Xtest, ytest);
|
||||||
double xg_score = xg.score(Xtest, ytest);
|
//
|
||||||
// cout << "Scores:" << endl;
|
// Scoring
|
||||||
// cout << "STree Score ......: " << clf_score << endl;
|
//
|
||||||
// cout << "STree hyper score : " << stree_score << endl;
|
cout << "Scoring dataset: " << datasetName << endl;
|
||||||
// cout << "RandomForest Score: " << rf.score(X, y) << endl;
|
cout << "Scores:" << endl;
|
||||||
// cout << "SVC Score ........: " << svc.score(X, y) << endl;
|
cout << "STree Score ......: " << clf_score << endl;
|
||||||
cout << "XGBoost Score ....: " << xg_score << endl;
|
cout << "STree train/test .: " << clf.fit(Xtrain, ytrain).score(Xtest, ytest) << endl;
|
||||||
|
cout << "STree hyper score : " << stree.fit(Xtrain, ytrain).score(Xtest, ytest) << endl;
|
||||||
|
cout << "RandomForest Score: " << rf.score(Xtest, ytest) << endl;
|
||||||
|
cout << "SVC Score ........: " << svc.score(X, y) << endl;
|
||||||
|
// cout << "XGBoost Score ....: " << xg_score << endl;
|
||||||
}
|
}
|
||||||
cout << "* End." << endl;
|
cout << "* End." << endl;
|
||||||
}
|
}
|
@@ -35,7 +35,7 @@ namespace pywrap {
|
|||||||
{
|
{
|
||||||
return pyWrap->callMethodString(id, method);
|
return pyWrap->callMethodString(id, method);
|
||||||
}
|
}
|
||||||
PyClassifier& PyClassifier::fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states)
|
PyClassifier& PyClassifier::fit(torch::Tensor& X, torch::Tensor& y)
|
||||||
{
|
{
|
||||||
if (!fitted && hyperparameters.size() > 0) {
|
if (!fitted && hyperparameters.size() > 0) {
|
||||||
pyWrap->setHyperparameters(id, hyperparameters);
|
pyWrap->setHyperparameters(id, hyperparameters);
|
||||||
@@ -47,6 +47,10 @@ namespace pywrap {
|
|||||||
fitted = true;
|
fitted = true;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
PyClassifier& PyClassifier::fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states)
|
||||||
|
{
|
||||||
|
return fit(X, y);
|
||||||
|
}
|
||||||
torch::Tensor PyClassifier::predict(torch::Tensor& X)
|
torch::Tensor PyClassifier::predict(torch::Tensor& X)
|
||||||
{
|
{
|
||||||
int dimension = X.size(1);
|
int dimension = X.size(1);
|
||||||
|
@@ -18,6 +18,7 @@ namespace pywrap {
|
|||||||
PyClassifier(const std::string& module, const std::string& className);
|
PyClassifier(const std::string& module, const std::string& className);
|
||||||
virtual ~PyClassifier();
|
virtual ~PyClassifier();
|
||||||
PyClassifier& fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states);
|
PyClassifier& fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states);
|
||||||
|
PyClassifier& fit(torch::Tensor& X, torch::Tensor& y);
|
||||||
torch::Tensor predict(torch::Tensor& X);
|
torch::Tensor predict(torch::Tensor& X);
|
||||||
double score(torch::Tensor& X, torch::Tensor& y);
|
double score(torch::Tensor& X, torch::Tensor& y);
|
||||||
std::string version();
|
std::string version();
|
||||||
|
Reference in New Issue
Block a user