Skip to content

Commit 4c79d13

Browse files
authored
Merge pull request #25 from jideoyelayo1/NN
2 parents 20068d3 + f1a5dde commit 4c79d13

File tree

12 files changed

+1002
-134
lines changed

12 files changed

+1002
-134
lines changed

CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ add_executable(HierarchicalClustering tests/clustering/HierarchicalClusteringTes
6969
target_compile_definitions(HierarchicalClustering PRIVATE TEST_HIERARCHICAL_CLUSTERING)
7070
target_link_libraries(HierarchicalClustering cpp_ml_library)
7171

72+
add_executable(SupportVectorRegression tests/regression/SupportVectorRegressionTest.cpp)
73+
target_compile_definitions(SupportVectorRegression PRIVATE TEST_SUPPORT_VECTOR_REGRESSION)
74+
target_link_libraries(SupportVectorRegression cpp_ml_library)
75+
76+
add_executable(NeuralNetwork tests/neural_network/NeuralNetworkTest.cpp)
77+
target_compile_definitions(NeuralNetwork PRIVATE TEST_NEURAL_NETWORK)
78+
target_link_libraries(NeuralNetwork cpp_ml_library)
79+
7280
# Register individual tests
7381
add_test(NAME LogisticRegressionTest COMMAND LogisticRegressionTest)
7482
add_test(NAME PolynomialRegressionTest COMMAND PolynomialRegressionTest)
@@ -81,6 +89,8 @@ add_test(NAME KMeansClustering COMMAND KMeansClustering)
8189
add_test(NAME KNNClassifier COMMAND KNNClassifier)
8290
add_test(NAME KNNRegressor COMMAND KNNRegressor)
8391
add_test(NAME HierarchicalClustering COMMAND HierarchicalClustering)
92+
add_test(NAME SupportVectorRegression COMMAND SupportVectorRegression)
93+
add_test(NAME NeuralNetwork COMMAND NeuralNetwork)
8494

8595

8696
# Add example executables if BUILD_EXAMPLES is ON
@@ -116,6 +126,10 @@ if(BUILD_EXAMPLES)
116126
target_compile_definitions(${EXAMPLE_TARGET} PRIVATE TEST_KNN_REGRESSOR)
117127
elseif(EXAMPLE_NAME STREQUAL "HierarchicalClusteringExample")
118128
target_compile_definitions(${EXAMPLE_TARGET} PRIVATE TEST_HIERARCHICAL_CLUSTERING)
129+
elseif(EXAMPLE_NAME STREQUAL "SupportVectorRegressionExample")
130+
target_compile_definitions(${EXAMPLE_TARGET} PRIVATE TEST_SUPPORT_VECTOR_REGRESSION)
131+
elseif(EXAMPLE_NAME STREQUAL "NeuralNetworkExample")
132+
target_compile_definitions(${EXAMPLE_TARGET} PRIVATE TEST_NEURAL_NETWORK)
119133
endif()
120134
endforeach()
121135
endif()

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,20 @@ The following machine learning algorithms are planned, inspired by concepts and
6363
- [x] Logistic Regression
6464
- [x] Decision Tree Regression
6565
- [x] Random Forest Regression
66-
- [ ] K-Nearest Neighbors
66+
- [x] K-Nearest Neighbors
6767

6868

6969
2. **Classification**
7070
- [x] Decision Tree Classifier
7171
- [x] Random Forest Classifier
72-
- [ ] K-Nearest Neighbors
72+
- [x] K-Nearest Neighbors
7373

7474
3. **Clustering**
75-
- [ ] K-Means Clustering
76-
- [ ] Hierarchical clustering
75+
- [x] K-Means Clustering
76+
- [x] Hierarchical clustering
7777

7878
4. **Neural Networks**
79-
- [ ] Neural Network (NN)
79+
- [x] Neural Network (NN)
8080
- [ ] Artificial Neural Network (ANN)
8181
- [ ] Convolutional Neural Network (CNN)
8282

@@ -100,7 +100,7 @@ The following machine learning algorithms are planned, inspired by concepts and
100100
| | Random Forest Classifier | [ ] | [ ] | [ ] |
101101
| | K-Nearest Neighbors | [ ] | [ ] | [ ] |
102102
| **Clustering** | K-Means Clustering | [ ] | [ ] | [ ] |
103-
| **Neural Networks** | Neural Network (NN) | [x] | [ ] | [ ] |
103+
| **Neural Networks** | Neural Network (NN) | [x] | [x] | [x] |
104104
| | Artificial Neural Network | [ ] | [ ] | [ ] |
105105
| | Convolutional Neural Network | [ ] | [ ] | [ ] |
106106
| **Association Rule Learning** | Apriori | [ ] | [ ] | [ ] |

examples/NeuralNetworkExample.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "../ml_library_include/ml/neural_network/NeuralNetwork.hpp"
2+
#include <iostream>
3+
#include <vector>
4+
#include <cstdlib>
5+
#include <cmath>
6+
7+
/**
8+
* @brief Utility function to display vector values.
9+
* @param label A label for the output.
10+
* @param v The vector to display.
11+
*/
12+
void showVectorVals(const std::string& label, const std::vector<double>& v) {
13+
std::cout << label << " ";
14+
for (double val : v) {
15+
std::cout << val << " ";
16+
}
17+
std::cout << std::endl;
18+
}
19+
20+
void testNeuralNetwork() {
21+
// Set up the topology: 3 layers with 2, 4, and 1 neurons respectively
22+
std::vector<unsigned> topology = {2, 4, 1};
23+
NeuralNetwork myNet(topology);
24+
25+
// Sample input and target output
26+
std::vector<double> inputVals = {1.0, 0.0};
27+
std::vector<double> targetVals = {1.0};
28+
std::vector<double> resultVals;
29+
30+
// Train the network with multiple iterations
31+
for (int i = 0; i < 1000; ++i) {
32+
myNet.feedForward(inputVals);
33+
myNet.backProp(targetVals);
34+
}
35+
36+
// Get the results after training
37+
myNet.feedForward(inputVals);
38+
myNet.getResults(resultVals);
39+
40+
showVectorVals("Inputs:", inputVals);
41+
showVectorVals("Outputs:", resultVals);
42+
}
43+
44+
int main() {
45+
testNeuralNetwork();
46+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "../ml_library_include/ml/regression/SupportVectorRegression.hpp"
2+
#include <iostream>
3+
4+
int testSupportVectorRegression() {
5+
// Training data
6+
std::vector<std::vector<double>> X_train = {
7+
{1.0},
8+
{2.0},
9+
{3.0},
10+
{4.0},
11+
{5.0}
12+
};
13+
std::vector<double> y_train = {1.5, 2.0, 2.5, 3.0, 3.5};
14+
15+
// Test data
16+
std::vector<std::vector<double>> X_test = {
17+
{1.5},
18+
{2.5},
19+
{3.5}
20+
};
21+
22+
// Create and train the model
23+
SupportVectorRegression svr(1.0, 0.1, SupportVectorRegression::KernelType::RBF, 3, 0.1);
24+
svr.fit(X_train, y_train);
25+
26+
// Make predictions
27+
std::vector<double> predictions = svr.predict(X_test);
28+
29+
// Output predictions
30+
for (size_t i = 0; i < predictions.size(); ++i) {
31+
std::cout << "Sample " << i << " predicted value: " << predictions[i] << std::endl;
32+
}
33+
34+
return 0;
35+
}
36+
37+
int main(){
38+
testSupportVectorRegression();
39+
}

0 commit comments

Comments
 (0)