Machine Learning in Practice: Error in the Storage and Reading of Decision Trees (Python3)

1. 报错一:TypeError: write() argument must be str, not bytes

The code to write the decision tree to disk is as follows:

1 def storeTree(inputTree, filename):
2     import pickle
3     fw = open(filename, 'w')
4     pickle.dump(inputTree, fw)
5     fw.close()

Corrected code:  fw = open(filename, ' wb ' ) 

 

2. 报错二:UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte

The code to read the stored decision tree file is as follows:

1 def grabTree(filename):
2     import pickle
3     fr = open(filename,'r')
4     return pickle.load(fr)

Corrected code:  fr = open(filename, ' rb ' ) 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324576251&siteId=291194637