Issue with inverting sparse matrix pylab - matplotlib

I try to do the following
from scipy import *
from numpy import *
import scipy as s
import numpy as np
import math
import scipy.sparse as l
from plot import Graph3DSolution
import numpy.linalg as lin
currentSol=s.sparse.linalg.inv(I-C)*A*lastSol
Im missing out some code but the issue is this
Traceback (most recent call last):
File "explict1wave.py", line 62, in <module>
currentSol=s.sparse.linalg.inv(I-C)*A*lastSol
AttributeError: 'module' object has no attribute 'linalg'
Python 2.7.6 |Anaconda 1.9.1 (x86_64)| (default, Jan 10 2014, 11:23:15)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
im>>> import scipy
>>> scipy.__version__
'0.14.0'
>>>
I look up the documentation and it seems these libraries existed since .12 . I dont know what the issue is, but im sure its something simple im not seeing.

>>> import scipy as s
>>> s.sparse
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'sparse'
>>>
>>> from scipy.sparse import linalg
>>> linalg.inv
<function inv at 0x19b1758>
>>>
General recommendations for importing functions from scipy .
On a side note, best avoid star imports. These from scipy import *, from numpy import * are not recommended and not needed here. Same for import scipy as s.

Related

cannot import name 'int' from 'numpy'

