Machine learning practices in python3.x and python machine learning practices

Source: Internet
Author: User

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 ......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.