www.gusucode.com > stats 源码程序 matlab案例代码 > stats/RegressionTreeIllustrationExample.m

    %% Train a Regression Tree Algorithm
% This example shows how to build a predictive model using a regression
% tree.  The data set consists of measurement on 100 randomly sampled
% vehicles.  Suppose that you want to model a vehicle's fuel economy (in
% mpg) using its weight (in lbs) and number of cylinders.
%%
% Load the fuel economy data set.

% Copyright 2015 The MathWorks, Inc.

load carsmall
%%
% There are several missing fuel economies in the sample. Save the weights
% corresponding to them for prediction. Then, remove instances with missing
% fuel economies from the data for the analysis.
nanWeight = Weight(isnan(MPG)); % Predict their fuel economies
nanCylinders = Cylinders(isnan(MPG));

Weight     = Weight(~isnan(MPG));
Cylinders  = Cylinders(~isnan(MPG));
MPG        = MPG(~isnan(MPG));
%%
% Build the regression tree .
EstRegTree = fitrtree([Weight,Cylinders],MPG,...
    'PredictorNames',{'Wght','Cylndr'},...
    'MinParent',20);
%%
% |EstRegTree| is the resulting regression tree.
%%
% Plot the resulting regression tree.
view(EstRegTree,'mode','graph')
%%
% The regression tree displays in an interactive graphics window. Each node
% (represented by a triangle), represents the optimal split of the feature.
% The values at the bottom of the tree are predictions for cars that meet
% the weight and cylinder descriptions of the branches above.  For example,
% the algorithm predicts a fuel economy of 27.6389 mpg given that the
% vehicle weighs at least 2371 lbs, but less than 2827.5 lbs, and has less
% than 5 cylinders.
%%
% By default, the algorithm computes the response prediction by finding the
% average of the MPGs within the corresponding feature subintervals. Verify
% that the average fuel economy is 27.6389 mpg for vehicles that 
% weigh at least 2371 lbs, but less than 2827.5 lbs, and have less than 5
% cylinders.
MPGBar1 = mean(MPG(Weight < 2827.5 & Weight >= 2371 & Cylinders < 5))
%%
% Predict the missing fuel economies.
nanMPG = predict(EstRegTree,[nanWeight,nanCylinders]);
nanMerge = [nanWeight'; nanCylinders'; nanMPG'];
fprintf('\nWeight | Cylinders | MPG\n')
fprintf('--------------------------\n')
fprintf('%5.0f  | %4.0d      | %6.3f\n',nanMerge(:))
%%
% The table displays the regression tree predicted fuel economies
% corresponding to the weights and and cylinders.
%%
% An advantage to using regression trees is that you do not have to
% normalize your features.  However, you should consider partitioning the
% data into training and validation sets to assess the performance of the
% fitted regression tree.