According to this task:
Principal Component Analysis (PCA) in Python
I included this line
import from matplotlib.mlab import PCA
but I get the error message:
cannot import name 'PCA' from 'matplotlib.mlab'
I'm using Python3.7 and I have no idea how I can use the PCA function from matlab. Is the new version of matplotlib depricated or is PCA included to another library?
I really don't know if it is too late to reply now. But I will just place it here anyways.
import numpy as np
from sklearn.decomposition import PCA
Related
I'm playing around with matplotlib to understand its structure better and I'm confused by the following piece of code:
import matplotlib as mpl
from mpl import pyplot as plt # ModuleNotFoundError : No module named 'mpl'
mpl.pyplot # AttributeError: module 'matplotlib' has no attribute 'pyplot'
If on the other hand I abstain from importing matplotlib as a different name and execute instead
import matplotlib
from matplotlib import pyplot as plt #works!
the things work.
Even more crazy, if I "combine" these two
import matplotlib as mpl
from matplotlib import pyplot as plt #works!
mpl.pyplot.get_backend() # works
I can curiously access attributes from pyplot even if I reference it as mpl.pyplot.
What is going on here, why does
from mpl import pyplot as plt throws a ModuleNotFoundError?
import mpl.pyplot not work? Since the error message indcates that mpl was correctly resolved to matplotlib, yet still pyplot couldn't be found...
referencing pyplot as mpl.pyplot not cause an error in my last example?
(Note that I do know of course that the preferred way is to import pyplot as import matplotlib.pyplot as plt but the point of my investigation is precisely to understand what fails and why when I ventured outside the welltrodden streets of code.)
The as part in an import statement is just syntactic sugar for assigning the imported module to a variable with the given name. The import documentation describes it as such:
If the module name is followed by as, then the name following as is bound directly to the imported module.
"Bound" in this context means doing an assignment to the given name. The following statement
import someModule as someName
Is equivalent to this code:
import someModule
someName = someModule
So, when you do import matplotlib as mpl, all you do is create a variable called mpl. This has no effect on any further import statements, as import statements do not care about your local variables, but search for python packages and modules - and an import as statement cannot change the package or module name of the imported element. Which is a good thing, as you do not want your import statements to fail just because another import 5 lines earlier used a conflicting name in an as clause.
Now, why you're getting weird results with the import mpl.pyplot statement - no idea, it shouldn't work. You can see the expected behaviour if you try the following:
import os as asdf
import asdf.path #ModuleNotFoundError: no module named 'asdf'
Tested with python 3.10.2 on Archlinux. If your example is reproducible, then you might have found a weird bug or undefined behaviour in your specific python version, or have some other issue (e.g. a mpl module in your path... although that on its own wouldn't explain why you get an error message about matplotlib instead of mpl).
In conclusion, all as does is assigning a name to the imported object, so a name assigned with as cannot be used as a source in another import statement.
On package imports and why matplotlib.pyplot gives an error:
Importing a package only imports the package itself, not any of its subpackages or modules. A package can explicitly import submodules in its __init__.py, so if the matplotlib init file would contain a statement like from . import pyplot line then accessing matplotlib.pyplot would work. There are however many reasons why a package might choose not to import any submodules, such as time and resources required to import and initialize them.
Everything in python is a object, but when you import matplotlib you are importing all library but you cannot change your functions names, the library will looking for plt
from matplotlib import pyplot as plt in this case.
You can use plt directly but you cannot use mpl.plt.
When you tried it :
import matplotlib as mpl
from mpl import pyplot as plt # ModuleNotFoundError : No module named 'mpl'
mpl.pyplot
You should import correctly:
import matplotlib as mpl
from mpl import pyplot
mpl.pyplot
'As' change the module name what you going to use in your project, but not from another import. But if you use plt directly it is going works.
Try this code to understand :
import matplotlib as mpl
import matplotlib
print(type(matplotlib))
print(type(mpl))
from matplotlib import pyplot as plt# ModuleNotFoundError : No module named 'mpl'
from matplotlib import pyplot
print(type(plt))
print(type(mpl.pyplot))
plt
mpl.pyplot
When you do import module as mdl, it doesn't change the module name. It only affects other statements when you actually use it. But the name doesn't change in the actual python lib or the external library. So what you can do is this:
import matplotlib
from matplotlib import pyplot as plt
or just from matplotlib import pyplot as plt
This is my answer...
I have imported norm as:
from scipy.stats import norm
I want to find out the version using:
print(scipy.__version__)
but it is raising an error called:
NameError: name 'scipy' is not defined
if i am using this:
print(norm.__version__)
but it is raising another error called:
AttributeError: 'norm_gen' object has no attribute '__version__'
Please help me to solve this issue.
Thanks
The line from scipy.stats import norm doesn't make the name scipy available in your current namespace. To use scipy.__version__, you must first import scipy.
In [57]: import scipy
In [58]: print(scipy.__version__)
1.4.1
I finished the code yesterday and ran it well. But after some other process on another code document, the previous code doesn't work. What funny is, as I learned from some answers on the website, if I change the wheel import sequence, the error result will be different.
I have tried reinstalled python. But it doesn't work.
Code 1:
import numpy as np
import scipy.integrate
import pandas as pd
print(np.mean(1+3))
Code 2:
import pandas as pd
import numpy as np
import scipy.integrate
print(np.mean(1+3))
I'm learning sklearn, but I can't use fetch_openml(). It says,
ImportError: cannot import name 'fetch_openml' from 'sklearn.datasets'
In the new version of sklearn, it's even easier to fetch open ML Datasets. For example, you can add import and fetch mnist dataset as:
from sklearn.datasets import fetch_openml
X, y = fetch_openml('mnist_784', version=1, return_X_y=True, as_frame=False)
print(X.shape, y.shape)
For more details check official example.
You can use this:
from sklearn.datasets import fetch_openml
Apparently, fetch_mldata has been deprecated in the newer sklearn. Use load_digits to achieve loading the MNIST data.
To solve this problem in jupyter follow these steps:
Download file mnist-original from " https://osf.io/jda6s/"
after download file copy it into C:\Users\YOURUSERNAME\scikit_learn_data\mldata
in notebook jupyter do:
from sklearn.datasets import fetch_mldata
mnist = fetch_mldata('mnist-original')
When I initialize HDFStore
import numpy as np
import pandas as pd
hdf = pd.HDFStore('polar.h5')
I see an error in flavor.py:
NameError, name '_conv_python_to_python' is not defined
I am using anaconda and pandas does work for dataframe stuff
I resolved it by adding:
import h5py
from pandas import HDFStore,DataFrame
This is actually not a real problem. I checked Breakpoint: "All Exceptions" and it paused here, simply unchecking it resolves the issue. You can continue from this error.