'module' object has no attribute 'corrplot' - python

import seaborn as sns
import matplotlib.pyplot as plt
sns.corrplot(rets,annot=False,diag_names=False)
I get this error after I call the function above...don't know whats going on
AttributeError Traceback (most recent call last)
<ipython-input-32-33914bef0513> in <module>()
----> 1 sns.corrplot(rets,annot=False,diag_names=False)
AttributeError: 'module' object has no attribute 'corrplot'

The corrplot function was deprecated in seaborn version v0.6: https://seaborn.github.io/whatsnew.html#other-additions-and-changes:
The corrplot() and underlying symmatplot() functions have been deprecated in favor of heatmap(), which is much more flexible and robust. These two functions are still available in version 0.6, but they will be removed in a future version.
Note that the function actually still exists in the seaborn codebase, but you have to directly import it from seaborn.linearmodels and you will get a warning that it is subject to removal in a future release.

Import using the command for corrplot and symmatplot
from seaborn.linearmodels import corrplot,symmatplot

corrplot or symmatplot has been deprecated in favor of heatmap .
#Example usage
>>> import numpy as np; np.random.seed(0)
>>> import seaborn as sns; sns.set()
>>> data = np.random.rand(10, 12)
>>> ax = sns.heatmap(data)
Check this for more about heatmap: http://seaborn.pydata.org/generated/seaborn.heatmap.html#seaborn.heatmap

Related

np is not defined after importing numpy

Why am I having this error
import numpy as np
np.array([1,2,3,4])
NameError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_2304/609253908.py in
----> 1 np.array([1,2,3,4])
NameError: name 'np' is not defined
Your error code says that np.array([1, 2, 3, 4]) is in line 1 so you are trying to use it before importing, you need to import numpy first, as shown in your question.
You can just go with numpy
import numpy
numpy.array([1,2,3,4])
Have you installed or updated numpy?
Try this once in your terminal.
>>> pip install numpy

DeprecationWarning rasied in Spyder for every Scipy function used

I'm coding in Spyder and the code runs, but every line that uses sp.___ raises a DeprecationWarning, e.g. DeprecationWarning: scipy.array is deprecated and will be removed in SciPy 2.0.0, use numpy.array instead.
Why is Spyder doing this and how do I allow me to use scipy without raising this error? Failing that, what can I do to suppress the error from popping up each time?
The code is like this:
import matplotlib.pyplot as plt,scipy as sp
import scipy.optimize as op
a=9.3779
x_in=sp.array([.095,.065,.09,.108,.125,.115,.040,.055,.055])
x=(x_in+14)
y_in=sp.array([.2,.6,.5,.4,.1,.3,-0.2,-0.4,0])
y=y_in+45
ax.plot(x_in,y_in,'ro')
plt.show()
This raises the error:
C:\Users\Shiva Pingle\Desktop\python\others\peaks.py:38: DeprecationWarning: scipy.array is deprecated and will be removed in SciPy 2.0.0, use numpy.array instead
x_in=sp.array([.095,.065,.09,.108,.125,.115,.040,.055,.055])
C:\Users\Shiva Pingle\Desktop\python\others\peaks.py:40: DeprecationWarning: scipy.array is deprecated and will be removed in SciPy 2.0.0, use numpy.array instead
y_in=sp.array([.2,.6,.5,.4,.1,.3,-0.2,-0.4,0])
Your solution in the comments will make you ignore all the deprecation warnings. This is not suggested.
You could instead import numpy as np and use the np.array().
Corrected code:
import matplotlib.pyplot as plt,scipy as sp
import scipy.optimize as op
import numpy as np # Added import of numpy
a=9.3779
x_in=np.array([.095,.065,.09,.108,.125,.115,.040,.055,.055]) # Changed sp to np
x=(x_in+14)
y_in=np.array([.2,.6,.5,.4,.1,.3,-0.2,-0.4,0]) # Changed sp to np
y=y_in+45
plt.plot(x_in,y_in,'ro') # Also changed the ax to plt
plt.show()

Error "ModuleNotFoundError: No module named seaborn", but the package seaborn is up to date

