📄 gasdemo.html
字号:
<!--This HTML is auto-generated from an m-file.Your changes will be overwritten.--><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="color:#990000; font-weight:bold; font-size:x-large">Car mileage prediction with ANFIS</p><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">This slide show addresses the use of ANFIS function in the Fuzzy LogicToolbox for predicting the MPG (miles per gallon) of a given automobile.</p><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">Copyright 1994-2002 The MathWorks, Inc.$Revision: 1.9 $</p><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="color:#990000; font-weight:bold; font-size:medium; page-break-before: auto;"><a name=""></a></p><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">Automobile MPG (miles per gallon) prediction is a typical nonlinearregression problem, in which several attributes of an automobile'sprofile information are used to predict another continuous attribute,that is, the fuel consumption in MPG. The training data is available fromthe UCI (Univ. of California at Irvine) Machine Learning Repository(<a href="http://www.ics.uci.edu/~mlearn/MLRepository.html).">http://www.ics.uci.edu/~mlearn/MLRepository.html).</a> It contains datacollected from automobiles of various manufactures and models, as shownin the next slide.</p><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">The table shown above is several tuples from the MPG data set. The sixinput attributes are no. of cylinders, displacement, horsepower, weight,acceleration, and model year; the output variable to be predicted is thefuel consumption in MPG. (The automobile's manufacturers and models inthe first column of the table are not used for prediction. The data setis obtained from the original data file 'auto-gas.dat'. Then we partitionthe data set into a training set (odd-indexed tuples) and a checking set(even-indexed tuples), and use the function 'exhsrch' to find the inputattributes that have better prediction power for ANFIS modeling.</p><pre xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="position: relative; left:30px">a=imread(<span style="color:#B20000">'gasdata.jpg'</span>, <span style="color:#B20000">'jpg'</span>);image(a); colormap(gray); axis image;axis off[data, input_name] = loadgas;trn_data = data(1:2:end, :);chk_data = data(2:2:end, :);</pre><img xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" src="gasdemo_img02.gif"><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="color:#990000; font-weight:bold; font-size:medium; page-break-before: auto;"><a name=""></a></p><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">To select the best input attribute, 'exhsrch' constructs six ANFIS, eachwith a single input attribute. Here the resultafter executing exhsrch(1, trn_data, chk_data, input_name). Obviously,'Weight' is the most influential input attribute and 'Disp' is the secondone, etc. The training and checking errors are comparable in size, whichimplies that there is no overfitting and we can select more inputvariables. Intuitively, we can simply select 'Weight' and 'Disp'directly. However, this will not necessarily lead to a two-ANFIS modelwith the minimal training error. To verify this, we can issue thecommand exhsrch(2, trn_data, chk_data, input_name) to select the best twoinputs from all possible combinations.</p><pre xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="position: relative; left:30px">exhsrch(1, trn_data, chk_data, input_name);win1 = gcf;</pre><pre xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="color:gray; font-style:italic;">Train 6 ANFIS models, each with 1 inputs selected from 6 candidates...ANFIS model 1: Cylinder --> trn=4.6400, chk=4.7255ANFIS model 2: Disp --> trn=4.3106, chk=4.4316ANFIS model 3: Power --> trn=4.5399, chk=4.1713ANFIS model 4: Weight --> trn=4.2577, chk=4.0863ANFIS model 5: Acceler --> trn=6.9789, chk=6.9317ANFIS model 6: Year --> trn=6.2255, chk=6.1693</pre><img xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" src="gasdemo_img03.gif"><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="color:#990000; font-weight:bold; font-size:medium; page-break-before: auto;"><a name=""></a></p><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">Demonstrate the result of selecting two inputs. 'Weight' and 'Year' areselected as the best two input variables, which is quite reasonable. Thetraining and checking errors are getting distinguished, indicating theoutset of overfitting. As a comparison, let us use exhsrch to selectthree inputs</p><pre xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="position: relative; left:30px">input_index = exhsrch(2, trn_data, chk_data, input_name);new_trn_data = trn_data(:, [input_index, size(trn_data,2)]);new_chk_data = chk_data(:, [input_index, size(chk_data,2)]);win2 = gcf;</pre><pre xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="color:gray; font-style:italic;">Train 15 ANFIS models, each with 2 inputs selected from 6 candidates...ANFIS model 1: Cylinder Disp --> trn=3.9320, chk=4.7920ANFIS model 2: Cylinder Power --> trn=3.7364, chk=4.8683ANFIS model 3: Cylinder Weight --> trn=3.8741, chk=4.6763ANFIS model 4: Cylinder Acceler --> trn=4.3287, chk=5.9625ANFIS model 5: Cylinder Year --> trn=3.7129, chk=4.5946ANFIS model 6: Disp Power --> trn=3.8087, chk=3.8594ANFIS model 7: Disp Weight --> trn=4.0271, chk=4.6350ANFIS model 8: Disp Acceler --> trn=4.0782, chk=4.4890ANFIS model 9: Disp Year --> trn=2.9565, chk=3.3905ANFIS model 10: Power Weight --> trn=3.9310, chk=4.2976ANFIS model 11: Power Acceler --> trn=4.2740, chk=3.8738ANFIS model 12: Power Year --> trn=3.3796, chk=3.3505ANFIS model 13: Weight Acceler --> trn=4.0875, chk=4.0095ANFIS model 14: Weight Year --> trn=2.7657, chk=2.9953ANFIS model 15: Acceler Year --> trn=5.6242, chk=5.6481</pre><img xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" src="gasdemo_img04.gif"><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="color:#990000; font-weight:bold; font-size:medium; page-break-before: auto;"><a name=""></a></p><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">The popped figure demonstrates the result of selecting three inputs, inwhich 'Weight', 'Year', and 'Acceler' are selected as the best threeinput variables. However, the minimal training (and checking) error donot reduce significantly from that of the best 2-input model, whichindicates that the newly added attribute 'Acceler' does not improve theprediction too much. For better generalization, we always prefer a modelwith a simple structure. Therefore we will stick to the two-input ANFISfor further exploration</p><pre xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="position: relative; left:30px">exhsrch(3, trn_data, chk_data, input_name);win3 = gcf;</pre><pre xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="color:gray; font-style:italic;">Train 20 ANFIS models, each with 3 inputs selected from 6 candidates...ANFIS model 1: Cylinder Disp Power --> trn=3.4446, chk=11.5329ANFIS model 2: Cylinder Disp Weight --> trn=3.6686, chk=4.8922ANFIS model 3: Cylinder Disp Acceler --> trn=3.6610, chk=5.2384ANFIS model 4: Cylinder Disp Year --> trn=2.5463, chk=4.9001ANFIS model 5: Cylinder Power Weight --> trn=3.4797, chk=9.3761ANFIS model 6: Cylinder Power Acceler --> trn=3.5432, chk=4.4804ANFIS model 7: Cylinder Power Year --> trn=2.6300, chk=3.6300ANFIS model 8: Cylinder Weight Acceler --> trn=3.5708, chk=4.8378ANFIS model 9: Cylinder Weight Year --> trn=2.4951, chk=4.0435ANFIS model 10: Cylinder Acceler Year --> trn=3.2698, chk=6.2616ANFIS model 11: Disp Power Weight --> trn=3.5879, chk=7.4948ANFIS model 12: Disp Power Acceler --> trn=3.5395, chk=3.9953ANFIS model 13: Disp Power Year --> trn=2.4607, chk=3.3563ANFIS model 14: Disp Weight Acceler --> trn=3.6075, chk=4.2318ANFIS model 15: Disp Weight Year --> trn=2.5617, chk=3.7865ANFIS model 16: Disp Acceler Year --> trn=2.4149, chk=3.2480ANFIS model 17: Power Weight Acceler --> trn=3.7884, chk=4.0480ANFIS model 18: Power Weight Year --> trn=2.4371, chk=3.2852ANFIS model 19: Power Acceler Year --> trn=2.7276, chk=3.2580ANFIS model 20: Weight Acceler Year --> trn=2.3603, chk=2.9152</pre><img xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" src="gasdemo_img05.gif"><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="color:#990000; font-weight:bold; font-size:medium; page-break-before: auto;"><a name=""></a></p><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">The input-output surface of the best two-input ANFIS model for MPGprediction is shown above. It is a nonlinear and monotonic surface, inwhich the predicted MPG increases with the increase in 'Weight' anddecrease in 'Year'. The training RMSE (root mean squared error) is 2.766;the checking RMSE is 2.995. In comparison, a simple linear regressionusing all input candidates results in a training RMSE of 3.452, and achecking RMSE of 3.444.</p><pre xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="position: relative; left:30px"><span style="color:blue">if</span> ishandle(win1), delete(win1); <span style="color:blue">end</span><span style="color:blue">if</span> ishandle(win2), delete(win2); <span style="color:blue">end</span><span style="color:blue">if</span> ishandle(win3), delete(win3); <span style="color:blue">end</span>in_fis=genfis1(new_trn_data);mf_n = 2;mf_type = <span style="color:#B20000">'gbellmf'</span>;epoch_n = 1;ss = 0.01;ss_dec_rate = 0.5;ss_inc_rate = 1.5;in_fismat = genfis1(new_trn_data, mf_n, mf_type);[trn_out_fismat trn_error step_size chk_out_fismat chk_error] = anfis(new_trn_data, in_fismat, [epoch_n nan ss ss_dec_rate ss_inc_rate], nan, new_chk_data, 1);<span style="color:blue">for</span> i=1:length(input_index), chk_out_fismat = setfis(chk_out_fismat, <span style="color:#B20000">'input'</span>, i, <span style="color:#B20000">'name'</span>, deblank(input_name(input_index(i), :)));<span style="color:blue">end</span>chk_out_fismat = setfis(chk_out_fismat, <span style="color:#B20000">'output'</span>, 1, <span style="color:#B20000">'name'</span>, deblank(input_name(size(input_name, 1), :)));gensurf(chk_out_fismat); colormap(<span style="color:#B20000">'default'</span>);set(gca, <span style="color:#B20000">'box'</span>, <span style="color:#B20000">'on'</span>);view(-22, 36);fprintf(<span style="color:#B20000">'\nLinear regression with parameters:\n'</span>);param= size(trn_data,2)A_trn = [trn_data(:, 1:size(data,2)-1) ones(size(trn_data,1), 1)];B_trn = trn_data(:, size(data,2));coef = A_trn\B_trn;trn_error = norm(A_trn*coef-B_trn)/sqrt(size(trn_data,1));A_chk = [chk_data(:, 1:size(data,2)-1) ones(size(chk_data,1), 1)];B_chk = chk_data(:, size(data,2));chk_error = norm(A_chk*coef-B_chk)/sqrt(size(chk_data,1));fprintf(<span style="color:#B20000">'\nRMSE for training data: '</span>);RMSE=trn_errorfprintf(<span style="color:#B20000">'\nRMSE for checking data: '</span>);RMSE = chk_error</pre><pre xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="color:gray; font-style:italic;">ANFIS info: Number of nodes: 21 Number of linear parameters: 12 Number of nonlinear parameters: 12 Total number of parameters: 24 Number of training data pairs: 196 Number of checking data pairs: 196 Number of fuzzy rules: 4Start training ANFIS ... 1 2.7657 2.99534Designated epoch number reached --> ANFIS training completed at epoch 1.Linear regression with parameters:param = 7RMSE for training data: RMSE = 3.4527
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -