/// \file /// \ingroup tutorial_math /// \notebook /// Principal Components Analysis (PCA) example /// /// Example of using TPrincipal as a stand alone class. /// /// We create n-dimensional data points, where c = trunc(n / 5) + 1 /// are correlated with the rest n - c randomly distributed variables. /// /// \macro_output /// \macro_code /// /// \authors Rene Brun, Christian Holm Christensen #include "TPrincipal.h" void principal(Int_t n=4, Int_t m=150) { cout << "*************************************************" << endl; cout << "* Principal Component Analysis *" << endl; cout << "* *" << endl; cout << "* Number of variables: " << setw(4) << n << " *" << endl; cout << "* Number of data points: " << setw(8) << m << " *" << endl; cout << "* Number of dependent variables: " << setw(4) << c << " *" << endl; cout << "* *" << endl; cout << "*************************************************" << endl; TPrincipal* principal = new TPrincipal(n,"ND"); // Make the m data-points // Make a variable to hold our data // Allocate memory for the data point Double_t* data = new Double_t[n]; ifstream inFile; inFile.open("path/to/iris.data"); if (!inFile) { cout << "\nError opening file.\n"; return 13; } for (Int_t i = 0; i < m; i++) // loop over file { // First we create the un-correlated, random variables, according // to one of three distributions for (Int_t j = 0; j < n ; j++) { double val; inFile >> val; data[j] = val; } TString label; inFile >> label; // Finally we're ready to add this datapoint to the PCA principal->AddRow(data); principal->GetRow(m); } // We delete the data after use, since TPrincipal got it by now. delete [] data; // Do the actual analysis principal->MakePrincipals(); // Print out the result on principal->Print(); // Test the PCA principal->Test(); // Make some histograms of the orginal, principal, residue, etc data principal->MakeHistograms("pca","XP"); // Make two functions to map between feature and pattern space principal->MakeCode(); const TVectorD *td = principal->GetUserData(); // Start a browser, so that we may browse the histograms generated // above TBrowser* b = new TBrowser("principalBrowser", principal); }