'DecisionTreeClassifier' object has no attribute 'export_graphviz' - python

I'm using python sklearn RandomForestClassifier and try to export decisiontrees.
The basic code is as following:
from sklearn import tree
with open(dot_file_name, 'w') as my_file:
tree.export_graphviz(tree1, out_file = my_file,feature_names = feature_names)
After run the python script, following error show up:
Attribute error: 'DecisionTreeClassifier' object has no attribute 'export_graphviz'
I'm using python 2.7. Is it because of the version of python? Do I have to use python 3.0?

That's because you used somewhere tree name as name for DecisionTreeClassifier. Use another name there.

Related

Can't unpickle a file in 3.6 Python

I had a pandas df a while back in 3.10 that I put into a .pkl pickle file. Because one of my modules is only available in 3.6, I had to revert the rest back to 3.6. However, I can't do pandas.read_pickle("file.pkl") now because my older pandas can't handle the newer file.
I'm getting an error like this:
AttributeError: Can't get attribute '_unpickle_block' on <module 'pandas._libs.internals' from '/opt/mambaforge/envs/...
What I tried to do as a workaround is to load the file in the new python 3.10 and then write to another file using PROTOCOL=4 because 4 is the protocol used in python 3.6.
Here's my code:
df = pd.read_pickle('file3ten.pkl')
import pickle5 as pickle
with open(path_to_protocol5, "rb") as fh:
data = pickle.load(fh)
import pickle
pickle.HIGHEST_PROTOCOL=4
with open("file3six.pkl", 'wb') as pfile:
pickle.dump(df, pfile, protocol=pickle.HIGHEST_PROTOCOL)
Now I thought that I would be able to do pd.read_pickle('file3six.pkl') because the protocol was reduced, but I just got a chained error, saying:
AttributeError: module 'pandas._libs.internals' has no attribute '_unpickle_block'
During handling of the above exception, another exception occurred:
...
AttributeError: Can't get attribute '_unpickle_block' on <module 'pandas._libs.internals' from '/opt/mambaforge/envs/
If anyone knows what do to fix this, I'd appreciate it a lot

name 'cPickle' is not defined

I am trying to run a code on colab given below:
vocab_file = 'vocabs/vocab_train.pkl'
vocab = utils.load_variables(vocab_file)
it calls a function load_variables
and here's a file of that function calling
import pickle as cPickle
def load_variables(pickle_file_name):
if os.path.exists(pickle_file_name):
with open(pickle_file_name, 'rb') as f:
d = cPickle.load(f)
return d
here's the error it is throwing
I tried changing it to "import pickle/import cPickle" as well but it keeps on showing same error.
cPickle was replaced by pickle in Python 3. You're likely using a Python 2.7 interpreter instead of a Python 3 one.

What am I doing wrong when trying to use easygui.enterbox(...)?

I am three weeks into my independent study Python course, and am having trouble getting passed an issue. I am trying to use the function enterbox() from easygui I write:
import easygui
value = easygui.enterbox("say something")
And when I run it I get the error:
**AttributeError: 'module' object has no attribute 'enterbox'
Does anyone know why this may be happening? Thanks! (I'm using version 2.7)

'module' object has no attribute 'cmap' in cubehelix python

I'm trying to use cubehelix in python however I've been getting simple problems which I don't think should be showing up. The code I'm using is the following:
import matplotlib.pyplot as plt
import numpy as np
import cubehelix
cx1 = cubehelix.cmap(reverse=True)
alplot = plt.imshow(rtotal_rotated,vmax=1100,extent[-21,19,23,-17],cmap=cx1)
However the following error comes up when I run the code:
AttributeError: 'module' object has no attribute 'cmap'
I know this can't be right since I'm simply following the code from this tutorial
http://www.ifweassume.com/2014/04/cubehelix-colormap-for-python.html
So I'm not sure why it's breaking.
Very like that the script imports itself, i.e. your script is named cubehelix.py. So, if you have file named cubehelix.py in your current working directory, rename it to my_cubehelix.py and try again.

skimage ImageCollection and Python3

I am having trouble using the skimage ImageCollection (http://scikit-image.org/docs/dev/api/skimage.io.html#imagecollection) object in Python3. For example, in python 2,7, I was using it as follows:
import skimage.io as io
files = io.ImageCollection('/home/luca/sequence/*.*)
method_do_something(files[0])
This worked fine. When I use it in Python 3.4, the creation of the ImageCollection object works fine and len(files) returns the correct amount of files.
However, when I try the files[0] call, I get the following error:
AttributeError: 'builtin_function_or_method' object has no attribute 'keys'
I was wondering if anyone knows of a workaround that might work on both 2.7 and python 3.x

Categories

Resources