Skip to content
Snippets Groups Projects
ex4_1_7.py 1.86 KiB
Newer Older
  • Learn to ignore specific revisions
  • bjje's avatar
    bjje committed
    # exercise 4.1.7
    
    
    Stas Syrota's avatar
    Stas Syrota committed
    import importlib_resources
    
    bjje's avatar
    bjje committed
    import numpy as np
    
    Stas Syrota's avatar
    Stas Syrota committed
    from matplotlib.pyplot import cm, figure, imshow, show, subplot, title, xticks, yticks
    
    bjje's avatar
    bjje committed
    from scipy.io import loadmat
    
    
    Stas Syrota's avatar
    Stas Syrota committed
    filename = importlib_resources.files("dtuimldmtools").joinpath("data/zipdata.mat")
    
    bjje's avatar
    bjje committed
    # Digits to include in analysis (to include all, n = range(10) )
    n = [1]
    
    # Number of digits to generate from normal distributions
    ngen = 10
    
    # Load Matlab data file to python dict structure
    # and extract variables of interest
    
    Stas Syrota's avatar
    Stas Syrota committed
    traindata = loadmat(filename)["traindata"]
    X = traindata[:, 1:]
    y = traindata[:, 0]
    N, M = np.shape(X)  # or X.shape
    
    bjje's avatar
    bjje committed
    C = len(n)
    
    # Remove digits that are not to be inspected
    class_mask = np.zeros(N).astype(bool)
    for v in n:
    
    Stas Syrota's avatar
    Stas Syrota committed
        cmsk = y == v
    
    bjje's avatar
    bjje committed
        class_mask = class_mask | cmsk
    
    Stas Syrota's avatar
    Stas Syrota committed
    X = X[class_mask, :]
    
    bjje's avatar
    bjje committed
    y = y[class_mask]
    
    Stas Syrota's avatar
    Stas Syrota committed
    N = np.shape(X)[0]  # or X.shape[0]
    
    bjje's avatar
    bjje committed
    
    mu = X.mean(axis=0)
    s = X.std(ddof=1, axis=0)
    S = np.cov(X, rowvar=0, ddof=1)
    
    # Generate 10 samples from 1-D normal distribution
    
    Stas Syrota's avatar
    Stas Syrota committed
    Xgen = np.random.randn(ngen, 256)
    
    bjje's avatar
    bjje committed
    for i in range(ngen):
    
    Stas Syrota's avatar
    Stas Syrota committed
        Xgen[i] = np.multiply(Xgen[i], s) + mu
    
    bjje's avatar
    bjje committed
    
    # Plot images
    figure()
    for k in range(ngen):
    
    Stas Syrota's avatar
    Stas Syrota committed
        subplot(2, int(np.ceil(ngen / 2.0)), k + 1)
        I = np.reshape(Xgen[k, :], (16, 16))
        imshow(I, cmap=cm.gray_r)
        xticks([])
        yticks([])
        if k == 1:
            title("Digits: 1-D Normal")
    
    bjje's avatar
    bjje committed
    
    
    # Generate 10 samples from multivariate normal distribution
    Xmvgen = np.random.multivariate_normal(mu, S, ngen)
    
    Stas Syrota's avatar
    Stas Syrota committed
    # Note if you are investigating a single class, then you may get:
    
    bjje's avatar
    bjje committed
    # """RuntimeWarning: covariance is not positive-semidefinite."""
    # Which in general is troublesome, but here is due to numerical imprecission
    
    
    # Plot images
    figure()
    for k in range(ngen):
    
    Stas Syrota's avatar
    Stas Syrota committed
        subplot(2, int(np.ceil(ngen / 2.0)), k + 1)
        I = np.reshape(Xmvgen[k, :], (16, 16))
        imshow(I, cmap=cm.gray_r)
        xticks([])
        yticks([])
        if k == 1:
            title("Digits: Multivariate Normal")
    
    bjje's avatar
    bjje committed
    
    show()