I'm trying to import a series of modules into my Python 3.5 code. I use the following code to import:
# import packages for analysis and modeling
import pandas as pd # data frame operations; use pandas 0.18
from pandas.tools.rplot import RPlot, TrellisGrid, GeomPoint, \
ScaleRandomColour # trellis/lattice plotting
import numpy as np # arrays and math functions
from scipy.stats import uniform # for training-and-test split
import statsmodels.api as sm # statistical models (including regression)
import statsmodels.formula.api as smf # R-like model specification
import matplotlib.pyplot as plt # 2D plotting
When i use this code, I receive the following error:
ImportError Traceback (most recent call last)
/var/folders/zy/snhf2bh51v33ny6nf7fyr4wh0000gn/T/tmpdxMQ0Y.py in <module>()
7 # import packages for analysis and modeling
8 import pandas as pd # data frame operations; use pandas 0.18
----> 9 from pandas.tools.rplot import RPlot, TrellisGrid, GeomPoint, \
10 ScaleRandomColour # trellis/lattice plotting
11 import numpy as np # arrays and math functions
ImportError: No module named 'pandas.tools.rplot'
I tried this code with "pd" and with "pandas" written out. I confirmed that pandas was installed by manually typing in import pandas as pd and then confirming its existence by typing in "pd" and receiving the following message: <module 'pandas' from '/Users/me/Library/Enthought/Canopy/edm/envs/User/lib/python3.5/site-packages/pandas/__init__.py'>
What is causing this to happen?
Renaming it during import with as doesn't mean Python will be able to find the original module (pandas) when you use the name pd at a later import statement. Python will look for a module named pd which it will not find.
Since pd does not correspond to some module while pandas does, you'll need to use from pandas import tools in order to get it to work.
Related
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
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__)
The following code works-
import sklearn.linear_model
clf= sklearn.linear_model.LogisticRegressionCV()
The following code does not work-
import sklearn
clf= sklearn.linear_model.LogisticRegressionCV()
whereas in case of Numpy, the following also works
import numpy as np
np.random.randint()
Why is that? Please elaborate.
I am running a very simple Python script:
from tftb.generators import amgauss, fmlin
I get this error:
C:\Users\Anaconda3\envs\tf_gpu\lib\site-packages\tftb-0.0.1-py3.6.egg\tftb\processing\affine.py in <module>
12
13 import numpy as np
---> 14 from matplotlib.mlab import find
15 from scipy.signal import hilbert
16 from scipy.optimize import brenth, newton
ImportError: cannot import name 'find'
I believe find is no longer in versions >=3. How can I get around this without downgrading Matplotlib?
The code of the matplotlib.mlab.find function was literally
import numpy as np
def find(condition):
res, = np.nonzero(np.ravel(condition))
return res
You may replace any occurance with that function.
I'm writing code in a Jupyter Notebook that involves cleaning and analyzing a large amount of consumer data. I'm trying to use dill to save the dataframes with thousands of rows so I don't have to run the code every time I want to make an adjustment, so dill seems like the perfect package to do so... Except I'm getting this error when attempting to pickle the notebook:
AttributeError: module 'dill' has no attribute 'dump_session'
Let me know if the program code is necessary - I don't think it should make a difference. The imports are:
import numpy as np
import pandas as pd
import dill
import scipy
from matplotlib import pyplot as plt
from __future__ import division
from collections import OrderedDict
from sklearn.cluster import KMeans
pd.options.display.max_columns = None
and when I run this code I get the error from above:
dill.dump_session('recengine.db')
Is there another package that's interfering with dill's use of pickle vs. cpickle?