Machine learning practices in python3.x and python machine learning practices
Machine Learning Practice this book is written in the python2.x environment, while many functions and 2 in python3.x. the names or usage methods in x are different. Therefore, the content in the original book needs to be corrected. Below is a simple record of the fix part in the learning process.
1. Brackets must be added to the print function (the print function starts to appear in the program list 2-4)
2. Change raw_input to the input function. In 3.x, python uses input to replace the raw_input function (program list 2-5)
3. reload (KNN)-> import importlib
Importlib. reload (KNN)
4. program list 3-6
In python2.7, find the first element corresponding to the key: firstStr = myTree. keys () [0]. The following error is returned in python3.4: 'dict _ keys 'object does not support indexing, because python3 has changed dict. keys, which returns the dict_keys object. It supports iterable but does not support indexable. We can convert it to list explicitly, so this function should be implemented in python3 as follows:
FirstSides = list (myTree. keys ())
FirstStr = firstSides [0] # Find the first input element
5. program list 3-9
Pickle stores and reads data in binary format during data persistence.
Fw = open (filename, 'w') ----> fw = open (filename, 'wb ')
Open the file in binary format to facilitate pickle writing.
When reading data from a file, use the binary method to read the following changes:
Fr = open (filename) ----> fr = open (filename, 'rb ')
6. program list 4-5
WordList = textParse (open ('ch04/email/ham/mongod.txt '% I). read ())
This statement always reports an Encoding Error during running. The result is garbled characters in the read file. The problem is solved after the garbled characters are deleted.
The error code is as follows:
UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 199: illegal multibyte sequence
TrainingSet = range (50 );
Del (trainingSet [randIndex])
Because range cannot return List and del is not supported, the following error occurs during running:
TypeError: 'range' object doesn' t support item deletion
You can modify the code:
TrainingSet = list (range (50 ));
To be continued ......