代碼如下(包括下載數據和訓練網絡):
%%Download Housing Prices
filename = 'housing.txt';
%下載
urlwrite('http://archive.ics.uci.edu/ml/machine-learning-databases/housing/housing.data',filename);
%指定名字
inputNames = {'CRIM','ZN','INDUS','CHAS','NOX','RM','AGE','DIS','RAD','TAX','PTRATIO','B','LSTAT'};
outputNames = {'MEDV'};
housingAttributes = [inputNames,outputNames];
%%Import Data
%格式規範
formatSpec = '%8f%7f%8f%3f%8f%8f%7f%8f%4f%7f%7f%7f%7f%f%[^\n\r]';
fileID = fopen(filename,'r');
%讀取大文件,比起testread方便
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'ReturnOnError', false);
fclose(fileID);
%寫進表格
housing = table(dataArray{1:end-1}, 'VariableNames', {'VarName1','VarName2','VarName3','VarName4','VarName5','VarName6','VarName7','VarName8','VarName9',...
'VarName10','VarName11','VarName12','VarName13','VarName14'});
%Delete the file and clear temporary variables
clearvars filename formatSpec fileID dataArray ans;
%%delete housing.txt
%%Read into a Table
%重新定義變量名字
housing.Properties.VariableNames = housingAttributes;
%X特徵向量 Y房價
X = housing{:,inputNames};
Y = housing{:,outputNames};
%數據處理好了,開始訓練
features=X;prices=Y;len = length(prices);
index = randperm(len);%生成1~len 的隨機數
%%產生訓練集和數據集
%訓練集——前70%
p_train = features(index(1:round(len*0.7)),:);%訓練樣本輸入
t_train = prices(index(1:round(len*0.7)),:);%訓練樣本輸出
%測試集——後30%
p_test = features(index(round(len*0.7)+1:end),:);%測試樣本輸入
t_test = prices(index(round(len*0.7)+1:end),:);%測試樣本輸出
%%數據歸一化
%輸入樣本歸一化
[pn_train,ps1] = mapminmax(p_train');
pn_test = mapminmax('apply',p_test',ps1);
%輸出樣本歸一化
[tn_train,ps2] = mapminmax(t_train');
%tn_test = mapminmax('apply',t_test',ps2);
%%神經網絡
%創建和訓練
net = feedforwardnet(5,'trainlm');%創建網絡
net.trainParam.epochs = 5000;%設置訓練次數
net.trainParam.goal=0.0000001;%設置收斂誤差
[net,tr]=train(net,pn_train,tn_train);%訓練網絡
%網絡仿真,測試數據
b=sim(net,pn_test);%放入到網絡輸出數據
%%結果反歸一化,預測的價格
predict_prices = mapminmax('reverse',b,ps2);
%%結果分析
t_test = t_test';
err_prices = t_test-predict_prices;%誤差
[mean(err_prices) std(err_prices)]%求平均,標準差
figure(1);
plot(t_test);
hold on;
plot(predict_prices,'r');
xlim([1 length(t_test)]);
hold off;
legend({'Actual','Predicted'})
xlabel('Training Data point');
ylabel('Median house price');
結果: