因此,我创建了一个模型,将电子邮件分类为不同的类别,就像垃圾邮件过滤器一样。我将模型部署为一个webservice,没有问题,但我无法理解如何使用它来预测新电子邮件的输出类别。如何预处理新的电子邮件(主题和消息体)以匹配模型/webservice的输入格式?我训练的模型大约有1000个特征,对应于训练数据集中最频繁的1000个单词。我要把这封新邮件转过来吗?我只是在搜索新邮件中的特征/单词吗?我觉得有件明显的事我错过了。
我用蟒蛇,雪橇和熊猫/小熊来预处理和训练模型。
发布于 2020-06-05 17:12:09
因此,从我收集到的信息来看,你是在问如何预处理新的(我想是未被观察到的)电子邮件,这些电子邮件不会出现在培训集中。在这种情况下,您应该将电子邮件文本转换为1000维向量,其中每个值对应于特定的特征值。
我要说的是,你只是在计算新邮件中任何一个最频繁的1000个单词出现的次数(让我们称之为x^{(1)}_{test})。
要将电子邮件转换为矢量表单,以下是一种方法:
import pandas as pd
import numpy as np
words = ["blah", "tea", "tetra", "pak"]
def vectorise_email(words, e_mail):
"""Vectorise email to a vector of word counts based on a list of words.
:param words: (List of Strings) List of frequent words in training set
:param e_mail: (String) E-mail string
:return word_counts: (Numpy Array) containing counts of words based on words list.
"""
e_mail = pd.DataFrame(e_mail.split())
e_mail = e_mail[e_mail[0].isin(words)][0].value_counts()
word_counts = np.zeros(len(words))
for w_idx, word in enumerate(words):
word_counts[w_idx] = e_mail.at[word]
return word_counts
print(vectorise_email(words, "this is a blah tetra pak tea tea blah blahh"))这里我们首先将句子分解成单词(我使用了标准的字符串拆分方法,但您可以使用nltk的tokenise方法[https://www.nltk.org/api/nltk.tokenize.html])。然后,我们将这个列表转换为一个熊猫DataFrame,使用value_counts方法(Ref:https://stackoverflow.com/questions/22391433/count-the-frequency-that-a-value-occurs-in-a-dataframe-column)来获取单词列表中出现的单词的字数。然后,我们通过将这些计数映射到Numpy Array来完成矢量化过程,其中数组中的每个元素对应于输入电子邮件中的特定字数。
希望这有帮助
发布于 2020-06-10 18:17:12
任何数据科学项目投入生产时。在生产和测试培训之前,必须遵循相同的预处理技术。在测试/生产中执行预测时,技术没有差别。
让我们假设您已经在预处理中使用了这个函数,同样也可以在生产中使用,因为它就在向模型发送数据之前。
def preprocessing(text):
text = text.split()
text = remove_stop_words(text)
return texthttps://datascience.stackexchange.com/questions/75518
复制相似问题