ImportError: cannot import name 'array2d' - python

Code:
from sklearn.utils.validation import array2d
Output:
ImportError: cannot import name 'array2d'
I have installed sklearn v0.23.1 with pycharm.
I tried to look in site-packages\sklearn\utils\validation.py and find "array2d" with ctrl+F, but it is not there. Can I import something when it is not in the file?
I am not sure, if the problem is in installation or import.

According to the documentation of the latest stable version (currently 0.23.1), there is no module array2d in sklearn.utils.validation.
The situation is the same for the previous versions 0.22.2, 0.21.3, 0.20.4, 0.19.2, and 0.18.2.
Not sure where you got this from and what you are trying to do - googling the error leads to this closed Github thread, where the usage is not like the one you try here (and the error there comes from a broken installation, which is certainly not the case here).
In short, the command from sklearn.utils.validation import array2d is invalid, and the error thus justified.

Related

ImportError: cannot import name 'Random' from 'django.db.models.functions'

Well, I am trying to run my django project in new environment, I installed all of my requirements although every package is installed but it is still giving me below mentioned error. I am not sure but I am guessing it is due to version conflict of some modules.
I looked for existing solutions or someone who faced similar situation but no success.
the Random method is located in django/db/models/functions/math.py . change your import statement to this :
from django.db.models.functions.math import Random

Python4Delphi can't import numpy

I was curious about Python4Delphi and installed it and looked through the demos a bit. Now I wanted to install Numpy via CMD with pip this went well without errors. but now when I enter the following code in DEMO01 of Python4Delphi I get an error message. If I enter the same code in python it works. How can i solve this?
How i installed Numpy:
pip install numpy
Example code:
import numpy as np
print(np .__version__)
The error i got:
AttributeError: partially initialized module 'numpy' has no attribute '__version__' (most likely due to a circular import). Did you mean: '_version'?
The versions:
Delphi 11,
Python 3.10.4,
Pip 22.1,
Numpy 1.22.4 and
Win64
if i forgot some information let met know.
Can you check you're using the same versions of python using both methods by checking what this returns...
import sys
for p in sys.path:
print(p)
Carefully compare the output of that little bit of code as it'll point out any obvious differences.
From my experiments if you've got more than one Python on your system things can get weird with Python4Delphi so the above should provide evidence if this is your situation.
Importing numpy should actually cause an exception anyway, just not the one you mention in your question. Importing Numpy in demo01 will trigger a div by zero unless you add a MaskFPUExceptions(True); before you execute the python
If you use an embedded Python things will get even worse for you as you'll need to set some additional paths to import anything. The only way I've found to do this properly ATM is by addending paths to sys.path in a small python stub with paths constructed relative to the embedded root (Lib and Lib/site-packages incidentally)

ImportError for skbio module

I am running Python 3 and have skbio v0.5.5 installed. Following the examples in this tutorial, I am trying to run the import statements for some skbio classes, but am getting errors. For example,
from skbio.alignment import Alignment
results in
ImportError: cannot import name 'Alignment' from 'skbio.alignment'
Also,
from skbio import BiologicalSequence
results in
ImportError: cannot import name 'Alignment' from 'BiologicalSequence'
How do I resolve this?
The Alignment class is from an older version of the skbio library, specifically from before version 0.3.
If you would like to use those classes, you would need to install scikit-bio from before that version, by doing something like:
pip install scikit-bio==0.2
If you're just going through the Introduction to Applied Bioinformatics book, like you mentioned in your comment, it's better to use the latest version of the book instead, which will use the latest version of the scikit-bio library.

Import threshold_yen and threshold_isodata from skimage.filter

I am working with different threshold algorithms from SKimage, and when I go to import certain packages I get an error, but have no problem with others. For example:
from skimage.filter import threshold_adaptive, threshold_isodata
returns the traceback:
ImportError: cannot import name threshold_isodata. I am using python 2.7, and following the documentation found here: http://scikit-image.org/docs/dev/api/skimage.filter.html#skimage.filter.threshold_isodata
Specifically, I'm hoping to use threshold_isodata and threshold_yen. Does anybody have suggestions for how to solve this error? Alternatively, are there other packages that use the same algorithm?
As mentioned in a comment, threshold_isodata is only available in the master repo (i.e. not officially released in v0.9), hence the import error.
It turns out that threshold_yen wasn't properly imported into the filter subpackage in version 0.9. (This has been fixed in master.) Until v0.10 is released, you should import threshold_yen as follows:
from skimage.filter.thresholding import threshold_yen
EDIT: Note that this question and answer are specific to very old versions of scikit-image. The skimage.filter module was renamed skimage.filters in v0.11

How to import a module from a directory?

On my system I have two versions of Python (to call them I type python and python2 in the command line). When I use the first version of Python, I cannot import sklearn module but I can do it in the second version of Python.
I would like to use the first version of python (because other modules are available there) and, at the same time, I would like to be able to import sklearn from this version of Python.
My solution was to use:
import sys
sys.path.append('location_of_the_sklearn_module')
To find the location of the sklearn module I started a python session (using the second version of python, in which sklearn works). The I type:
import sklearn
sklearn.__file__
As a result I got:
/home/name/my_name/numpy/local/lib/python2.7/site-packages/sklearn/__init__.pyc
In the session of the first version of Python I tried:
import sys
sys.path.append('/home/name/my_name/numpy/local/lib/python2.7/site-packages/sklearn')
import sklearn
Unfortunately it did not work. As a result I got: ImportError: No module named sklearn
Does anybody know what I am doing wrong and if it is possible to reach the goal in the way I try?
When importing packages, you need to add the parent directory of the package to PYTHONPATH, not the package directory itself, so just change...
sys.path.append('/home/name/my_name/numpy/local/lib/python2.7/site-packages/sklearn')
...to...
sys.path.append('/home/name/my_name/numpy/local/lib/python2.7/site-packages')
...although it may not necessarily import correctly in Python 3.x.

Categories

Resources