-
Notifications
You must be signed in to change notification settings - Fork 15
Added Decision tree model #32 issue #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Sar-thak-3
wants to merge
16
commits into
PEC-CSS:main
Choose a base branch
from
Sar-thak-3:decision_tree
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
1e23409
Merge branch 'main' of https://github.com/PEC-CSS/slowmokit into deci…
Sar-thak-3 06c3650
Errors of decision tree removed
Sar-thak-3 6678c57
Merge branch 'main' of https://github.com/PEC-CSS/slowmokit into deci…
Sar-thak-3 49b04eb
Changes in dt
Sar-thak-3 5ced617
changes
Sar-thak-3 301a5cf
decision tree with docs examples code
Sar-thak-3 1676e79
comments
Sar-thak-3 64e8962
cmake texts
Sar-thak-3 7c63ffb
cmake texts
Sar-thak-3 c6e8e2e
Merge branch 'main' of https://github.com/PEC-CSS/slowmokit into deci…
Sar-thak-3 f49b5b4
cmake errors last line
Sar-thak-3 398432f
matrix.cpp missing in ducks. Done manually
Sar-thak-3 4e27635
Merge branch 'main' of https://github.com/PEC-CSS/slowmokit into deci…
Sar-thak-3 4b7320c
Merge branch 'main' of https://github.com/PEC-CSS/slowmokit into deci…
Sar-thak-3 eefc405
Merge branch 'main' of https://github.com/PEC-CSS/slowmokit into deci…
Sar-thak-3 2dd60d5
cmake till slowmokit.cpp
Sar-thak-3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # Decision Tree | ||
|
|
||
| Decision Tree model. | ||
|
|
||
| Decision Tree forms the tree based on the best information gain we received at each node. The tree formed later use to predict the output values for test data. | ||
|
|
||
|
|
||
| ## Attributes | ||
|
|
||
| | Name | Definition | Shape | | ||
| | ------------ | --------------------------------------------------------- | ---------- | | ||
| | entropy | Entropy of the y values left | 1 | | ||
| | max depth | Max depth of the decision tree | 1 | | ||
| | max depth | Max depth of the decision tree | 1 | | ||
| | min Samples leaf | Minimum samples that should be there to form further nodes of branch | 1 | | ||
| | target | Output class | 1 | | ||
| | max features | Max features that should be considered to form further branches | 1 | | ||
| | features | All the features/columns in data | n_features | | ||
| | fkey | Column number that will be considered to form next node | 1 | | ||
| | fval | Double splitting value | 1 | | ||
| | left | Left node of decision tree | 1 | | ||
| | right | Right node of decision tree | 1 | | ||
|
|
||
| ## Methods | ||
|
|
||
| | Name | Definition | Return value | | ||
| | ------------------------------- | ----------------------------------------------------- | ----------------- | | ||
| | `entropy(vector<double>)` | Compute entropy | double | | ||
| | `divideData(vector<vector<double>>,int,int)` | Divide the data after finding best Info gain | `vector<vector<vector<double>>>`| | ||
| | `infoGain(vector<vector<double>>,int,int)` | Computes info gain | double | | ||
| | `train(vector<vector<double>>)` | Train the model on training values | void | | ||
| | `predict(vector<double>)` | Predict the output for testing value | int | | ||
|
|
||
| ## Example | ||
|
|
||
| ``` | ||
| std::vector<std::vector<double>> x_data{{0,23.76,3,76.56,1},{1,12.76,2,87.45,0},{1,21.86,1,79.98,1},{0,32.64,1,76.87,1},{0,22.76,3,89.90,0},{1,28.64,0,73.87,1},{0,12.87,3,82.86,0}}; | ||
| DecisionTree<double> dt = new DecisionTree(9,0,4); | ||
| dt->train(x_data); | ||
| std::vector<double> test{1,38.19,2,81.65}; | ||
| std::cout<<dt->predict(test); | ||
|
|
||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| // #include "../../src/slowmokit/methods/neighbors/decision_tree.hpp" | ||
| // #include "../../src/slowmokit/core.hpp" | ||
|
|
||
| // signed main(){ | ||
| // std::vector<std::vector<double>> x_data{ | ||
| // {0,23.76,3,76.56,1}, | ||
| // {1,12.76,2,87.45,0}, | ||
| // {1,21.86,1,79.98,1}, | ||
| // {0,32.64,1,76.87,1}, | ||
| // {0,22.76,3,89.90,0}, | ||
| // {1,28.64,0,73.87,1}, | ||
| // {0,12.87,3,82.86,0}, | ||
| // {0,33.87,2,80.97,1}, | ||
| // {1,39.64,1,70.87,1}, | ||
| // {0,28.90,2,89.86,1}, | ||
| // {0,13.76,3,72.56,0}, | ||
| // {1,19.76,2,88.45,1}, | ||
| // {0,16.86,1,78.98,0}, | ||
| // {0,32.44,1,73.87,1}, | ||
| // {1,22.76,3,80.93,1}, | ||
| // {0,28.64,0,78.87,0}, | ||
| // {1,8.87,2,81.96,0}, | ||
| // {0,31.87,2,75.97,0}, | ||
| // {1,27.64,1,71.89,1}, | ||
| // {0,20.90,2,80.86,0}, | ||
| // {0,23.76,3,76.56,1}, | ||
| // {1,12.76,2,87.45,1}, | ||
| // {1,21.86,1,79.98,1}, | ||
| // {0,32.64,1,76.87,1}, | ||
| // {0,22.76,3,89.90,0}, | ||
| // {1,28.64,0,73.87,1}, | ||
| // {0,12.87,3,82.86,0}, | ||
| // {0,33.87,2,80.97,1}, | ||
| // {1,39.64,1,70.87,1}, | ||
| // {0,28.90,2,89.86,0}, | ||
| // {0,13.76,3,72.56,0}, | ||
| // {1,19.76,2,88.45,1}, | ||
| // {0,16.86,1,78.98,0}, | ||
| // {0,32.44,1,73.87,1}, | ||
| // {1,22.76,3,80.93,1}, | ||
| // {0,28.64,0,78.87,0}, | ||
| // {1,8.87,2,81.96,1}, | ||
| // {0,31.87,2,75.97,0}, | ||
| // {1,27.64,1,71.89,0}, | ||
| // {0,20.90,2,80.86,1}, | ||
| // {0,32.64,1,76.87,1}, | ||
| // {0,22.76,3,89.90,0}, | ||
| // {1,28.64,0,73.87,1}, | ||
| // {0,12.87,3,82.86,0}, | ||
| // {0,33.87,2,80.97,1}, | ||
| // {1,39.64,1,70.87,1}, | ||
| // {0,28.90,2,89.86,1}, | ||
| // {0,13.76,3,72.56,0}, | ||
| // {1,19.76,2,88.45,1}, | ||
| // {0,16.86,1,78.98,0}, | ||
| // {0,32.44,1,73.87,1}, | ||
| // {1,22.76,3,80.93,1}, | ||
| // {0,28.64,0,78.87,0}, | ||
| // {1,8.87,2,81.96,0}, | ||
| // {0,31.87,2,75.97,0}, | ||
| // {1,27.64,1,71.89,1}, | ||
| // {0,20.90,2,80.86,1}, | ||
| // {0,23.76,3,76.56,1}, | ||
| // {1,12.76,2,87.45,0}, | ||
| // {1,21.86,1,79.98,1}, | ||
| // {0,32.64,1,76.87,1}, | ||
| // {0,22.76,3,89.90,0}, | ||
| // {1,28.64,0,73.87,1}, | ||
| // {0,12.87,3,82.86,0}, | ||
| // {0,33.87,2,80.97,0}, | ||
| // {1,39.64,1,70.87,0}, | ||
| // {0,28.90,2,89.86,1}, | ||
| // {0,13.76,3,72.56,1}, | ||
| // {1,19.76,2,88.45,0}, | ||
| // {0,16.86,1,78.98,1}, | ||
| // {0,32.44,1,73.87,1}, | ||
| // {1,22.76,3,80.93,0}, | ||
| // {0,28.64,0,78.87,0}, | ||
| // {1,8.87,2,81.96,1}, | ||
| // {0,31.87,2,75.97,0}, | ||
| // {1,27.64,1,71.89,1}, | ||
| // {0,20.90,2,80.86,0}, | ||
| // }; | ||
| // DecisionTree<double> dt = new DecisionTree(9,0,4); | ||
| // dt->train(x_data); | ||
| // std::vector<double> test{1,38.19,2,81.65}; | ||
| // std::cout<<dt->predict(test); | ||
| // return 0; | ||
| // } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| /** | ||
| * @file ducks/matrix/matrix.cpp | ||
| * | ||
| * Implementation of the matrix main program | ||
| */ | ||
|
|
||
| #include "matrix.hpp" | ||
|
|
||
| template<class T> Matrix<T>::Matrix(int n, int m) : n(n), m(m) | ||
| { | ||
| if (n <= 0 or m <= 0) | ||
| throw std::out_of_range("\nCannot have non-positive dimension."); | ||
|
|
||
| mat.resize(n, std::vector<T>(m, T(0))); | ||
| } | ||
|
|
||
| template<class T> Matrix<T>::Matrix(const std::vector<std::vector<T>> in) | ||
| { | ||
| if (std::size(in) <= 0 or std::size(in[0]) <= 0) | ||
| throw std::out_of_range("\nCannot have non-positive dimension."); | ||
|
|
||
| n = std::size(in); | ||
| m = std::size(in[0]); | ||
| mat.resize(n, std::vector<T>(m)); | ||
|
|
||
| for (int i = 0; i < n; i++) | ||
| { | ||
| if (std::size(in[i]) != m) | ||
| throw std::invalid_argument("\nAll rows must have same dimension"); | ||
|
|
||
| for (int j = 0; j < m; j++) | ||
| this->mat[i][j] = in[i][j]; | ||
| } | ||
| } | ||
|
|
||
| template<class T> Matrix<T> &Matrix<T>::operator*=(const T &scalar) | ||
| { | ||
| for (int i = 0; i < n; i++) | ||
| { | ||
| for (int j = 0; j < m; j++) | ||
| mat[i][j] *= scalar; | ||
| } | ||
|
|
||
| return *this; | ||
| } | ||
|
|
||
| template<class T> Matrix<T> &Matrix<T>::operator*=(const Matrix<T> &rhs) | ||
| { | ||
| auto [n2, m2] = rhs.getShape(); | ||
|
|
||
| if (n2 <= 0 or m2 <= 0) | ||
| throw std::out_of_range("\nCannot have non-positive dimension."); | ||
|
|
||
| if (m != n2) | ||
| throw std::invalid_argument("\nColumn dimension of matrix-1 must be equal " | ||
| "to row dimension of matrix-2"); | ||
|
|
||
| auto lhs = this->mat; | ||
| std::vector res(n, std::vector<T>(m2, T(0))); | ||
|
|
||
| for (int i = 0; i < n; i++) | ||
| { | ||
| for (int j = 0; j < m2; j++) | ||
| { | ||
| for (int k = 0; k < m; k++) | ||
| res[i][j] += lhs[i][k] * rhs[k][j]; | ||
| } | ||
| } | ||
|
|
||
| this->mat = res; | ||
| updateShape(); | ||
|
|
||
| return *this; | ||
| } | ||
|
|
||
| template<class T> Matrix<T> &Matrix<T>::operator+=(const Matrix<T> &rhs) | ||
| { | ||
| auto [n2, m2] = rhs.getShape(); | ||
|
|
||
| if (n2 <= 0 or m2 <= 0) | ||
| throw std::out_of_range("\nCannot have non-positive dimension."); | ||
|
|
||
| if (n != n2 or m != m2) | ||
| throw std::invalid_argument( | ||
| "\nBoth Dimension of matrix-1 must be equal to that of matrix-2"); | ||
|
|
||
| for (int i = 0; i < n; i++) | ||
| { | ||
| for (int j = 0; j < m; j++) | ||
| this->mat[i][j] += rhs[i][j]; | ||
| } | ||
|
|
||
| return *this; | ||
| } | ||
|
|
||
| template<class T> Matrix<T> &Matrix<T>::operator-=(const Matrix<T> &rhs) | ||
| { | ||
| auto [n2, m2] = rhs.getShape(); | ||
|
|
||
| if (n2 <= 0 or m2 <= 0) | ||
| throw std::out_of_range("\nCannot have non-positive dimension."); | ||
|
|
||
| if (n != n2 or m != m2) | ||
| throw std::invalid_argument( | ||
| "\nBoth Dimension of matrix-1 must be equal to that of matrix-2"); | ||
|
|
||
| for (int i = 0; i < n; i++) | ||
| { | ||
| for (int j = 0; j < m; j++) | ||
| this->mat[i][j] -= rhs[i][j]; | ||
| } | ||
|
|
||
| return *this; | ||
| } | ||
|
|
||
| template<class T> std::array<int, 2> Matrix<T>::getShape() const | ||
| { | ||
| return {this->n, this->m}; | ||
| } | ||
|
|
||
| template<class T> T &Matrix<T>::operator()(int i, int j) | ||
| { | ||
| if (i >= n or i < 0) | ||
| throw std::out_of_range("\ni should be between 0 and " + | ||
| std::to_string(n - 1) + " inclusive"); | ||
| if (j >= m or j < 0) | ||
| throw std::out_of_range("\nj should be between 0 and " + | ||
| std::to_string(m - 1) + " inclusive"); | ||
|
|
||
| return mat[i][j]; | ||
| } | ||
|
|
||
| template<class T> const std::vector<T> &Matrix<T>::operator[](int i) const | ||
| { | ||
| if (i >= n or i < 0) | ||
| throw std::out_of_range("\ni should be between 0 and " + | ||
| std::to_string(n - 1) + " inclusive"); | ||
|
|
||
| return this->mat[i]; | ||
| } | ||
|
|
||
| template<class T> | ||
| std::ostream &operator<<(std::ostream &os, const Matrix<T> &matrix) | ||
| { | ||
| int n = std::size(matrix); | ||
| int m = std::size(matrix[0]); | ||
|
|
||
| for (int i = 0; i < n; i++) | ||
| { | ||
| for (int j = 0; j < m; j++) | ||
| { | ||
| if (j > 0) | ||
| os << " "; | ||
| os << matrix[i][j]; | ||
| } | ||
|
|
||
| if (i != n - 1) | ||
| os << "\n"; | ||
| } | ||
|
|
||
| return os; | ||
| } | ||
|
|
||
| template<class T> Matrix<T> operator*(Matrix<T> lhs, const Matrix<T> &rhs) | ||
| { | ||
| lhs *= rhs; | ||
| return lhs; | ||
| } | ||
|
|
||
| template<class T> Matrix<T> operator+(Matrix<T> lhs, const Matrix<T> &rhs) | ||
| { | ||
| lhs += rhs; | ||
| return lhs; | ||
| } | ||
|
|
||
| template<class T> Matrix<T> operator-(Matrix<T> lhs, const Matrix<T> &rhs) | ||
| { | ||
| lhs -= rhs; | ||
| return lhs; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Vro all we need is this much, remove all the other files added
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done sir!