DeprecationWarning rasied in Spyder for every Scipy function used - python

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()

Related

Scipy strange import behaviour

Scipy has an different way of handling submodules to Numpy, for example
import scipy as sp
import numpy as np
A = np.eye(4)
np.linalg.det(A)
sp.linalg.det(A)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'scipy' has no attribute 'linalg'
This is moderately annoying because of the asymmetry with respect to Numpy, but it is exactly the behaviour that the documentation describes. The proper usage according to the docs is
from scipy import linalg
import numpy as np
A = np.eye(4)
np.linalg.det(A)
linalg.det(A) # using Scipy
which works just fine.
Now, here's the weird thing
import scipy as sp
import numpy as np
from scipy.linalg import expm # extra line inserted into first example
A = np.eye(4)
np.linalg.det(A)
sp.linalg.det(A)
then the Numpy-style code works just fine. The extra line causes linalg to be added to the namespace sp, a side effect of the extra import.
I get the programming pattern I want but the third line is not easy to explain in example code.
QUESTION: why does Scipy do this? is there any more straightforward way to have it so that Scipy behaves more Numpyish?
In fact, you almost never need or to want import scipy as sp or anything like that.
There is almost nothing in the top level scipy namespace. All useful stuff is in subpackages (one exception is LowLevelCallable which is in the top-level namespace). This way, users are better off either importing from subpackages, from scipy.signal import detrend, or importing subpackages themselves (from scipy import signal; signal.detrend(...)).
As to the disparity with numpy, numpy is very much the opposite: a lot of useful stuff is in the top-level namespace, so you import it from there.
Unless you're using np.linalg, np.random, np.fft or np.testing, which are public-facing usable submodules.

Cannot import name 'threshold' from scipy.stats

I am trying to use function threshold from scipy package.
from scipy.stats import threshold
I_t=threshold(I, threshmin=2, threshmax=400, newval=-1) # I is an array containing image data
However, i am getting an error message saying
cannot import name 'threshold' from 'scipy.stats'
The function threshold was deprecated in SciPy 0.17.0 and removed from SciPy 1.0.0. Use numpy.clip (with arguments adjusted appropriately) instead.

How to import single function from packages with the same name

What should I do about this? It seems python is calling the first function.
from numpy.random import multivariate_normal
from scipy.stats import multivariate_normal
The usual convention is:
import numpy as np
np.random.multivariate_normal
Then there won't be such collisions.

How can I use numpy functions within NumbaPro functions?

I am new to CUDA, I am trying to use NumbaPro's cuda extension to compute some exponentials, however I cannot seem to use numpy within the cuda complied functions? I know i am doing something silly, but I cannot figure it out.
import numpy as np
from pylab import imshow, show, plot
from timeit import default_timer as timer
from numbapro import cuda
from numba import *
import math
def Deng_test(xx,Expre):
ExTest = (np.abs(np.sum(Expre)))**2
return ExTest
Deng_test_gpu = cuda.jit(argtypes=(double,complex128), restype=float64, device=True, inline=True)(Deng_test)

scipy equivalent for MATLAB spy

I have been porting code for an isomap algorithm from MATLAB to Python. I am trying to visualize the sparsity pattern using the spy function.
MATLAB command:
spy(sparse(A));
drawnow;
Python command:
matplotlib.pyplot.spy(scipy.sparse.csr_matrix(A))
plt.show()
I am not able to reproduce the MATLAB result in Python using the above command. Using the command with only A in non-sparse format gives quite similar result to MATLAB. But it's taking quite long (A being 2000-by-2000). What would be the MATLAB equivalent of a sparse function for scipy?
Maybe it's your version of matplotlib that makes trouble, as for me scipy.sparse and matplotlib.pylab work well together.
See sample code below that produces the 'spy' plot attached.
import matplotlib.pylab as plt
import scipy.sparse as sps
A = sps.rand(10000,10000, density=0.00001)
M = sps.csr_matrix(A)
plt.spy(M)
plt.show()
# Returns here '1.3.0'
matplotlib.__version__
This gives this plot:
I just released betterspy, which arguably does a better job here. Install with
pip install betterspy
and run with
import betterspy
from scipy import sparse
A = sparse.rand(20, 20, density=0.1)
betterspy.show(A)
betterspy.write_png("out.png", A)
With smaller markers:
import matplotlib.pylab as pl
import scipy.sparse as sps
import scipy.io
import sys
A=scipy.io.mmread(sys.argv[1])
pl.spy(A,precision=0.01, markersize=1)
pl.show()

Categories

Resources