error= ImportError: cannot import name 'comb' - python

#ImportError: cannot import name 'comb'
import scipy
from scipy.misc import comb
# Loading the vectorizer and classfier
with open('classifier.pickle','rb') as f:
classifier = pickle.load(f)
with open('tfidfmodel.pickle','rb') as f:
vectorizer = pickle.load(f)
both the files classifier.pickle and tfid.pickle as kept in same folder

This has nothing to do with the location of classifier or vectorizer (tfid does not appear in your question).
This is an import error, due to the fact that comb was moved to scipy.special. This would've been easy to find by googling "scipy.misc.comb" and seeing the result "scipy.special.comb" fairly high.

Related

Why i am getting "name 'os' is not defined" error?

Why i am getting this error? it should be work.Probally i'm missing something from my sight.
Before that same thing happen the for Classes.I tried rewrite and still same.
import tensorflow as tf
import cv2
import os
import matplotlib.pyplot as plt
import numpy as np
img_array = cv2.imread("Training/0/Training_233976.jpg")
img_array.shape
plt.imshow(img_array)
Datadirectory = "Training/"
Classes = ["0","1","2","3","4","5","6"]
for category in Classes:
path = os.path.join(Datadirectory, category)
for img in os.listdir(path):
img_array = cv2.imread(os.path.join(path,img))
plt.imshow(cv2.cvtColor(img_array, cv2.COLOR_BGR2RGB))
plt.show()
break
break
I am formalizing in an answer, all you need to do is add a line at the top
import os
import os
use this at beginning of your code or where you are importing other libraries and codes .

Is there a way to remove the unwanted imports in python when object got serialzied

Imagine I have a.py:
import tensorflow as tf
class A:
name = "Class A"
dump.py:
import pickle
from a import A
a = A()
with open("a.pickled", "wb") as f:
f.write(pickle.dump(a))
load.py:
import pickle
with open("a.pickled", "rb") as f:
a = pickle.load(f.read())
When python pickles the object a, it actually also pulls in the tensorflow. As a result, when the object got deserialized in load.py, it imports tensorflow and brings in a bunch of expensive initialization related to tensorflow. I am wondering if we can remove the unwanted tensorflow import in dump.py without modifying a.py
I tried something like this in dumpy.py, but this doesn't work for me:
import pickle
from a import A
# Delete tensorflow related import from cache
import sys
names = [name for name in sys.modules.keys() if "tensorflow" in name]
for name in names:
del sys.modules[name]
a = A()
with open("a.pickled", "wb") as f:
f.write(pickle.dump(a))

how to read pickle file

I ran the following code as well as different version of same code but I keep running into this specific error: **ModuleNotFoundError: No module named 'pandas.core.internals.managers'; 'pandas.core.internals' is not a package
**
Please see code below:
import pickle
pickle_off = open(r"C:\Users\database.pkl","rb")
df = pd.read_pickle(pickle_off)
Let me know if this works:
import pickle
file_name = 'data.p'
with open(file_name,mode='rb') as f:
data = pickle.load(f)

Cifar - 10 / Unpickle

I am getting following error when i try to unpickle the cifar-10 dataset. I need to train a model but I can't even get the data for my operations. How can I fix this problem
dict=cPickle.load(fo)
UnpicklingError: invalid load key, '\x06'.
import tensorflow as tf
import os
import numpy as np
import dataset_class
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import glob
from PIL import Image
from scipy.spatial.distance import pdist
def cifar_10_reshape(batch_arg):
output=np.reshape(batch_arg,(10000,3,32,32)).transpose(0,2,3,1)
return output
def unpickle(file):
import _pickle as cPickle
fo=open(file,'rb')
dict=cPickle.load(fo)
fo.close()
return dict
#Loading cifar-10 data and reshaping it to be batch_sizex32x32x3
batch1=unpickle('cifar-10-batches-py/data_batch_1.bin')
batch2=unpickle('cifar-10-batches-py/data_batch_2.bin')
batch3=unpickle('cifar-10-batches-py/data_batch_3.bin')
batch4=unpickle('cifar-10-batches-py/data_batch_4.bin')
batch5=unpickle('cifar-10-batches-py/data_batch_5.bin')
batch1_data=cifar_10_reshape(batch1['data'])
batch2_data=cifar_10_reshape(batch2['data'])
batch3_data=cifar_10_reshape(batch3['data'])
batch4_data=cifar_10_reshape(batch4['data'])
batch5_data=cifar_10_reshape(batch5['data'])
batch1_labels=batch1['labels']
batch2_labels=batch2['labels']
batch3_labels=batch3['labels']
batch4_labels=batch4['labels']
batch5_labels=batch5['labels']
test_batch=unpickle('cifar-10-batches-py/test_batch')
test_images=cifar_10_reshape(test_batch['data'])
test_labels_data=test_batch['labels']
train_images=np.concatenate((batch1_data,batch2_data,batch3_data,batch4_data,batch5_data),axis=0)
train_labels_data=np.concatenate((batch1_labels,batch2_labels,batch3_labels,batch4_labels,batch5_labels),axis=0)
From what I have understood of the CIFAR-10 dataset, the version you are trying to unpickle is in a binary format, while you are not providing any information to the 'unpickler' about the encoding. You might have more luck trying the loading function provided on the CIFAR-10 website (https://www.cs.toronto.edu/~kriz/cifar.html) for python 3.x:
def unpickle(file):
import pickle
with open(file, 'rb') as fo:
dict = pickle.load(fo, encoding='bytes')
return dict

throws some error while loading a text file for data analysis last 2 line throws an error

throws some error while loading a text file for data analysis last 2 line throws an error
import numpy as np
f=open('decision_tree_data.txt','r')
x_train=[]
y_train=[]
for line in f:
line = np.asarray(line.split(),dtype=np.float32)
x_train.append(line[:-1])
y_train.append(line[:-1])
x_train = np.asmatrix(x_train)
y_train = np.reshape(y_train,(len(y_train),1))
Check indentation of your code.Following code successfully compiles for me and also make sure you have installed the numpy module.
import numpy as np
f=open('decision_tree_data.txt','r')
x_train=[]
y_train=[]
for line in f:
line=np.asarray(line.split(),dtype=np.float32)
x_train.append(line[:-1])
y_train.append(line[-1])
x_train=np.asmatrix(x_train)
y_train=np.reshape(y_train,(len(y_train),1))

Categories