You are on page 1of 3

Pregunta 4

clc
clearvars
load data2.mat
N=array2table(force);
N1=array2table(target);
T1= [N N1];
M1=arreglodenombre(2000,T1,101);
name=M1';
Name=array2table(name);
T1=[T1 Name];
clearvars -except T1 N1 Name

%Division de datos de 70%


n=2000;
hpartition = cvpartition(n,'Holdout',0.3);
idxTrain = training(hpartition);
tblTrain = T1(idxTrain,:);
idxNew = test(hpartition);
tblNew = T1(idxNew,:);
%division de datos 50%
n1=600;
hpartition1 = cvpartition(n1,'Holdout',0.5);
idxTrain1 = training(hpartition1);
tblTrain1 = tblNew(idxTrain1,:);
idxNew1= test(hpartition1);
tblNew1 = tblNew(idxNew1,:);

%modelo knn,svm,tree
Mdl1 = fitcecoc(tblTrain(:,1:100),tblTrain(:,101),'Learners',"knn","Coding","onevsall");
y1 = predict(Mdl1,tblTrain1);
Y1=table(y1);
a1= arreglodenombre(300,Y1,1);
prediccion1=a1';
L1=table(prediccion1);
cMetrics(tblTrain1.name,prediccion1)

Accuracy = 99.67%
ans = 5×5 table
Precision Recall Fallout Specificity F1

1 OK 0.9964 1.0000 0.0400 0.9600 0.9982


2 Crack 1.0000 0.8333 0 1.0000 0.9091
3 Overload 1.0000 1.0000 0 1.0000 1.0000
4 Avg 0.9988 0.9444 0.0133 0.9867 0.9691
5 WgtAvg 0.9967 0.9967 0.0367 0.9633 0.9965

1
%Analisis por PCA
X = T1{:,1:100};
mu = mean(X); % Row vector of column (feature) means
r = range(X); % Row vector of column ranges
X = (X-mu)./r ;% Scaled feature matrix
[coeff,score,latent,tsquared,explained] = pca(X);
S=array2table(score);
S=[S N1 Name];

i=1;
suma=0;
while suma <=95
suma=suma+explained(i);
i=i+1;
end
%se necesitan 8 componentes principales
i-1

ans = 8

%modelo pca con knn,svm,tree


Mdl2 = fitcecoc(S(:,1:i-1),S(:,101),'Learners',"tree","Coding","onevsall");
y2 = predict(Mdl2,S(:,1:8));
Y2=table(y2);
a2= arreglodenombre(300,Y2,1);
prediccion2=a2';
L2=table(prediccion2);
cMetrics(tblTrain1.name,prediccion2)

Accuracy = 91.67%
ans = 5×5 table
Precision Recall Fallout Specificity F1

1 OK 0.9167 1.0000 1.0000 0 0.9565


2 Crack NaN 0 0 1.0000 NaN
3 Overload NaN 0 0 1.0000 NaN
4 Avg NaN 0.3333 0.3333 0.6667 NaN
5 WgtAvg NaN 0.9167 0.9167 0.0833 NaN

%Es mejor el modelo sin pca

function N = arreglodenombre(n, T1,j)


i=1;
while i <= n

if table2array(T1(i,j))== 1
N(i)="OK";
elseif table2array(T1(i,j))== 2

2
N(i)="Overload";
else
N(i)="Crack";
end

i=i+1;

end
end

You might also like