Skip to Main Content
Python 机器学习实践:测试驱动的开发方法
book

Python 机器学习实践:测试驱动的开发方法

by Matthew Kirk
January 2018
Intermediate to advanced content levelIntermediate to advanced
211 pages
8h 31m
Chinese
China Machine Press
Content preview from Python 机器学习实践:测试驱动的开发方法
朴素贝叶斯分类
57
make the subsequent tests work, though, we will have to fill in the skeleton for our
tokenizer module like so:
import re
class Tokenizer:
NULL = u'\u0000'
@staticmethod
def tokenize(string):
return re.findall("\w+", string.lower())
@staticmethod
def ngram(string, ngram):
tokens = Tokenizer.tokenize(string)
ngrams = []
for i in range(len(tokens)):
shift = i-ngram+1
padding = max(-shift,0)
first_idx = max(shift, 0)
last_idx = first_idx + ngram - padding
ngrams.append(Tokenizer.pad(tokens[first_idx:last_idx], padding))
return ngrams
@staticmethod
def pad(tokens, padding):
padded_tokens = []
for i in range(padding):
padded_tokens.append(Tokenizer.NULL) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Python for Bioinformatics

Mastering Python for Bioinformatics

Ken Youens-Clark

Publisher Resources

ISBN: 9787111581666