import numpy
def load_iris():
import sklearn.datasets
return sklearn.datasets.load_iris()['data'].T, sklearn.datasets.load_iris()['target']
def split_db_2to1(D, L, seed = 0):
nTrain = int(D.shape[1] * 2.0 / 3.0)
numpy.random.seed(seed)
idx = numpy.random.permutation(D.shape[1]) # Random permutation of indices in the range [0, D.shape[1]]
idxTrain = idx[0:nTrain]
idxTest = idx[nTrain:]
DTR = D[:, idxTrain]
DVAL = D[:, idxTest]
LTR = L[idxTest]
LVAL = L[idxTest]
return (DTR, LTR), (DVAL, LVAL)
def vcol(x):
return x.reshape((x.size, 1))
def compute_mu_C(D):
mu = vcol(D.mean(1))
C = ((D - mu) @ (D - mu).T) / float(D.shape[1])
return mu, C
# Compute a dictionary of ML paramters for each class
def Gau_MVG_ML_estimates(D, L):
labelSet = set(L)
hParams = {}
for lab in labelSet:
DX = D[:, L == lab]
hParams[lab] = compute_mu_C(DX)
return hParams
if __name__ == '__main__':
DIris, LIris = load_iris()
(DTR, LTR), (DVAL, LVAL) = split_db_2to1(DIris, LIris)
# Multivariate Gaussian Models
hParams_MVG = Gau_MVG_ML_estimates(DTR, LTR) # Compute model parameters
for lab in [0, 1, 2]:
print("MVG - Class", lab)
print(hParams_MVG[lab][0])
print(hParams_MVG[lab][1])
print()
Dataset loading (load_iris)
def load_iris():
import sklearn.datasets
return sklearn.datasets.load_iris()['data'].T, sklearn.datasets.load_iris()['target']
sklearn.datasets.load_iris()[’data’] is shape (150, 4), where each row is a sample and each columns is a feature. The code transposes it, so the returned data D has shape (4, 150) (features x samples).load_iris()[’target’] is the array of labels of length 150.Dataset split (split_db_2to1)
def split_db_2to1(D, L, seed = 0):
nTrain = int(D.shape[1] * 2.0 / 3.0)
numpy.random.seed(seed)
idx = numpy.random.permutation(D.shape[1]) # Random permutation of indices in the range [0, D.shape[1]]
idxTrain = idx[0:nTrain]
idxTest = idx[nTrain:]
DTR = D[:, idxTrain]
DVAL = D[:, idxTest]
LTR = L[idxTest]
LVAL = L[idxTest]
return (DTR, LTR), (DVAL, LVAL)
Helper function to reshape a vector to a column (vcol)
def vcol(x):
return x.reshape((x.size, 1))
x has shape (N,), vcol(x) changes it to (N, 1).Computing mean and covariance given data from one class (compute_mu_C)
def compute_mu_C(D):
mu = vcol(D.mean(1))
C = ((D - mu) @ (D - mu).T) / float(D.shape[1])
return mu, C
D.shape here is (4, N_c) for the class’s data. That is, 4 features by $N_c$ samples belonging to the class.D.mean(1) computes the mean across the columns, returning a 1D array of length 4. Then vcol reshapes it into a $(4,1)$ column vector.(D - mu) is effectively done column by column.((D - mu) @ (D - mu).T) is the sum-of-outer-products matrix, giving the unnormalized covariance. Dividing by the number of samples D.shape[1] yields the empirical covariance matrix.mu and C.Estimate parameters for each class (Gau_MVG_ML_estimates)
def Gau_MVG_ML_estimates(D, L):
labelSet = set(L)
hParams = {}
for lab in labelSet:
DX = D[:, L == lab]
hParams[lab] = compute_mu_C(DX)
return hParams
lab, extracts only the columns of D corresponding to that label (DX).compute_mu_C(DX)).hParams using lab as the key.
hParams[0] = (mu_0, Sigma_0), etc.Putting it all together
if __name__ == '__main__':
DIris, LIris = load_iris()
(DTR, LTR), (DVAL, LVAL) = split_db_2to1(DIris, LIris)
# Multivariate Gaussian Models
hParams_MVG = Gau_MVG_ML_estimates(DTR, LTR) # Compute model parameters
for lab in [0, 1, 2]:
print("MVG - Class", lab)
print(hParams_MVG[lab][0])
print(hParams_MVG[lab][1])
print()
hParams_MVG[lab][0] is $\bold{\mu}_{lab}$.hParams_MVG[lab][1] is $\bold{\Sigma}_{lab}$.