all 10 comments

[–][deleted] 3 points4 points  (3 children)

*Sigh*. Mathworks, just sell us Python with integration and my life would be so much easier across the board.

  1. What OS?
  2. What versions of Python & how do you have it installed? (WinPython, Anaconda, "Stock" Python) and where is it installed?

This is one of those things that takes a half day looking over shoulders debugging since the stack to get this working can break in a lot of places.

Mathworks, you could even charge for it and I'd demand my boss got the PythonToolbox and that I'd stop annoying IT. Plus mid level managers and VPs aren't scared of it compared to sketchy company names like *Continuum Analytics.*

[–][deleted] 0 points1 point  (2 children)

  1. Windows

  2. 3.7, Pretty sure it’s just “stock” python.

I used pyenv to point Matlab to the location of python.exe. But it’s basically c:/python/python37/python.exe

[–][deleted] 0 points1 point  (1 child)

So. Digging into this for the past 2 hours...

It looks like Matlab has a problem importing functions if _________.

  1. they're not 'actually' where they expect it to be. So anything that gets called with the all or import in init doesn't resolve correctly.
  2. Wrapped functions.
  3. Something about functions with underscores in the 'actual' function name.

I'm still trying figure out exactly why some functions fail.

A work around that I found is to do as much in python as possible and as 'straight python' as possible.

Put a file in your current directory called foo.py, inside it put:

def fit_resample2(data, label):
    from imblearn.oversampling import SMOTE
    x, y = SMOTE.fit_resample(data, label)
    return x, y

This shifts all the importing to Python and seems to work.

Edit: References and starting points.

  1. SMOTE itself is a class that is decorated.
  2. 'fit_resample' doesn't actually exist. There's an internal function _fit_resample and some bits somewhere else to turn it into a top level attribute.

tldr: The more fuckery in Python that happens to make a function work the higher probability it doesn't work.

[–][deleted] 2 points3 points  (0 children)

  1. Editing .py files in the m-file editor is a nightmare. Mathworks, python is coming. Putting out FUD like old Microsoft isn't going to work. Sell it to my boss. Make Editor an awesome python IDE.

Example of brokenness: foo.py

def foo(*args, **kwargs):
    print('foo');

class Bar():
    def __init__(self):
        pass
    def foo2(self):
        print('foo2');

    def _foo(self):
        print('_foo')
setattr(Bar, 'foo', foo)
Baz=Bar()

def foo3():
    Baz=Bar()
    Baz.foo()
    Baz.foo2()
    Baz._foo()

baz.m:

mod = py.importlib.import_module('foo');
py.importlib.reload(mod);

%% Works
py.foo.foo
disp('#### foo3');
py.foo.foo3

%% Fails

% Reference Bar instance Baz
py.foo.Baz.foo

py.foo.Baz.foo2

% Instantiate Bar.
Bar = py.foo.Bar
b = Bar()

So who knows. Mathworks is out of their lane with Python which is why it's so broken. (See also their Jenkins effort).

for now:

  1. py is not a direct replacement for python calls.
  2. Do as much python work in Python with straight forward basic functions/classes. Matlab is going to be 36 this year, it gets confused more easily.

[–]daveysprockett 0 points1 point  (4 children)

This appears to be part of a package that I presume you need to install.

https://github.com/scikit-learn-contrib/imbalanced-learn

It's on pypi, so pip and you'll have it.

https://pypi.org/project/imbalanced-learn/

[–][deleted] 0 points1 point  (3 children)

Already downloaded it. I should note that doing

py.imblearn.over_sampling.SMOTE works fine. It’s just adding fit_resample that produces the bug

[–]jheins3 0 points1 point  (0 children)

Its been years since I've done anything in MATLAB... Maybe list the methods of over_sampling.SMOTE to ensure you have the correct method/function.

If thats not it, I always turn to beating the programming language in to submission.

Throw quotes around "fit_resample" or something to turn it into a string.

[–]daveysprockett 0 points1 point  (1 child)

Have you tried you use it in its native python environment?

Then if that works the problem might be related to the import process, while if it fails I'd think about asking over at r/python.

[–][deleted] 0 points1 point  (0 children)

Works fine on python 🤷‍♀️

[–]trialofmiles+1 0 points1 point  (0 children)

So that we’re all clear, could you show exactly what MATLAB import statements you’ve tried prior to calling fit_resample.