UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0x92 in position 884: invalid start byte

---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-81-823d79549753> in <module>
----> 1 bayes.spamTest()

D:\maxwelllearning\maxwellhandon\machine learning in action\bayes.py in spamTest()
    144         fullText.extend(wordList)
    145         classList.append(1)
--> 146         wordList = textParse(open('email/ham/%d.txt' % i).read())
    147         docList.append(wordList)
    148         fullText.extend(wordList)

D:\Soft\Anaconda3\lib\codecs.py in decode(self, input, final)
    320         # decode input (taking the buffer into account)
    321         data = self.buffer + input
--> 322         (result, consumed) = self._buffer_decode(data, self.errors, final)
    323         # keep undecoded input until the next call
    324         self.buffer = data[consumed:]

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 in position 884: invalid start byte

original code:

def spamTest():
    docList = []; classList = []; fullText = []
    for i in range(1, 26):
        wordList = textParse(open('email/spam/%d.txt' % i, encoding="ISO-8859-1").read())
        docList.append(wordList)
        fullText.extend(wordList)
        classList.append(1)
        wordList = textParse(open('email/ham/%d.txt' % i).read())
        docList.append(wordList)
        fullText.extend(wordList)
        classList.append(0)
    vocabList = createVocabList(docList)#create vocabulary
    trainingSet = range(50); testSet = []           #create test set
    for i in range(10):
        randIndex = int(np.random.uniform(0, len(trainingSet)))
        testSet.append(trainingSet[randIndex])
        del(list(trainingSet)[randIndex])
    trainMat = []; trainClasses = []
    for docIndex in trainingSet:#train the classifier (get probs) trainNB0
        trainMat.append(bagOfWords2VecMN(vocabList, docList[docIndex]))
        trainClasses.append(classList[docIndex])
    p0V, p1V, pSpam = trainNB0(np.array(trainMat), np.array(trainClasses))
    errorCount = 0
    for docIndex in testSet:        #classify the remaining items
        wordVector = bagOfWords2VecMN(vocabList, docList[docIndex])
        if classifyNB(np.array(wordVector), p0V, p1V, pSpam) != classList[docIndex]:
            errorCount += 1
            print("classification error", docList[docIndex])
    print('the error rate is: ', float(errorCount)/len(testSet))
    #return vocabList, fullText

Modify Code:

def spamTest():
    docList = []; classList = []; fullText = []
    for i in range(1, 26):
        wordList = textParse(open('email/spam/%d.txt' % i, encoding="ISO-8859-1").read())
        docList.append(wordList)
        fullText.extend(wordList)
        classList.append(1)
        wordList = textParse(open('email/ham/%d.txt' % i, encoding="ISO-8859-1").read())
        docList.append(wordList)
        fullText.extend(wordList)
        classList.append(0)
    vocabList = createVocabList(docList)#create vocabulary
    trainingSet = range(50); testSet = []           #create test set
    for i in range(10):
        randIndex = int(np.random.uniform(0, len(trainingSet)))
        testSet.append(trainingSet[randIndex])
        del(list(trainingSet)[randIndex])
    trainMat = []; trainClasses = []
    for docIndex in trainingSet:#train the classifier (get probs) trainNB0
        trainMat.append(bagOfWords2VecMN(vocabList, docList[docIndex]))
        trainClasses.append(classList[docIndex])
    p0V, p1V, pSpam = trainNB0(np.array(trainMat), np.array(trainClasses))
    errorCount = 0
    for docIndex in testSet:        #classify the remaining items
        wordVector = bagOfWords2VecMN(vocabList, docList[docIndex])
        if classifyNB(np.array(wordVector), p0V, p1V, pSpam) != classList[docIndex]:
            errorCount += 1
            print("classification error", docList[docIndex])
    print('the error rate is: ', float(errorCount)/len(testSet))
    #return vocabList, fullText

Adding code : encoding="ISO-8859-1" 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
UnicodeDecodeError是Python中的一个异常,表示在解码Unicode字符串时发生了错误。具体地说,"utf-8 codec can't decode byte 0x8f in position 18: invalid start byte"这个错误表示在使用UTF-8编解码器解码字节序列时,遇到了无效的起始字节0x8f。 UTF-8是一种变长编码方式,它使用1到4个字节来表示一个Unicode字符。在UTF-8编码中,每个字节的最高位用于标识该字节是否为一个字符的起始字节,如果一个字节的最高位为0,则表示该字节为一个字符的起始字节;如果最高位为1,则表示该字节为一个字符的后续字节。 在你提供的错误信息中,字节序列中的第18个字节0x8f被认为是无效的起始字节,因此无法正确解码。这可能是由于以下原因导致的: 1. 字节序列中包含了非UTF-8编码的字节。 2. 字节序列中的某些字节丢失或损坏。 3. 字符串本身不是以UTF-8编码保存的。 要解决这个问题,你可以尝试以下几种方法: 1. 确保输入的字节序列是以UTF-8编码保存的,并且没有丢失或损坏的字节。 2. 如果你知道输入的编码方式,可以尝试使用相应的编码器进行解码。 3. 如果你不确定输入的编码方式,可以尝试使用Python的chardet库来自动检测编码方式。 4. 如果你无法修复输入的字节序列,可以考虑使用错误处理机制来处理解码错误,例如忽略错误的字节或替换为特定的占位符。 希望以上信息对你有帮助!如果你还有其他问题,请随时提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值