0
import sklearn

# Spot check algorithm
models = []
models.append(('LR', LogisticRegression()))
models.append(('LDA', LinearDiscriminantAnanlysis()))
models.append(('KNN', KNeighbourClassifier()))
models.append(('CART', DecisionTreeClassifier()))
models.append(('NB', GaussianNB()))
models.append(('SVM', SVC()))
#evaluate each model in turn
results = []
names = []
for name, model in models:
    kfold = model_selection.KFold(n_splits = 10, random_state = seed)
    cv_results = model_selection.cross_val_score(model, X_train, 
    Y_train, cv = kfold, scoring = scoring)
    results.append(cv_results)
    names.append(name)
    msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std())
    print(msg)

On running this code am getting error name 'LogisticRegression' is not defined and the same for each function

but when i import each functions separately, the functions work properly

Please help, I tried searching for the remeedy but am not able to find it.

  • Does this answer your question? [module 'sklearn' has no attribute 'cross\_validation'](https://stackoverflow.com/questions/46572475/module-sklearn-has-no-attribute-cross-validation) – ScootCork Jun 27 '20 at 11:37

3 Answers3

2

This way it cannot work. If you want to just import sklearn, for each model you should specify the module for it to be correctly imported. Ex:

import sklearn

# Spot check algorithm
models = []
models.append(('LR', sklearn.models.LogisticRegression()))

However, IMO this is less readable, so you might wanna keep the imports separate. Also, if you just import sklearn you will have quite some unused imports, which is also unnecessary.

Koralp Catalsakal
  • 1,114
  • 8
  • 11
1

See when you import a module

import module

so any function, class or for that matter any thing that resides within the module can't be accessed directly as by importing the module we define the module and not it's functions or classes.

So if u want to just write the name of a class and run it then you need to import everything from the module.

from module import *

'*' here means that we are importing everything or 'all' from the module. But in case, you want only the module to be imported you can also use

module.Class()

this defines the class and we have to access it from the module as it's the module that is imported and not just it's classes and functions.

Also the reason why we use

from module import Class

is because this just imports that specific class or function. This helps reduce the amount of things imported as not the whole module is imported. So it somewhat helps in improving both the speed of the program and the performance of the computer.

I hope this was helpful And hope you are safe during this ongoing pandemic!

typedecker
  • 1,351
  • 2
  • 13
  • 25
0

You can specify which functions that are part of sklearn you are going to use in the import statement.

from sklearn import LogisticRegression, LinearDiscriminantAnanlysis # and the list continues

This way, you can avoid having to type sklearn.{function_name} every time you want to use a submodule in sklearn. It is also arguably more efficient in that you are loading only the modules that you will be using, as opposed to a blanket statement like import sklearn, which tells the program no prior information on what segment of sklearn you are going to use.

Jake Tae
  • 1,681
  • 1
  • 8
  • 11