How to import special.hermite from scipy? - python

I'm looking to import a hermite polynomial. When I check the documentation on the scipy website it's available. However, when I try to import it there is no module found

import scipy.special
p = scipy.special.hermite(63)
should work.

Related

Can I import modules in python using a function

I am working on a small library and I need to know can I import modules like numpy, sklearn and etc. Using functions. For example:
def ml():
import numpy as np
import pandas as pd
x = np.array([1,2,647,345,3,7,3,8,36,64])
Is this possible ?
Simply can I import a module using a function and then use that later outside the function
The main idea is when the user calls the function ml he has all the modules related to machine learning imported and then he can use them. X = np.array was just kind of an example.
UPDATED
This should work
import importlib
def importmd(modulex):
return importlib.import_module(modulex) #Returning the module
np = importmd("numpy") #Same as import numpy as np

Calling librosa.grifflim returns an attribute error

In the following code I'm getting errors when trying to call librosa.grifflim, telling me the attribute does not exist.
import os
from matplotlib import pyplot as plt
import librosa
import librosa.display
import IPython.display as ipd
import numpy as np
import cv2
S = cv2.imread('spectrograms/CantinaBand60.wav10.jpg')
D = librosa.amplitude_to_db(np.abs(S), ref=np.max)
signal = librosa.griffinlim(D)
sf.write('test.wav', signal, 352000)
I've upgraded librosa, and I still encounter the error. The documentation page for this function no longer seems to exist either. I've also tried import just that module using librosa.griffinlim but it continues to tell me this module doesn't exist. Was this function removed during a recent version? If so, is there another function I can use to apply the griffin lim algorithm?
librosa.griffinlim was introduced in librosa 0.7.0. So you need to have that version or later. You can check this using the following code.
import librosa; print(librosa.__version__)

Python - cannot import `linalg`

I have this code
import scipy.sparse as sparse
import numpy as np
id = np.eye(13)
vals, vecs = sparse.linalg.eigsh(id, k=6)
vals
which is just the example code from the documentation here.
I am running it in a Python 2.7 console and I get the following error message:
AttributeError: 'module' object has no attribute 'linalg'
Does anyone know why this happens?
Try this code
import scipy.sparse.linalg as sp
import numpy as np
id = np.eye(13)
vals, vecs = sp.eigsh(id, k=6)
vals
This happens because linalg is a directory and not source code i.e it is a sub-package. And I guess this causes the issue because some of the Scipy sub modules do not have __init__.py, Maybe the devs did this to reduce loading times of top-level packages. You can find this information in Scipy Organization section in this link

Can not access ConvexHull in Scipy 0.16.1

I was able to use the ConvexHull function in previous versions of Scipy. The function, in the version I had, would not provide the Volume of the convex hull so I decided to install the new version of Scipy. But after installing Scipy 0.16.1 I noticed that the Spatial library is rearranged and now I get an error when trying to call ConvexHull function.
This code would work in previous versions of Scipy:
import numpy as np
import scipy as sp
X = np.random.randint(0,200,(30,2))
hull = sp.spatial.qhull.Delaunay(X).convex_hull
Now, for the code above, I get the error:
AttribureError: 'module' object has no attribute 'spatial'.
It seems there is nothing inside .qhull.
I have checked this in VS,Spider,Ubuntu
You have to explicitly import the spatial subpackage. This should work:
import numpy as np
import scipy as sp
import scipy.spatial
X = np.random.randint(0,200,(30,2))
hull = sp.spatial.qhull.Delaunay(X).convex_hull

Error importing scipy.special.expit

I want to import the sigmoid function available at scipy.special.expit.
If I use import scipy.special.expit as sigmoid (or without the "as sigmoid") I get a "No module called expit" error.
If I use from scipy import special, then using special.expit(N) works.
If I use from scipy import special.expit as sigmoid, invalid syntax at special.expit (the dot).
If I use from scipy.special import expit, special is not defined error.
So... how the hell can I import the expitfunction assigning the "sigmoid" alias?
As I noted in a comment,
from scipy.special import expit
should work. To import expit with the name sigmoid, use
from scipy.special import expit as sigmoid

Categories