我正在使用scikit-learn python进行情感分析,现在我正在使用nltk来进行单词排序,以提高处理速度,例如:
经过nltk处理后得到以下数组:
array([ ['Really', 'a', 'terrible', 'course', u'lecture', u'be', 'so', 'boring', 'i', u'contemplate', 'suicide', 'on', 'numerous', u'occasion', 'and', 'the', 'tutes', u'go', 'for', 'two', u'hour', 'and', u'be', 'completely'], ['Management', 'accounting', u'require', 'sufficient', 'practice', 'to', 'get', 'a', 'hang', 'of', 'Made', 'easier', 'with', 'a', 'great', 'lecturer']], dtype=object)
但是scklearn要求数组是
array([ 'Really a terrible course lectures were so boring i contemplated suicide on numerous occasions and the tutes went for two hours and were completely ', 'Management accounting requires sufficient practice to get a hang of Made easier with a great lecturer '],dtype=object)
那么把这个数组转换成正确形式的最好方法是什么呢?我尝试使用联合列表,但结果很奇怪
你可以这样做:
second_array = [' '.join(each) for each in first_array]
或者你可以告诉sklearn.CountVectorizer
只使用你的令牌:
vect = CountVectorizer(preprocessor=lambda x: x, tokenizer=lambda x: x)
X = vect.fit_transform(first_array)