I'm running a simple code to draw a graph:
import seaborn as sns
sns.set(style="darkgrid")
# Load an example dataset with long-form data
fmri = sns.load_dataset("fmri")
# Plot the responses for different events and regions
sns.lineplot(x="timepoint", y="signal",
hue="region", style="event",
data=fmri)
and then I got the error message
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-b0fb62af2e98> in <module>
----> 1 import seaborn as sns
2 sns.set(style="darkgrid")
3
4 # Load an example dataset with long-form data
5 fmri = sns.load_dataset("fmri")
ModuleNotFoundError: No module named 'seaborn'
Here is the screenshot from my JupyterLab:
It's weir to me because I use Anaconda and the package seaborn is up to date.
Could you please inform me how to fix this code? Thank you so much!
I've just figured that I have two python distributions in my system, one from Anaconda (for which the package seaborn is already installed). The other one does not have the package seaborn. I open the JupyterLab from the latter one. This is where the problem arises.

sklearn doesn't have attribute 'datasets'

I have started using sckikit-learn for my work. So I was going through the tutorial which gives standard procedure to load some datasets:
$ python
>>> from sklearn import datasets
>>> iris = datasets.load_iris()
>>> digits = datasets.load_digits()
However, for my convenience, I tried loading the data in the following way:
In [1]: import sklearn
In [2]: iris = sklearn.datasets.load_iris()
However, this throws following error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-2-db77d2036db5> in <module>()
----> 1 iris = sklearn.datasets.load_iris()
AttributeError: 'module' object has no attribute 'datasets'
However, if I use the apparently similar method:
In [3]: from sklearn import datasets
In [4]: iris = datasets.load_iris()
It works without problem. In fact the following also works:
In [5]: iris = sklearn.datasets.load_iris()
I am completely confused about this. Am I missing something very trivial? What is the difference between the two approaches?
sklearn is a package. This answer said it very succinctly:
when you import a package, only variables/functions/classes in the __init__.py file of that package are directly visible, not sub-packages or modules.
datasets is a sub-package of sklearn. This is why this happens:
In [1]: import sklearn
In [2]: sklearn.datasets
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-2-325a2bfc35d0> in <module>()
----> 1 sklearn.datasets
AttributeError: module 'sklearn' has no attribute 'datasets'
However, the reason why this works:
In [3]: from sklearn import datasets
In [4]: sklearn.datasets
Out[4]: <module 'sklearn.datasets' from '/home/ethan/.virtualenvs/test3/lib/python3.5/site-packages/sklearn/datasets/__init__.py'>
is that when you load the sub-package datasets by doing from sklearn import datasets it is automatically added to the namespace of the package sklearn. This is one of the lesser-known "traps" of the Python import system.
Also, note that if you look at the __init__.py for sklearn you will see 'datasets' as a member of __all__, but this only allows you to do:
In [1]: from sklearn import *
In [2]: datasets
Out[2]: <module 'sklearn.datasets' from '/home/ethan/.virtualenvs/test3/lib/python3.5/site-packages/sklearn/datasets/__init__.py'>
One last point to note is that if you inspect either sklearn or datasets you will see that, although they are packages, their type is module. This is because all packages are considered modules - however, not all modules are packages.

Error when importing modules

I'm getting the following error when importing modules in python. I'm using jupyter notebook (python 2). I've searched through the internet but still can't quite figure out why. Any help would be so much appreciated.
Here's the code:
import numpy as np
from pandas import Series,DataFrame
import pandas as pd
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-e4e9959b303a> in <module>()
----> 1 import numpy as np
2 from pandas import Series,DataFrame
3 import pandas as pd
/Users/...filepath.../Python/data_analysis/numpy.pyc in <module>()
17
18 import numpy as np
---> 19 from numpy.random import randn
20
21
ImportError: No module named random
I've tried adding import random to the above code (before the other modules) and it still gives the same error. Could this be due to the version of gfortran on my system? I have version 4.9.2
Since I dont have complete code, just tried using import statements.
If we use np as per #John
import numpy as np
from np.random import randn
I am getting
from np.random import randn
ImportError: No module named np.random
I am not getting any error if I import randn from numpy.random
import numpy as np
from numpy.random import randn
print "randn1= ", randn()
from numpy.random import rand
print "rand1= ", rand()
Its working for me with output as below,
randn1= 0.147667079884
rand1= 0.243935746205
You can also try to use np.random.randn() and np.random.rand() directly.
import numpy as np
print "randn2= ", np.random.randn()
print "rand2= ", np.random.rand()
I get :
randn2= -0.22571513741
rand2= 0.486507681046

Categories

Resources