简体   繁体   中英

failed to import module in another jupyter notebook

I have 2 jupyter notebooks, first one is named friedman_1.ipynb and I defined a class. In 2nd jupyter notebook, I can import it without error. However I got an error when calling the class method. see below. Thanks for your help.

class Friedman1Test:
    """This class encapsulates the Friedman1 regression test for feature selection
    """

    VALIDATION_SIZE = 0.20
    NOISE = 1.0

    def __init__(self, numFeatures, numSamples, randomSeed):
        """
        :param numFeatures: total number of features to be used (at least 5)
        :param numSamples: number of samples in dataset
        :param randomSeed: random seed value used for reproducible results
        """

        self.numFeatures = numFeatures
        self.numSamples = numSamples
        self.randomSeed = randomSeed

in 2nd jupyter notebook, I am trying to import the module and call its class method, then I got an error


from ipynb.fs.full.friedman_1 import *

# create the Friedman-1 test class:
friedman = friedman_1.Friedman1Test(NUM_OF_FEATURES, NUM_OF_SAMPLES, RANDOM_SEED)
friedman

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-12-48edeee5c421> in <module>
      1 # create the Friedman-1 test class:
----> 2 friedman = friedman_1.Friedman1Test(NUM_OF_FEATURES, NUM_OF_SAMPLES, RANDOM_SEED)
      3 friedman

NameError: name 'friedman_1' is not defined

You are already importing all the functions and classes into the notebook. Try the following directly. -

from ipynb.fs.full.friedman_1 import *

friedman = Friedman1Test(NUM_OF_FEATURES, NUM_OF_SAMPLES, RANDOM_SEED)
friedman

Try this


from ipynb.fs.full.friedman_1 import *

# create the Friedman-1 test class:
friedman = Friedman1Test(NUM_OF_FEATURES, NUM_OF_SAMPLES, RANDOM_SEED)
friedman

You have already imported the class when you use the asterisk, you just need to call the class constructor directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM