1

I was trying to modify a function within a class. I was following the steps from this link. I want to understand why the changes are not working.

The function is:

def explain(self, test_df, row_index=None, row_num=None, class_id=None, bacckground_size=50, nsamples=500)

from module ktrain

I am trying to get the shape values themselves instead of the plot. My changes are in

 def alternative_explain (self, test_df, row_index=None, row_num=None, class_id=None, background_size=50, nsamples=500)

Then I try:

import types
import ktrain
funcType = types.MethodType
predictor1 = TabularPredictor()

But get the error that "name 'TabularPredictor' is not defined. Simarly, I can not make a new, inherited class from TabularPredictor. What am I doing wrong?

Update: I did import ktrain

Alex Waygood
  • 6,304
  • 3
  • 24
  • 46
J.Julian
  • 68
  • 6

1 Answers1

4

It sounds like you're a little confused about the python import statement, which has several alternative syntaxes.

Using import ktrain will only import a reference to the module ktrain in your code; if you want your code to refer to anything inside the ktrain module, you need to use dot-notation, e.g. ktrain.TabularPredictor(). Pros: everything from the ktrain module is now accessible from within your code. Cons: it might be a bit wordy to type out ktrain.TabularPredictor() every time you want to make an instance of the class, and you might only actually need one or two classes from that module.

Using from ktrain import TabularPredictor will make the TabularPredictor class accessible in your code's namespace, so there will be no need to use the dot-notation; you can just type TabularPredictor() when you want to create an instance. Pros: less wordy, you only import what you need (none of the other classes or functions from ktrain will be accessible from within your code). Cons: you might find out later on that some of the other classes/functions in the module are useful, which means you'll have to change your import statement. It can also be a pain to have to individually import 10 different classes from the same module.

You can read more here.

Alex Waygood
  • 6,304
  • 3
  • 24
  • 46
  • when I do from ktrain import TabularPedictor I receive an error "cannot import name "TabularPredictor" from 'ktrain' (***\ktrain\__init__.py) – J.Julian Jun 22 '21 at 20:44
  • I've never used the module, but, from looking at the directory structure on github, you could try `from ktrain.tabular.predictor import TabularPredictor`. The TabularPredictor class is found within the predictor.py file, found within the tabular directory, found within the ktrain package. – Alex Waygood Jun 22 '21 at 20:52