简体   繁体   中英

AttributeError: 'str' object has no attribute 'read'

list=[]
ct = 1      
import numpy as np

import os, os.path
isfile = os.path.isfile
join = os.path.join
fn = 'C:\\Users\\Keshav\\Desktop\\xyz\\data1\\black_and_white\\'
target = np.array([1, 2, 3, 4, 5])
num = sum(1 for item in os.listdir(fn) if isfile(join(fn, item)))



for ct in range(1,num+1):
    f = open(fn+"1_"+str(ct)+".dat","r") 
    list.append(f)
    ct = ct + 1

from sklearn.feature_extraction.text import CountVectorizer
count_vect = CountVectorizer(input="file")
X_train_counts = count_vect.fit_transform(list)

from sklearn.feature_extraction.text import TfidfTransformer
tfidf_transformer = TfidfTransformer()
X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)
#print X_train_tfidf.shape

from sklearn.naive_bayes import MultinomialNB
clf = MultinomialNB().fit(X_train_tfidf, target)
#clf.fit(X, y)
docs_new = ['10 years of marriage and now divorce. I just wasted my entire life too with her.']
X_new_counts = count_vect.transform(docs_new)
print X_new_counts

X_new_tfidf = tfidf_transformer.transform(X_new_counts)
predicted = clf.predict(X_new_tfidf)
print predicted

I am trying to build a multi-class classifier using sklearn using the following link .

The classifier used here is the Multinomial Naive Bayes Classifier.

I am getting the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Keshav\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 580, in runfile
    execfile(filename, namespace)
  File "C:/Users/Keshav/Desktop/iHeal/mturk-distortions/main1.py", line 40, in <module>
    X_new_counts = count_vect.transform(docs_new)
  File "C:\Users\Keshav\Anaconda\lib\site-packages\sklearn\feature_extraction\text.py", line 867, in transform
    _, X = self._count_vocab(raw_documents, fixed_vocab=True)
  File "C:\Users\Keshav\Anaconda\lib\site-packages\sklearn\feature_extraction\text.py", line 748, in _count_vocab
    for feature in analyze(doc):
  File "C:\Users\Keshav\Anaconda\lib\site-packages\sklearn\feature_extraction\text.py", line 234, in <lambda>
    tokenize(preprocess(self.decode(doc))), stop_words)
  File "C:\Users\Keshav\Anaconda\lib\site-packages\sklearn\feature_extraction\text.py", line 109, in decode
    doc = doc.read()
AttributeError: 'str' object has no attribute 'read'

Any idea how to resolve it?

docs_new = ['10 years of marriage and now divorce. I just wasted my entire life too with her.']

is a list of strings -- which is not what count_vect.transform wants; it wants a list of file-like objects with read methods.

So import StringIO at the top of your module and add

docs_new = [ StringIO.StringIO(x) for x in docs_new ]

right after you first assign to docs_new , and you shd be fine...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM