Python 分类阿拉伯语数据集中的 UnicodeDecodeError

哈立德

我有使用 Python 进行分类的阿拉伯语数据集;Twitter 目录中的两个目录(负面和正面)。

我想使用 Python 类对数据进行分类。当我运行附加的代码时,出现此错误:

> 文件“C:\Users\DEV2016\Anaconda2\lib\encodings\utf_8.py”,第 16 行,解码返回 codecs.utf_8_decode(input, errors, True)

UnicodeDecodeError: 'utf8' 编解码器无法解码位置 0 的字节 0xc7: 无效的继续字节

import sklearn.datasets
import sklearn.metrics
import sklearn.cross_validation
import sklearn.svm
import sklearn.naive_bayes
import sklearn.neighbors

dir_path = "E:\Twitter\Twitter"

# Loading files into memory
files = sklearn.datasets.load_files(dir_path)

# Calculating BOW
count_vector = sklearn.feature_extraction.text.CountVectorizer()
word_counts=count_vector.fit_transform(files.data)

# Calculating TFIDF
tf_transformer = sklearn.feature_extraction.text.TfidfTransformer(use_idf=True).fit(word_counts)
X = tf_transformer.transform(word_counts)

# Create classifier
# clf = sklearn.naive_bayes.MultinomialNB()
# clf = sklearn.svm.LinearSVC()
n_neighbors = 11
weights = 'distance'
clf = sklearn.neighbors.KNeighborsClassifier(n_neighbors, weights=weights)

# Test the classifier
# Train-test split
test_size=0.4
X_train, X_test, y_train, y_test = sklearn.cross_validation.train_test_split(X, files.target, test_size=test_size)

# Test classifier
clf.fit(X_train, y_train)
y_predicted = clf.predict(X_test)
print (sklearn.metrics.classification_report(y_test, y_predicted,
target_names=files.target_names))
print ('Confusion Matrix:')
print (sklearn.metrics.confusion_matrix(y_test, y_predicted))

追溯

File "<ipython-input-19-8ea269fd9c3d>", line 1, in <module>
runfile('C:/Users/DEV2016/.spyder/clf.py', wdir='C:/Users/DEV2016/.spyder')

File "C:\Users\DEV2016\Anaconda2\lib\site-
packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)

File "C:\Users\DEV2016\Anaconda2\lib\site-
packages\spyder\utils\site\sitecustomize.py", line 87, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)

File "C:/Users/DEV2016/.spyder/clf.py", line 18, in <module>
word_counts=count_vector.fit_transform(files.data)

File "C:\Users\DEV2016\Anaconda2\lib\site-
packages\sklearn\feature_extraction\text.py", line 869, in fit_transform
self.fixed_vocabulary_)

File "C:\Users\DEV2016\Anaconda2\lib\site-
packages\sklearn\feature_extraction\text.py", line 792, in _count_vocab
for feature in analyze(doc):

File "C:\Users\DEV2016\Anaconda2\lib\site-
packages\sklearn\feature_extraction\text.py", line 266, in <lambda>
tokenize(preprocess(self.decode(doc))), stop_words)

File "C:\Users\DEV2016\Anaconda2\lib\site-
packages\sklearn\feature_extraction\text.py", line 116, in decode
doc = doc.decode(self.encoding, self.decode_error)

File "C:\Users\DEV2016\Anaconda2\lib\encodings\utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)

UnicodeDecodeError: 'utf8' codec can't decode byte 0xc7 in position 0:
invalid continuation byte
同仁堂

在您尝试加载的 Twitter 数据中,存在 utf-8 无法识别的字符。尝试使用其他编码格式加载它,例如

files = sklearn.datasets.load_files(dir_path, encoding="iso-8859-1")

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在python中导入阿拉伯语Wordnet

来自分类Dev

在python 2.7中打印阿拉伯语/波斯语字母

来自分类Dev

python3中波斯语或阿拉伯语的unicode和编码

来自分类Dev

在python 2.7中打印阿拉伯语/波斯语字母

来自分类Dev

无法使用Python将阿拉伯语解码的Unicode保存到CSV文件

来自分类Dev

Python中的阿拉伯语Word Net同义词?

来自分类Dev

Python将阿拉伯语存储在数组中吗?

来自分类Dev

如何使用 Python 读取包含阿拉伯语行的 CSV 文件

来自分类Dev

python 2.7中阿拉伯语单词的正则表达式

来自分类Dev

通过 Python tcp 套接字传输以阿拉伯语命名的文件时出现 ValueError

来自分类Dev

无法在phpmyadmin中输入阿拉伯语数据

来自分类Dev

iOS阿拉伯语Pushnotification

来自分类Dev

iOS阿拉伯语Pushnotification

来自分类Dev

笔尖镜像阿拉伯语

来自分类Dev

Redis阿拉伯语解码

来自分类Dev

在MySQL数据库中存储波斯语\阿拉伯语文本

来自分类Dev

PostgreSQL数据库中的阿拉伯语和英语文本

来自分类Dev

php mysql在数据库中插入阿拉伯语单词

来自分类Dev

如何在MYSQL中选择阿拉伯语数据?

来自分类Dev

PostgreSQL数据库中的阿拉伯语和英语文本

来自分类Dev

为什么 Hibernate saveOrUpdate 对阿拉伯语数据失败?

来自分类Dev

如果数据是阿拉伯语,则在 ajax 函数中获取字母“a”

来自分类Dev

在数据库中保存字符串的阿拉伯语列表

来自分类常见问题

Python泡菜错误:UnicodeDecodeError

来自分类Dev

Python 3 + FDB:UnicodeDecodeError

来自分类Dev

Python 2.7上的UnicodeDecodeError

来自分类Dev

Python/Scrapy - UnicodeDecodeError

来自分类Dev

阿拉伯语的PHP Hashtag不能是链接

来自分类Dev

连接从左到右和从右到左的语言(阿拉伯语等)

Related 相关文章

热门标签

归档