Professional Documents
Culture Documents
N (^x ; ^ μ, ∑) = ∑∨ e ∑=covariance: %Load the hogg dataset
N (^x ; ^ μ, ∑) = ∑∨ e ∑=covariance: %Load the hogg dataset
hogg
diag(var(hogg))
%For hogg, covariance is diag variance
1
N ( ^x ; ^
μ , ∑ )= −1 T
( ^x − μ^ ) ( x−
^ ^μ )
d 2
(2 π )
∑=covariance ¿
√ 2
√ ¿ ∑∨¿ e ∑
¿
load fisheriris
load fisheriris
data = [meas(:,1) meas(:,3)];
m1 = mean(data(101:150,:))
c1 = cov(data(101:150,:))
c1 = eye*diag(c1)'
[X1,X2] = meshgrid(linspace(0,8,50)', linspace(0,8,50)');
X = [X1(:) X2(:)];
p = mvnpdf(X,m1,c1);
figure
surf(X1,X2,reshape(p,50,50));
m2 = mean(data(51:100,:))
c2 = cov(data(51:100,:))
c2 = eye*diag(c2)'
hold on
p2 = mvnpdf(X,m2,c2);
surf(X1,X2,reshape(p2,50,50));
testx = [2.5 5]
a = mvnpdf(testx,m1,c1);
b = mvnpdf(testx,m2,c2)
a/(a+b)
b/(a+b)
k
p ( x ) =∑ p(x∨Gi )P(Gi )
i−1
=G i
x 1 + x 2+ …+ x n
ub =
nb
2
σ b=¿ ¿ ¿
z ti
clear all
MU1 = [1 2]; %Initialise MEAN1 at [x1 = 1, x2 = 2]
SIGMA1 = [2 0; 0 .5]; %Initialise covariance matrix 1
MU2 = [-3 -5]; %Initialise MEAN2 at [x1 = -3, x2 = -5]
SIGMA2 = [1 0; 0 1]; %Initalise covariance matrix 2
rng(1); %Seed the rng
X = [mvnrnd(MU1,SIGMA1,1000);
mvnrnd(MU2,SIGMA2,1000)]; %Create an array of MVGaussians
figure;
hold on
scatter(X(:,1),X(:,2),10,'.') %Plot data of multivariate Gaussians
c = 1;
%Iterate over all possible combinations of
%full/diagonal and shared/unique
for i = 1:nSigma
for j = 1:nSC
%Fit gaussian model to the data, with covariance options
gmfit = fitgmdist(X,k,'CovarianceType',Sigma{i},...
'SharedCovariance',SharedCovariance{j},'Options',options);
plot(gmfit.mu(:,1),gmfit.mu(:,2),'kx','LineWidth',2,'MarkerSize',10)
%Add legend, title and move to next combination
title(sprintf('Sigma is %s, SharedCovariance = %s',...
Sigma{i},SCtext{j}),'FontSize',8)
legend(h1,{'1','2','3'});
hold off
c = c + 1;
end
end
N
1 x −x
f kernel ( x )= ∑
Nb i =1 ( )
K i
b
√¿ ¿
| A x −B x|+ ¿ A y −B y ∨¿
% Dendrograms for the Iris dataset
load fisheriris
%Use average linkage
tree = linkage(meas,'average');
dendrogram(tree,0)
%Use single linkage
tree = linkage(meas,'single');
dendrogram(tree,0)
%Use complete linkage
tree = linkage(meas,'complete');
dendrogram(tree,0)
z=wT x
λ 1+ λ2+ …+ λ k
λ1 + λ2 +…+ λk +… λ d
E ( F ∪ x i ) < E(F )
j=arg ¿
2 T T T 2
( m1−m2 ) =( w m1 −w m2 ) =w S B w
S B=( m 1−m 2 ) ( m 1−m 2 )T
2
s21=∑ ( w T xt −m 1 ) r t =wT S1 w
t
T
Sn=∑ ( x t−m n)( x t−m 1 ) r t
t
T 2
J ( w )=
|w ( m1−m2 )|
wT Sw w
w=c S−1
w (m 1−m 2 )
figure()
plot(xtrain, ytrain, '*') %Plot training data
%Get the posteriors for the validation data using Bayes rule
postPosVal = (posPostNumVal)./(posPostNumVal+negPostNumVal);
postNegVal = (negPostNumVal)./(posPostNumVal+negPostNumVal);
%Plot posteriors
figure();
hold on;
plot(x, c1Post);
plot(x, c2Post);
clear all; close all; clc;
rawRead = readtable('pima_indians_diabetes.csv');
rawRead(1,:) = [];
rawData = table2array(rawRead(:,1:8)); %Read raw features
rawLabels = string(table2array(rawRead(:,9))); %Read raw labels
pdF = makedist('Normal', 'mu', mf, 'sigma', sf); %Create female normal dist
pdfF = pdf(pdF, TESTPOINT); %Get probability @ 210
plot(x,pdf(pdF,x)); %Plot female pdf
hold on
pdM = makedist('Normal', 'mu', mm, 'sigma', sm); %Create male normal dist
pdfM = pdf(pdM, 210); %Get prbability @ 210
plot(x,pdf(pdM,x)); %Plot male pdf
b 1 x 1+ b2 x2 +…+ bn xn
μb =
b 1+ b2+ …+b n
2
b1 ( x1 −μ 1 )2+ …+b n ( x n−μ n) 2
σ b=
b1 +b 2+ … bn
bn
N
1 x−x t
p ( x) = ∑
Nh t =1
K
h ( )
1
K ( u )=
{ 1 ,∧¿ x∨¿
2
0 ,∧otherwise
¿( x∈bin)
p ( x t )=
Nh
1
8
( 3× 3 ×3 ×1 ×1 ×2 ×2 ×1)=6 .44 ×10−6
( 1× 8 )
y=Step
(∑ x w )
i
i i
display(neuron(3));
w i=w i+ ∆ w i
∆ wi=η ( y i −t i ) x i
−η ( d E2k )
∆ wi = =2 η E k x i=2 η ( t k −w o−w1 x 1−w2 x2 ) x i
d wi
K ( x i , x j ) =φ ( x i )T φ( x j)
ϕ : x → φ (x)
y= ( a× b+r )n
y=e−γ ( a−b)
load fisheriris
P ( W )=∑ P ( R , W )=P ( W |R ) P ( R ) + P ( W | R ) P( R)
R
P ( W |R ) P ( R )
P ( R∨W )=
P(W )
P ( X ,Y ∨Z ) =P ( X∨Z ) P(Y ∨Z )
P ( X|Y , Z )=P( X∨Z)
P ( R|C ) P ( C ) P ( R|C ) P (C )
P ( C|R ) = =
P (R ) P ( R|C ) P ( C ) + P ( R| C ) P ( C )
P ( W )=∑ P ( W , R , S )=P ( W |R , X ) P ¿ ¿
R ,S
P(R) = 0.4
Rain
P(W|R) = 0.9
Wet P(W|~R) = 0.2
Grass
p ( x|θ ) p (θ )
p ( θ|x )= αL ( θ|x ) p ( θ ) ×
p(x)
%Specify prior
alpha = 5;
beta = 10;
%Specify posterior
A1 = 20; %Sum of sample
N1 = 30; %Size of the sample
data = cleanData(inputData);
data.labels = cleanData(outputLabels);
for n = 1:size(data.X,2)
feature = data.X(:,n);
nUnique = length(unique(feature));
%Determine type of data such that
%0 CONSTANT: Feature is constant for all values
%1 BINARY: Feature only takes two distinct values
%2 CATAGORICAL: Feature only takes THRESH % values
%3 REGRESSION: Feature takes a large number of distinct values
if (nUnique == 1)
data.type = [data.type; [0, nUnique]]; %CONSTANT
elseif (nUnique == 2)
data.type = [data.type; [1, nUnique]]; %BINARY
else
data.type = [data.type; [3, nUnique]]; %REGRESSION
end
figure();
heatmap(corr(data.X));
data.corr = corr(data.X);
[valMax, idxMax] = max(abs(data.corr - eye(size(data.X,2))));
[valMin, idxMin] = min(abs(data.corr));
data.mapping.fNo = [];
data.mapping.labels = [];
for n = 1:size(rawData,2)
feature = table2array( rawData(:,n));
if(isa(feature, 'cell')) %Must contain at least 1 string
nan = 0; %NAN Count
nanIdx = []; %Indexes of NAN
for i = 1:length(feature)
if isempty(str2num(cell2mat(feature(i)))) %Empty is NAN
nan = nan + 1; %Add to count
nanIdx = [nanIdx i]; %Save index
end
end
data.numNan = [data.numNan; [n nan]];
for n = 1:length(K)
classFeatures = tX(tY == n,:);
MVG.TStat(n).prior = length(classFeatures)/length(tY);
MVG.TStat(n).mean = mean(classFeatures);
MVG.TStat(n).cov = cov(classFeatures);
MVG.TStat(n).postNum = mvnpdf(tX, MVG.TStat(n).mean, ...
MVG.TStat(n).cov)*MVG.TStat(n).prior;
end
for n = 1:length(K)
MVG.TPostNumSum = MVG.TPostNumSum + MVG.TStat(n).postNum;
end
figure();
hold on
for n = 1:length(K)
MVG.TStat(n).post = (MVG.TStat(n).postNum)./(MVG.TPostNumSum);
MVG.TPosterior = [MVG.TPosterior MVG.TStat(n).post];
end
hold off;
MVG.VPosterior = [];
MVG.VPostNumSum = zeros(size(K, 2) ,1);
for n = 1:length(K)
MVG.VStat(n).postNum = mvnpdf(vX, ...
MVG.TStat(n).mean, MVG.TStat(n).cov)*MVG.TStat(n).prior;
end
for n = 1:length(K)
MVG.VPostNumSum = MVG.VPostNumSum + MVG.VStat(n).postNum;
end
for n = 1:length(K)
MVG.VStat(n).post = (MVG.VStat(n).postNum)./(MVG.VPostNumSum);
MVG.VPosterior = [MVG.VPosterior MVG.VStat(n).post];
end
end
PCA.PCEigVal = reshape(PCA.eigVal(1:NPCA,:),[1,2]);
PCA.PCEigVect = PCA.eigVect(:,1:NPCA);
[dir,midx] = max(abs(PCA.PCEigVect));
for i = 1:NPCA
percent = abs(dir(:,i))/sum(abs(PCA.PCEigVect(:,i)))*100;
fprintf('PC%d has %.2f%% of its weight in the direction of feature
%d \n',i, round(percent,2), midx(i));
end
PCA.varCaptured = (sum(PCA.PCEigVal)/(sum(PCA.eigVal)))*100;
fprintf("PCA with %d components is capturing %.2f%% of the
variation\n",...
NPCA, round(PCA.varCaptured,2));
figure()
plot(linspace(-1,800,length(PCA.eigVect)), PCA.eigVal, '-x');
xlabel('Value index');
ylabel('EigenValue');
title('Scree graph');
figure();
varProp = [];
for n = 1:length(PCA.eigVal)
varProp = [varProp sum(PCA.eigVal(1:n))/(sum(PCA.eigVal))];
end
plot(varProp);
xlabel('Number of eigenvalues');
ylabel('Portion of variance captured');
title('Percentage variance captured per number of Principal
components');
end
nUnique = length(unique(feature));
%Determine type of data such that
%0 CONSTANT: Feature is constant for all values
%1 BINARY: Feature only takes two distinct values
%2 CATAGORICAL: Feature only takes THRESH % values
%3 REGRESSION: Feature takes a large number of distinct values
if (nUnique == 1)
stat.type = [stat.type; [0, nUnique]]; %CONSTANT
elseif (nUnique == 2)
stat.type = [stat.type; [1, nUnique]]; %BINARY
elseif ((nUnique)/length(data(:,n))) < 0.1
stat.type = [stat.type; [2, nUnique]]; %CATAGORICAL
else
stat.type = [stat.type; [3, nUnique]]; %REGRESSION
end
for n = 1:size(rawData,2)
feature = table2array(rawData(:,n));
if(isa(feature, 'cell')) %Must contain at least 1 string
nan = 0; %NAN Count
nanIdx = []; %Indexes of NAN
for i = 1:length(feature)
if isempty(str2num(cell2mat(feature(i)))) %Empty is NAN
nan = nan + 1; %Add to count
nanIdx = [nanIdx i]; %Save index
end
end
numNan = [numNan; [n nan]];