Метод принимает генераторы просто отлично.Но для этого требуется итерация необработанных документов , то есть строк .Ваш генератор представляет собой итерацию из numpy.ndarray
объектов.Так что попробуйте что-то вроде:
def ChunkIterator(filename):
for chunk in pd.read_csv(csvfilename, chunksize=1):
for document in chunk['text_column'].values:
yield document
Обратите внимание, я не очень понимаю, почему вы здесь используете панд.Просто используйте обычный модуль csv
, например:
import csv
def doc_generator(filepath, textcol=0, skipheader=True):
with open(filepath) as f:
reader = csv.reader(f)
if skipheader:
next(reader, None)
for row in reader:
yield row[textcol]
Итак, в вашем случае, передайте 1
в textcol, например:
In [1]: from sklearn.feature_extraction.text import TfidfVectorizer
In [2]: import csv
...: def doc_generator(filepath, textcol=0, skipheader=True):
...: with open(filepath) as f:
...: reader = csv.reader(f)
...: if skipheader:
...: next(reader, None)
...: for row in reader:
...: yield row[textcol]
...:
In [3]: vectorizer = TfidfVectorizer()
In [4]: result = vectorizer.fit_transform(doc_generator('testing.csv', textcol=1))
In [5]: result
Out[5]:
<4x9 sparse matrix of type '<class 'numpy.float64'>'
with 21 stored elements in Compressed Sparse Row format>
In [6]: result.todense()
Out[6]:
matrix([[ 0. , 0.46979139, 0.58028582, 0.38408524, 0. ,
0. , 0.38408524, 0. , 0.38408524],
[ 0. , 0.6876236 , 0. , 0.28108867, 0. ,
0.53864762, 0.28108867, 0. , 0.28108867],
[ 0.51184851, 0. , 0. , 0.26710379, 0.51184851,
0. , 0.26710379, 0.51184851, 0.26710379],
[ 0. , 0.46979139, 0.58028582, 0.38408524, 0. ,
0. , 0.38408524, 0. , 0.38408524]])