Advertisement
cesarsouza

Untitled

Aug 12th, 2011
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.11 KB | None | 0 0
  1.     // References: http://www.utdallas.edu/~herve/Abdi-PLSR2007-pretty.pdf
  2.  
  3.     // Following the small example by Hervé Abdi (Hervé Abdi, Partial Least Square Regression),
  4.     // we will create a simple example where the goal is to predict the subjective evaluation of
  5.     // a set of 5 wines. The dependent variables that we want to predict for each wine are its
  6.     // likeability, and how well it goes with meat, or dessert (as rated by a panel of experts).
  7.     // The predictors are the price, the sugar, alcohol, and acidity content of each wine.
  8.  
  9.  
  10.     // Here we will list the inputs, or characteristics we would like to use in order to infer
  11.     // information from our wines. Each row denotes a different wine and lists its corresponding
  12.     // observable characteristics. The inputs are usually denoted by X in the literature.
  13.  
  14.     double[,] inputs =
  15.     {
  16.     // Wine | Price | Sugar | Alcohol | Acidity
  17.             {   7,     7,      13,        7 },
  18.             {   4,     3,      14,        7 },
  19.             {  10,     5,      12,        5 },
  20.             {  16,     7,      11,        3 },
  21.             {  13,     3,      10,        3 },
  22.     };
  23.  
  24.  
  25.     // Here we will list our dependent variables. Dependent variables are the outputs, or what we
  26.     // would like to infer or predict from our available data, given a new observation. The outputs
  27.     // are usually denotes as Y in the literature.
  28.  
  29.     double[,] outputs =
  30.     {
  31.         // Wine | Hedonic | Goes with meat | Goes with dessert
  32.         {           14,          7,                 8 },
  33.         {           10,          7,                 6 },
  34.         {            8,          5,                 5 },
  35.         {            2,          4,                 7 },
  36.         {            6,          2,                 4 },
  37.     };
  38.  
  39.  
  40.     // Next, we will create our Partial Least Squares Analysis passing the inputs (values for
  41.     // predictor variables) and the associated outputs (values for dependent variables).
  42.            
  43.     // We will also be using the using the Covariance Matrix/Center method (data will only
  44.     // be mean centered but not normalized) and the NIPALS algorithm.
  45.     PartialLeastSquaresAnalysis pls = new PartialLeastSquaresAnalysis(inputs, outputs,
  46.         AnalysisMethod.Center, PartialLeastSquaresAlgorithm.NIPALS);
  47.  
  48.     // Compute the analysis with all factors. The number of factors
  49.     // could also have been specified in a overload of this method.
  50.  
  51.     pls.Compute();
  52.  
  53.     // After computing the analysis, we can create a linear regression model in order
  54.     // to predict new variables. To do that, we may call the CreateRegression() method.
  55.  
  56.     MultivariateLinearRegression regression = pls.CreateRegression();
  57.  
  58.     // After the regression has been created, we will be able to classify new instances.
  59.     // For example, we will compute the outputs for the first input sample:
  60.  
  61.     double[] y = regression.Compute(new double[] { 7, 7, 13, 7 });
  62.  
  63.     // The y output will be very close to the corresponding output used as reference.
  64.     // In this case, y is a vector of length 3 with values { 13.98, 7.00, 7.75 }.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement