mirror of
https://github.com/Relintai/MLPP.git
synced 2025-03-10 18:03:23 +01:00
Added median & mode
This commit is contained in:
parent
17884c6c15
commit
6cb5770a26
@ -6,6 +6,9 @@
|
||||
|
||||
#include "Stat.hpp"
|
||||
#include "Activation/Activation.hpp"
|
||||
#include "Data/Data.hpp"
|
||||
#include <algorithm>
|
||||
#include <map>
|
||||
#include <cmath>
|
||||
|
||||
namespace MLPP{
|
||||
@ -25,6 +28,44 @@ namespace MLPP{
|
||||
return sum / x.size();
|
||||
}
|
||||
|
||||
double Stat::median(std::vector<double> x){
|
||||
double center = double(x.size())/double(2);
|
||||
std::vector<double> original_vec = x;
|
||||
sort(x.begin(), x.end());
|
||||
if(x.size() % 2 == 0){
|
||||
return mean({x[center - 1], x[center]});
|
||||
}
|
||||
else{
|
||||
return x[center - 1 + 0.5];
|
||||
}
|
||||
x = original_vec;
|
||||
}
|
||||
|
||||
std::vector<double> Stat::mode(std::vector<double> x){
|
||||
Data data;
|
||||
std::vector<double> x_set = data.vecToSet(x);
|
||||
std::map<double, int> element_num;
|
||||
for(int i = 0; i < x_set.size(); i++){
|
||||
element_num[x[i]] = 0;
|
||||
}
|
||||
for(int i = 0; i < x.size(); i++){
|
||||
element_num[x[i]]++;
|
||||
}
|
||||
std::vector<double> modes;
|
||||
double max_num = element_num[x_set[0]];
|
||||
for(int i = 0; i < x_set.size(); i++){
|
||||
if(element_num[x_set[i]] > max_num){
|
||||
max_num = element_num[x_set[i]];
|
||||
modes.clear();
|
||||
modes.push_back(x_set[i]);
|
||||
}
|
||||
else if(element_num[x_set[i]] == max_num){
|
||||
modes.push_back(x_set[i]);
|
||||
}
|
||||
}
|
||||
return modes;
|
||||
}
|
||||
|
||||
double Stat::variance(std::vector<double> x){
|
||||
double sum = 0;
|
||||
for(int i = 0; i < x.size(); i++){
|
||||
|
@ -18,6 +18,8 @@ namespace MLPP{
|
||||
|
||||
// Statistical Functions
|
||||
double mean(std::vector <double> x);
|
||||
double median(std::vector<double> x);
|
||||
std::vector<double> mode(std::vector<double> x);
|
||||
double variance(std::vector <double> x);
|
||||
double covariance(std::vector <double> x, std::vector <double> y);
|
||||
double correlation(std::vector <double> x, std::vector<double> y);
|
||||
|
4
main.cpp
4
main.cpp
@ -76,11 +76,13 @@ int main() {
|
||||
// std::vector<double> outputSet = {0,1,1,0};
|
||||
|
||||
// // STATISTICS
|
||||
// std::vector<double> x = {1,2,3,4,5,6,7,8,9,10};
|
||||
// std::vector<double> x = {1,2,3,4,5,6,7,8,9,1};
|
||||
// std::vector<double> y = {10,9,8,7,6,5,4,3,2,1};
|
||||
// std::vector<double> w = {0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1};
|
||||
|
||||
// std::cout << "Arithmetic Mean: " << stat.mean(x) << std::endl;
|
||||
// std::cout << "Median: " << stat.median(x) << std::endl;
|
||||
// alg.printVector(stat.mode(x));
|
||||
// std::cout << "Variance: " << stat.variance(x) << std::endl;
|
||||
// std::cout << "Covariance: " << stat.covariance(x, y) << std::endl;
|
||||
// std::cout << "Correlation: " << stat.correlation(x, y) << std::endl;
|
||||
|
Loading…
Reference in New Issue
Block a user