Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
principal.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_math
3## \notebook
4## Principal Components Analysis (PCA) example.
5##
6## Example of using TPrincipal as a stand alone class.
7##
8## I create n-dimensional data points, where c = trunc(n / 5) + 1
9## are correlated with the rest n - c randomly distributed variables.
10##
11## Based on principal.C by Rene Brun and Christian Holm Christensen
12##
13## \macro_image
14## \macro_output
15## \macro_code
16##
17## \authors Juan Fernando, Jaramillo Botero
18
19from ROOT import TPrincipal, gRandom, TBrowser, vector
20
21
22n = 10
23m = 10000
24
25c = int(n / 5) + 1
26
27print ("""*************************************************
28* Principal Component Analysis *
29* *
30* Number of variables: {0:4d} *
31* Number of data points: {1:8d} *
32* Number of dependent variables: {2:4d} *
33* *
34*************************************************""".format(n, m, c))
35
36# Initilase the TPrincipal object. Use the empty string for the
37# final argument, if you don't wan't the covariance
38# matrix. Normalising the covariance matrix is a good idea if your
39# variables have different orders of magnitude.
40principal = TPrincipal(n, "ND")
41
42# Use a pseudo-random number generator
43randomNum = gRandom
44
45# Make the m data-points
46# Make a variable to hold our data
47# Allocate memory for the data point
48data = vector('double')()
49for i in range(m):
50 # First we create the un-correlated, random variables, according
51 # to one of three distributions
52 for j in range(n - c):
53 if j % 3 == 0:
55 elif j % 3 == 1:
57 else:
59
60 # Then we create the correlated variables
61 for j in range(c):
63 for k in range(n - c - j):
64 data[n - c + j] += data[k]
65
66 # Finally we're ready to add this datapoint to the PCA
69
70# Do the actual analysis
72
73# Print out the result on
75
76# Test the PCA
78
79# Make some histograms of the original, principal, residue, etc data
81
82# Make two functions to map between feature and pattern space
83# Start a browser, so that we may browse the histograms generated
84# above
86b = TBrowser("principalBrowser", principal)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t format
Using a TBrowser one can browse all ROOT objects.
Definition TBrowser.h:37
Principal Components Analysis (PCA)
Definition TPrincipal.h:21