我正在按照教程 https://github.com/amueller/introduction_to_ml_with_python/blob/master/07-working-with-text-data.ipynb 瞭解機器學習和文本。使用sklearn的解碼/編碼load_files
就我而言,我正在使用我下載的推文,在他們正在使用的完全相同的目錄結構(嘗試學習情感分析)中使用正面和負面的推文。
在這裏,在IPython的筆記本我打開我的數據,就像他們做的事:
tweets_train =load_files('Path to my training Tweets')
然後我嘗試用CountVectorizer適合他們
vect = CountVectorizer().fit(text_train)
我得到
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd8 in position 561: invalid continuation byte
這是因爲我的Tweets中有各種非標準文字嗎?我沒有做我的鳴叫的任何清理(我假設有一些與幫助,以使單詞一袋工作庫?)
編輯:
:我用用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以外的編碼存儲數據,要麼數據以某種方式損壞。請提供有關如何下載並將推文保存到磁盤的詳細信息(=代碼)。 – lenz
請參閱編輯代碼以下載推文。 –
你也可以顯示你如何從'tweets_train'到'text_train'? – lenz