How to import single function from packages with the same name - python

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.

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.

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

Pandas/Numpy Errors in Eclipse

I am working in python and eclipse for the first time and i am having troubles. This is the code I have now
from pandas import pandas as pd
from numpy import numpy as np
update = pd.read_csv("PPList.txt")
My error says that the read_csv is an undefined from import
My previous searches indicates that i need to change from numpy import numpy as np to just import numpy *
When I do that, the * has an error saying that it's expecting a comma or colon.
The imports should be
import pandas as pd
import numpy as np
Or
from pandas import *
from numpy import *

Is the fact that scipy.fft links to numpy.fft.fftpack.fft documented anywhere?

I couldn't find any official documentation of the fact that scipy.fft actually is a link to numpy.fft.fftpack.fft. Here is an iPython session showing the link:
In [1]: import scipy
In [2]: import numpy
In [3]: scipy.__version__
Out[3]: '0.19.0'
In [4]: numpy.__version__
Out[4]: '1.12.1'
In [5]: scipy.fft
Out[5]: <function numpy.fft.fftpack.fft>
The only mentions I could find of a scipy.fft submodule were this discussion on numpy-discussion and this discussion on SciPy's Github, both of which seem to hint at the fact that no such submodule actually existed at the time.
Yes, it's documented:
Guidelines for importing functions from Scipy
The scipy namespace itself only contains functions imported from numpy. These functions still exist for backwards compatibility, but should be imported from numpy directly.
[...]
The scipy.__init__.py file has:
from numpy import *
from numpy.random import rand, randn
from numpy.fft import fft, ifft
from numpy.lib.scimath import *
__all__ += _num.__all__
__all__ += ['randn', 'rand', 'fft', 'ifft']

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)

Categories

Resources