I was just getting started with PyCharm and python for statistics.
And I got this error:
ImportError: cannot import name 'int' from 'numpy' (/home/tetiana/.local/lib/python3.8/site-packages/numpy/init.py)
Full traceback looks like this:
Traceback (most recent call last):
File "/home/tetiana/forVScode/python/first/first_try.py", line 1, in
from scipy import stats
File "/usr/lib/python3/dist-packages/scipy/stats/init.py", line 379, in
from .stats import *
File "/usr/lib/python3/dist-packages/scipy/stats/stats.py", line 180, in
import scipy.special as special
File "/usr/lib/python3/dist-packages/scipy/special/init.py", line 643, in
from .basic import *
File "/usr/lib/python3/dist-packages/scipy/special/basic.py", line 19, in
from . import orthogonal
File "/usr/lib/python3/dist-packages/scipy/special/orthogonal.py", line 81, in
from numpy import (exp, inf, pi, sqrt, floor, sin, cos, around, int,
ImportError: cannot import name 'int' from 'numpy' (/home/tetiana/.local/lib/python3.8/site-packages/numpy/init.py)
Process finished with exit code 1
How can I fix it?
Here is my code:
from scipy import stats
import pandas as pd
state = pd.read_csv('state_murder_rate_test_table.csv')
state['Population'].mean()
stats.trim_mean(state['Population'], 0.1)
state['Population'].median()
I checked whether the Python versions in os and in the project match and they are. I have python 3.8.10 and my os is Ubuntu 20.04
Referring to the current numpy documentation, there exists no type called numpy.int that you can import. I believe that the type you wanna import is numpy.integer or numpy.int_.
The code you provided does not have any statement like: from numpy import int. If you could provide a full traceback error, it'll be easier to see where the error stems from.
I hope this answer will be somewhat useful.

ImportError: cannot import name '_backports' from 'matplotlib.cbook'

---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_22516\254480426.py in <module>
2 import matplotlib.pyplot as plt
3 from mpl_toolkits.mplot3d import Axes3D, art3d # NOQA
----> 4 from matplotlib.cbook import _backports
5 from collections import defaultdict
6 import types
ImportError: cannot import name '_backports' from 'matplotlib.cbook' (C:\Users\saidt\anaconda3\envs\tensorflow\lib\site-packages\matplotlib\cbook\__init__.py)
I cannot solve this problem. I tried several ways advised on other posts (uninstalled, reinstalled, changed the version of matplotlib) but still have this issue. I even checked matplotlib.cbook (https://matplotlib.org/stable/api/cbook_api.html) but there is no _backports. I wonder where to find the solution. If someone could help, I would really appreciate it.

ImportError: cannot import name 'get_versions' from 'matplotlib._version'

When doing my imports:
import cv2
from matplotlib import pyplot as plt
import numpy as np
import opencv_wrapper as cvw
I get:
ImportError Traceback (most recent call last)
/var/folders/bw/fb7g3vhj2ln41zrg3v5ynnn40000gn/T/ipykernel_96862/883562467.py in <module>
1 # import the necessary packages
2 import cv2
----> 3 from matplotlib import pyplot as plt
4 import numpy as np
5 import opencv_wrapper as cvw
/opt/anaconda3/envs/py37/lib/python3.7/site-packages/matplotlib/__init__.py in <module>
114 # Get the version from the _version.py versioneer file. For a git checkout,
115 # this is computed based on the number of commits since the last tag.
--> 116 from ._version import get_versions
117 __version__ = str(get_versions()['version'])
118 del get_versions
ImportError: cannot import name 'get_versions' from 'matplotlib._version' (/opt/anaconda3/envs/py37/lib/python3.7/site-packages/matplotlib/_version.py)
I'm using:
matplotlib 3.4.3
numpy 1.16.2
opencv-python 4.0.0.21
opencv-wrapper 0.2.3
python 3.7.13
Due to the opencv wrapper these are the versions I need to use.
I've tried to uninstall and reinstall but the error persists. Any help will be appreciated.

metpy.plots issue with Cartopy Natural Earth

I just noticed that when I try to import metpy.plots on NCAR's Cheyenne supercomputer, it loads and works fine when using metpy 0.10.0 (with Cartopy 0.17.0), but I get an error with metpy 0.12.0 or 0.12.1 (with Cartopy 0.18.0b2). This is the error I get:
(NPL) jaredlee#cheyenne3:~> python3
Python 3.6.8 (default, Jun 27 2019, 20:02:05)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import metpy
>>> metpy.__version__
'0.12.1'
>>> import metpy.plots as mpplots
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/glade/work/jaredlee/python/my_npl_clone_20200417/lib/python3.6/site-packages/metpy/plots/__init__.py", line 27, in <module>
from .cartopy_utils import USCOUNTIES, USSTATES
File "/glade/work/jaredlee/python/my_npl_clone_20200417/lib/python3.6/site-packages/metpy/plots/cartopy_utils.py", line 43, in <module>
USCOUNTIES = MetPyMapFeature('us_counties', '20m', facecolor='None', edgecolor='black')
File "/glade/work/jaredlee/python/my_npl_clone_20200417/lib/python3.6/site-packages/metpy/plots/cartopy_utils.py", line 16, in __init__
super().__init__('', name, scale, **kwargs)
File "/glade/work/jaredlee/python/my_npl_clone_20200417/lib/python3.6/site-packages/cartopy/feature/__init__.py", line 264, in __init__
self._validate_scale()
File "/glade/work/jaredlee/python/my_npl_clone_20200417/lib/python3.6/site-packages/cartopy/feature/__init__.py", line 274, in _validate_scale
'Valid scales are "110m", "50m", and "10m".'
ValueError: 20m is not a valid Natural Earth scale. Valid scales are "110m", "50m", and "10m".
>>>
If I manually change cartopy_utils.py in my local copy of metpy so that the USCOUNTIES and USSTATES function calls to MetPyMapFeature use '10m' instead of '20m', then the error goes away. Is this '20m' a bug in cartopy_utils.py? Natural Earth's webpage advertises that it provides data at 1:10m, 1:50m, and 1:110m scales.
MetPy <=0.12.1 is incompatible with CartoPy 0.18. As pointed out by Daryl in his comment, this is an open issue that will hopefully be resolved soon.

where is the scipy.optimize function or module? [duplicate]

I get an errr when using scipy.stats. in a script after importing scipy.
AttributeError: 'module' object has no attribute 'stats'
Within script editor I can click on stats after typing scipy. from the pulldown menu,
within python console I can not select python.stats from the pulldown menu, it's not there.
I'm using pandas 2.7 and SciPy 0.13.0
Why is that?
Any known issues?
expanding on my comment (to have a listed answer).
Scipy, as many other large packages, doesn't import all modules automatically. If we want to use the subpackages of scipy, then we need to import them directly.
However, some scipy subpackages load other scipy subpackages, so for example importing scipy.stats also imports a large number of the other packages. But I never rely on this to have the subpackage available in the namespace.
In many packages that use scipy, the preferred pattern is to import the subpackages to have them available by their names, for example:
>>> from scipy import stats, optimize, interpolate
>>> import scipy
>>> scipy.stats
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'stats'
>>> scipy.optimize
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'optimize'
>>> import scipy.stats
>>> scipy.optimize
<module 'scipy.optimize' from 'C:\Python26\lib\site-packages\scipy\optimize\__init__.pyc'>
This is expected. Most of the subpackages are not imported when you just do import scipy. There are a lot of them, with a lot of heavy extension modules that take time to load. You should always explicitly import the subpackages that you want to use.
https://github.com/scipy/scipy/issues/13618
if you import scipy alone like this:
import scipy
then you use:
scipy.stats
You will get:
AttributeError: module 'scipy' has no attribute 'stats'
You have to import like this:
import scipy.stats
or
import scipy
import stats