Ändern Sätze zu Vektoren mit while-Funktion in Python

Möchte ich ändern die folgenden Sätze zu Vektoren mit sklearn:

Article 1. It is not good to eat pizza after midnight
Article 2. I wouldn't survive a day withouth stackexchange
Article 3. All of these are just random phrases
Article 4. To prove if my experiment works.
Article 5. The red dog jumps over the lazy fox

Bekam ich den folgenden code:

from sklearn.feature_extraction.text import CountVectorizer
vectorizer = CountVectorizer(min_df=1)

n=0
while n < 5:
   n = n + 1
   a = ('Article %(number)s' % {'number': n})
   print(a)
   with open("LISR2.txt") as openfile:
     for line in openfile:
       if a in line:
           X=line
           print(vectorizer.fit_transform(X))

Gibt mir die folgende Fehlermeldung:

ValueError: Iterable over raw text documents expected, string object received.

Warum geschieht dies? Ich weiß, das sollte funktionieren, denn wenn ich in einzeln:

X=("It is not good to eat pizza","I wouldn't survive a day", "All of these")

print(vectorizer.fit_transform(X))

Er gibt mir meine gewünschten Vektoren.

(0, 8)  1
(0, 2)  1
(0, 11) 1
(0, 3)  1
(0, 6)  1
(0, 4)  1
(0, 5)  1
(1, 1)  1
(1, 9)  1
(1, 12) 1
(2, 10) 1
(2, 7)  1
(2, 0)  1
Schreibe einen Kommentar