#include #include #include #include #include #include #include #include #include #include #include #include #include #include void correlation(){ //Open the file f = new TFile("insurance.root"); t = (TTree*) f->Get("insurance"); //Variable Int_t age, sex, children, smoker, north, east; Float_t bmi, charges; //Bind variables t->SetBranchAddress("age", &age); t->SetBranchAddress("sex", &sex); t->SetBranchAddress("children", &children); t->SetBranchAddress("smoker", &smoker); t->SetBranchAddress("north", &north); t->SetBranchAddress("east", &east); t->SetBranchAddress("age", &age); t->SetBranchAddress("bmi", &bmi); t->SetBranchAddress("charges", &charges); values = xt::xarray::from_shape({8}); mat = xt::xarray::from_shape({(size_t)t->GetEntries(), 8}); for (Int_t i = 0; i < t->GetEntries(); i++) { t->GetEntry(i); //Append variables to matrix values[0] = age; values[1] = sex; values[2] = children; values[3] = smoker; values[4] = north; values[5] = east; values[6] = bmi; values[7] = charges; xt::view(mat, i) = values; } //Calculate the corrilation cor = xt::eval(xt::zeros({8,8})); float means[8]; float stds[8]; for(int i=0;i<8;i++) means[i] = xt::mean(xt::view(mat, xt::all(), i))[0]; for(int i=0;i<8;i++) stds[i] = xt::stddev(xt::eval(xt::view(mat, xt::all(), i)))[0]; for(int i=0;i<8;i++) { auto a = xt::view(mat, xt::all(), i); for(int j=0;j<8;j++) { auto b = xt::view(mat, xt::all(), j); xt::view(cor, i, j) = xt::mean((a-means[i])*(b-means[j])) /(stds[i]*stds[j]); } } cout << cor << endl; }