I am using anaconda and running a script on spyder. When I call
import from sklearn.metrics balanced_accuracy_score
I get the error: "cannot import name balanced_accuracy_score "
Why is this particular metric not being imported?
import from is not valid syntax for Python, the pattern is
import <package> - for entire package
or
from <package> import <module> - only specific module in package
you want the latter, try:
from sklearn.metrics import balanced_accuracy_score
Related
I receive an error message:
cannot import name 'ConfusionMatrixDisplay' from 'sklearn.metrics'".
when I run the following import code:
from sklearn.metrics import ConfusionMatrixDisplay
How to fix it?
Most likely your version of sklearn is outdated -
sklearn.metrics.ConfusionMatrixDisplay was added in sklearn>=1.0.0.
Source (docs)
You can check your sklearn version with:
python3 -m pip show scikit-learn
trying to do the following import:
from sklearn.metrics.regression import mean_absolute_error, mean_squared_error, r2_score
and I get the error:
ModuleNotFoundError: No module named 'sklearn.metrics.regression'
tried fixing with the installation of:
pip install -U scikit-learn scipy matplotlib
but I don't think that's what I'm missing since it didn't work...
You're probably looking to import them from sklearn.metrics, not sklearn.metrics.regression
Link to the library
What you are looking for is:
from sklearn import metrics
metrics.mean_absolute_error(<your params here>)
metrics.mean_squared_error(<your params here>)
metrics.r2_score(<your params here>)
Edit
you can check the attributes of the metrics instance with:
dir(metrics)
My python file looks like this:
import sys, os
sys.path.append("../..")
sys.path.append("..")
sys.path.append(os.getcwd())
#import pdb
from sklearn.preprocessing import StandardScaler
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
import copy
from tslib import tsUtils
from tslib.src.synthcontrol.syntheticControl import RobustSyntheticControl
from tslib.src.synthcontrol.multisyntheticControl import MultiRobustSyntheticControl
But i keep receiving this error:
Traceback (most recent call last):
File "testScriptMultiSynthControlSVDV1.1.py", line 34, in <module>
from tslib import tsUtils
ImportError: cannot import name 'tsUtils'
While the tsUtils file is clearly inside the src folder. Any idea as to why I'm getting "cannot import name" error would be extremely helpful.
dir(module) lists all members of a file/module. From the print result, it is obvious that you do not have a member called tsUtils.
Basically you are trying to import some member/attribute which is not defined in that module/file.
Ensure you are working with right version of files.
I would suggest that first uninstall tsUtils using pip uninstall command then install again using pip install and restart your system.
Why i am getting the error fastai.structured is not a module?.
i have tried installing previous versions of fastai. but nothing helped.
from fastai.imports import *
from fastai.structured import *
#from pandas_summary import DataFrameSummary
from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
from IPython.display import display
from sklearn import metrics
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-17-35432a48f631> in <module>()
1 from fastai.imports import *
----> 2 from fastai.structured import *
3
4 #from pandas_summary import DataFrameSummary
5 from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
ModuleNotFoundError: No module named 'fastai.structured'
---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.
To view examples of installing some common dependencies, click the
"Open Examples" button below.
That module is no longer included in fastai's default python packages. Your default setup commands for using fastai packages will no longer includes that module. You may need to download it from the GitHub master, save it to your working directory, and import from your working directory to your jupyter notebook.
Here's a note from the fastai forum:
The structured.py has been moved to folder “old” (in anticipation to fastai_v1).
https://github.com/fastai/fastai/blob/master/old/fastai/structured.py
--- Andrei Oct '18
When importing from your working directory:
from structured import *
This will replace:
from fastai.structured import *
The module’s name has been changed to “tabular”. So use “from fastai.tabular import *” instead.
see this >> https://forums.fast.ai/t/modulenotfounderror-no-module-named-fastai-structured/36904
I installed gensim, Python library.
I executed the command
Import gensim
It executed without any error.
Then I tried to import test from gensim using the command
from gensim import test
and it showed the following error
Traceback (most recent call last):
File "", line 1, in
from gensim import test
ImportError: cannot import name 'test'
Python site-packages had gensim folder in that.
Any help would be highly appreciated.
As it says: cannot import name 'test'
That means that test is not in gensim package.
You can see package's modules with:
import gensim
print(gensim.__all__) # when package provide a __all__
or
import gensim
import pkgutil
modules = pkgutil.iter_modules(gensim.__path__)
for module in modules:
print(module[1])
Edit:
How can I verify gensim installation ?
try:
import gensim
except NameError:
print('gensim is not installed')
Side note: if you have a file or package named gensim, this will ne imported instead of the real package.
I had similar experience but with scipy. After uninstalling scipy import scipy did not give error. but none of its modules will work. I noticed that scipy folder was still in \site-packages but modules inside it were uninstalled.
When I installed it again it worked fine.
You should also see inside the directory, and try to reinstall or upgrade it.