機器學習及其matlab實現—從基礎到實踐——HW3

來源:互聯網
上載者:User

標籤:

Contents
  • I. 清空環境變數
  • II. 訓練集/測試集產生
  • III. 資料歸一化
  • IV. BP神經網路建立、訓練及模擬測試
  • V. 效能評價
  • VI. 畫圖
I. 清空環境變數
clear allclc
II. 訓練集/測試集產生

1. 匯入資料

load concrete_data.mat

2. 隨機產生訓練集和測試集

temp = randperm(size(attributes,2));% 訓練集——80個樣本P_train = attributes(:,temp(1:80));T_train = strength(:,temp(1:80));% 測試集——23個樣本P_test = attributes(:,temp(81:end));T_test = strength(:,temp(81:end));N = size(P_test,2);
III. 資料歸一化
[p_train, ps_input] = mapminmax(P_train,0,1);p_test = mapminmax(‘apply‘,P_test,ps_input);[t_train,ps_output] = mapminmax(T_train,0,1);
IV. BP神經網路建立、訓練及模擬測試

1. 建立網路

net = newff(p_train,t_train,11);

2. 設定訓練參數

net.trainParam.epochs = 1000;net.trainParam.goal = 1e-3;net.trainParam.lr = 0.01;

3. 訓練網路

net = train(net,p_train,t_train);

4. 模擬測試

t_sim = sim(net,p_test);

5. 資料反歸一化

T_sim = mapminmax(‘reverse‘,t_sim,ps_output);
V. 效能評價

1. 絕對誤差error

error = abs(T_sim - T_test)./T_test;

2. 決定係數R^2

R2 = (N * sum(T_sim .* T_test) - sum(T_sim) * sum(T_test))^2 / ((N * sum((T_sim).^2) - (sum(T_sim))^2) * (N * sum((T_test).^2) - (sum(T_test))^2));

3. 結果對比

result = [T_test‘ T_sim‘ error‘]
result =   28.1600   31.1291    0.1054   52.6500   53.0643    0.0079   30.9700   26.2469    0.1525   38.4600   38.0986    0.0094   41.1400   39.8364    0.0317   30.8300   30.8207    0.0003   36.1900   35.7525    0.0121   32.7100   33.8095    0.0336   41.0100   40.0879    0.0225   32.8400   33.3301    0.0149   33.9100   31.6726    0.0660   38.1900   38.3960    0.0054   26.4200   27.5678    0.0434   17.1900   18.4602    0.0739   35.5200   35.2201    0.0084   49.9700   51.0821    0.0223   48.7700   48.8715    0.0021   46.3600   46.2920    0.0015   31.5000   29.0295    0.0784   42.0800   42.0742    0.0001   36.4600   39.2970    0.0778   44.4800   44.6282    0.0033   58.5300   53.3348    0.0888
VI. 畫圖
figureplot(1:N,T_test,‘b:*‘,1:N,T_sim,‘r-o‘)legend(‘真實值‘,‘預測值‘)xlabel(‘預測樣本‘)ylabel(‘混凝土抗壓強度‘)string = {‘測試集混凝土抗壓強度預測結果對比‘;[‘R^2=‘ num2str(R2)]};title(string)


Published with MATLAB? R2015a

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

機器學習及其matlab實現—從基礎到實踐——HW3

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.