Skip to content
Snippets Groups Projects
Select Git revision
  • 18c851b341ff5c4d01941d1db2d04b47bc7d83ef
  • main default protected
  • s2025dev
  • plot-fixes
  • Fall2024
5 results

ex1_5_3.py

Blame
  • user avatar
    Stas Syrota authored
    18c851b3
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    ex1_5_3.py 1.49 KiB
    # exercise 1.5.3
    import importlib_resources
    from scipy.io import loadmat
    
    # 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
    # matlab variable names, and values to arrays representing matlab matrices.
    
    # Load Matlab data file to python dict structure
    
    filename = importlib_resources.files("dtuimldmtools").joinpath("data/iris.mat")
    iris_mat = loadmat(filename, squeeze_me=True)
    # The argument squeeze_me ensures that there the variables we get from the
    # 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:
    # mat_data.keys()
    # 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:
    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"]
    
    # Loading the Iris data from the .mat-file was quite easy, because all the work
    # 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.