【发布时间】:2017-10-26 20:15:42
【问题描述】:
我在这里学习教程 https://github.com/amueller/introduction_to_ml_with_python/blob/master/07-working-with-text-data.ipynb 了解机器学习和文本。
就我而言,我使用的是我下载的推文,正面和负面推文的目录结构完全相同(试图学习情绪分析)。
在 iPython Notebook 中,我像他们一样加载我的数据:
tweets_train =load_files('Path to my training Tweets')
然后我尝试用 CountVectorizer 来拟合它们
vect = CountVectorizer().fit(text_train)
我明白了
UnicodeDecodeError: 'utf-8' 编解码器无法解码位置的字节 0xd8 561: 无效的继续字节
这是因为我的推文中有各种非标准文本吗?我没有对我的推文进行任何清理(我假设有一些库可以帮助我处理这些问题,以便让一堆单词起作用?)
编辑: 我使用 Twython 下载推文的代码:
def get_tweets(user):
twitter = Twython(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET)
user_timeline = twitter.get_user_timeline(screen_name=user,count=1)
lis = user_timeline[0]['id']
lis = [lis]
for i in range(0, 16): ## iterate through all tweets
## tweet extract method with the last list item as the max_id
user_timeline = twitter.get_user_timeline(screen_name=user,
count=200, include_retweets=False, max_id=lis[-1])
for tweet in user_timeline:
lis.append(tweet['id']) ## append tweet id's
text = str(tweet['text']).replace("'", "")
text_file = open(user, "a")
text_file.write(text)
text_file.close()
【问题讨论】:
-
这意味着您要么使用 UTF-8 以外的编码存储数据,要么数据已以某种方式损坏。请提供有关您如何下载推文并将推文保存到磁盘的详细信息(= 代码)。
-
查看编辑以获取下载推文的代码。
-
你能不能也展示一下你是如何从
tweets_train到text_train的? -
与链接的 iPython Notebook 中的示例相同。
标签: python-3.x scikit-learn nlp countvectorizer