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 3 1 1 2
7 8 7 2 4 8 3 8 2 2
5 3 3 4 2 4 3 4 1 2
5 5 5 8 10 8 7 3 7 2
10 6 6 3 4 5 3 6 1 2
8 2 4 1 5 1 5 4 4 2
9 5 5 2 2 2 5 1 1 2
6 3 4 1 5 2 3 9 1 2
10 4 2 1 3 2 4 3 10 2
5 3 4 1 8 10 4 9 1 2
6 10 2 8 10 2 7 8 10 2
9 4 5 10 6 10 4 8 1 2
8 10 10 8 7 10 9 7 1 2
7 4 6 4 6 1 4 3 1 2
10 5 5 3 6 7 7 10 1 2
5 2 3 4 2 7 3 6 1 2
10 7 7 3 8 5 7 4 3 2
10 10 10 8 6 1 8 9 1 2
5 4 4 9 2 10 5 6 1 2
6 10 10 2 8 10 7 3 3 2
10 10 10 4 8 1 8 10 1 2
3 7 7 4 4 9 4 8 1 2
9 5 8 1 2 3 2 1 5 2
10 3 6 2 3 5 4 10 2 2
10 5 5 6 8 8 7 1 1 2
8 10 10 1 3 6 3 9 1 2
5 2 3 1 6 10 5 1 1 2
5 3 5 5 3 3 4 10 1 2
9 10 10 1 10 8 3 3 1 2
8 3 8 3 4 9 8 9 8 2
10 6 4 1 3 4 3 2 3 2
]%
%x=xlsread('d:/AData2.xls',1, 'B2:J21')%
a=randperm(78);
Train=data(a(1:60),:)
Test=data(a(61:end),:)
% 訓練數(shù)據(jù)
P_train=Train(:,1:end-1)'
Tc_train=Train(:,end)'
T_train=ind2vec(Tc_train);
% 測試數(shù)據(jù)
P_test=Test(:,1:end-1)';
Tc_test=Test(:,end)';
%% K-fold交叉驗證確定最佳神經(jīng)元個數(shù)
k_fold=5;
Indices=crossvalind('Kfold',size(P_train,2),k_fold);
error_min=10e10;
best_number=1;
best_input=[];
best_output=[];
best_train_set_index=[];
best_validation_set_index=[];
h=waitbar(0,'正在尋找最佳神經(jīng)元個數(shù).....');
for i=1:k_fold
% 驗證集標號
validation_set_index=(Indices==i);
% 訓練集標號
train_set_index=~validation_set_index;
% 驗證集
validation_set_input=P_train(:,validation_set_index);
validation_set_output=T_train(:,validation_set_index);
% 訓練集
train_set_input=P_train(:,train_set_index);
train_set_output=T_train(:,train_set_index);
for number=10:30
count_B_train=length(find(Tc_train(:,train_set_index)==1));
count_M_train=length(find(Tc_train(:,train_set_index)==2));
rate_B=count_B_train/length(find(train_set_index==1));
rate_M=count_M_train/length(find(train_set_index==1));
net=newlvq(minmax(train_set_input),number,[rate_B rate_M]);
% 設置網(wǎng)絡參數(shù)
net.trainParam.epochs=1000;
net.trainParam.show=10;
net.trainParam.lr=0.1;
net.trainParam.goal=0.1;
% 訓練網(wǎng)絡
net=train(net,train_set_input,train_set_output);
waitbar(((i-1)*21+number)/114,h);
%% 仿真測試
T_sim=sim(net,validation_set_input);
Tc_sim=vec2ind(T_sim);
error=length(find(Tc_sim~=Tc_train(:,validation_set_index)));
if error<error_min
error_min=error;
best_number=number;
best_input=train_set_input;
best_output=train_set_output;
best_train_set_index=train_set_index;
best_validation_set_index=validation_set_index;
end
end
end
disp(['經(jīng)過交叉驗證,得到的最佳神經(jīng)元個數(shù)為:' num2str(best_number)]);
close(h);
%% 創(chuàng)建網(wǎng)絡
count_B=length(find(Tc_train==1));
count_M=length(find(Tc_train==2));
rate_B=count_B/60;
rate_M=count_M/60;
net=newlvq(minmax(P_train),10,[rate_B rate_M],0.01,'learnlv1');
% 設置網(wǎng)絡參數(shù)
net.trainParam.epochs=1000;
net.trainParam.show=10;
net.trainParam.lr=0.1;
net.trainParam.goal=0.1;
%% 訓練網(wǎng)絡
net=train(net,P_train,T_train);
%% 仿真測試
T_sim=sim(net,P_test);
Tc_sim=vec2ind(T_sim);
result=[Tc_sim;Tc_test]
% %% ROC
% net2 = newpr(P_train,P_test,20);
% net2 = train(net2,P_train,P_test);
% irisOutputs = sim(net2,P_train);
% [tpr,fpr,thresholds] = roc(P_test,irisOutputs)
%% 結(jié)果顯示
total_B=length(find(data(:,end)==1));
total_M=length(find(data(:,end)==2));
number_B=length(find(Tc_test==1));
number_M=length(find(Tc_test==2));
number_B_sim=length(find(Tc_sim==1 & Tc_test==1));
number_M_sim=length(find(Tc_sim==2 &Tc_test==2));
disp(['病例總數(shù):' num2str(78)...
' 良性:' num2str(total_B)...
' 惡性:' num2str(total_M)]);
disp(['訓練集病例總數(shù):' num2str(60)...
' 良性:' num2str(count_B)...
' 惡性:' num2str(count_M)]);
disp(['測試集病例總數(shù):' num2str(18)...
' 良性:' num2str(number_B)...
' 惡性:' num2str(number_M)]);
disp(['良性乳腺癌確診:' num2str(number_B_sim)...
' 誤診:' num2str(number_B-number_B_sim)...
' 確診率p1=' num2str(number_B_sim/number_B*100) '%']);
disp(['惡性乳腺癌確診:' num2str(number_M_sim)...
' 誤診:' num2str(number_M-number_M_sim)...
' 確診率p2=' num2str(number_M_sim/number_M*100) '%']);