Skip to content
Snippets Groups Projects
ex1_5_3.py 1.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • bjje's avatar
    bjje committed
    # exercise 1.5.3
    
    Stas Syrota's avatar
    Stas Syrota committed
    import importlib_resources
    
    bjje's avatar
    bjje committed
    from scipy.io import loadmat
    
    Stas Syrota's avatar
    Stas Syrota committed
    
    # You can load the matlab data (matlab's m-file) to Python environment with
    # 'loadmat()' function imported from 'scipy.io' module.
    # The matlab workspace is loaded as a dictionary, with keys corresponding to
    
    bjje's avatar
    bjje committed
    # matlab variable names, and values to arrays representing matlab matrices.
    
    
    # Get path to the datafile
    
    Stas Syrota's avatar
    Stas Syrota committed
    filename = importlib_resources.files("dtuimldmtools").joinpath("data/iris.mat")
    
    
    # Print the location of the iris.mat file on your computer. 
    # You should inspect it manually to understand the format and content
    print("\nLocation of the iris.mat file: {}".format(filename))
    
    # Load Matlab data file to python dict structure
    
    Stas Syrota's avatar
    Stas Syrota committed
    iris_mat = loadmat(filename, squeeze_me=True)
    
    Stas Syrota's avatar
    Stas Syrota committed
    # The argument squeeze_me ensures that there the variables we get from the
    
    bjje's avatar
    bjje committed
    # MATLAB filed are not stored within "unneeded" array dimensions.
    
    # You can check which variables are in the loaded dict by calling
    # the function keys() for the dict:
    
    Stas Syrota's avatar
    Stas Syrota committed
    # mat_data.keys()
    
    bjje's avatar
    bjje committed
    # this will tell you that X, y, M, N and C are stored in the dictionary,
    # as well as some extra information about e.g. the used MATLAB version.
    
    # We'll extract the needed variables by using these keys:
    
    Stas Syrota's avatar
    Stas Syrota committed
    X = iris_mat["X"]
    y = iris_mat["y"]
    M = iris_mat["M"]
    N = iris_mat["N"]
    C = iris_mat["C"]
    attributeNames = iris_mat["attributeNames"]
    classNames = iris_mat["classNames"]
    
    bjje's avatar
    bjje committed
    
    # Loading the Iris data from the .mat-file was quite easy, because all the work
    
    Stas Syrota's avatar
    Stas Syrota committed
    # of putting it into the correct format was already done. This is of course
    # likely not the case for your own data, where you'll need to do something
    # similar to the two previous exercises. We will, however, sometimes in the
    # course use .mat-files in the exercises.