【问题标题】:How do I apply ML model after it has been trained?机器学习模型经过训练后如何应用?
【发布时间】:2021-01-14 06:44:25
【问题描述】:

我为这个幼稚的问题道歉,我已经在 python 中训练了一个模型(朴素贝叶斯),它做得很好(95% 的准确率)。它接受一个输入字符串(即“Apple Inc.”或“John Doe”)并识别它是企业名称还是客户名称。

如何在另一个数据集上实际实现这一点?如果我引入另一个 pandas 数据框,我如何将模型从训练数据中学到的知识应用到新的数据框?

新的数据框有一个全新的人口和一组字符串,它需要预测它是企业名称还是客户名称。

理想情况下,我想在新数据框中插入一个包含模型预测的列。

欢迎任何代码 sn-ps。

当前模型的示例代码:

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(df["CUST_NM_CLEAN"], 
                                                    df["LABEL"],test_size=0.20, 
                                                    random_state=1)

# Instantiate the CountVectorizer method
count_vector = CountVectorizer()

# Fit the training data and then return the matrix
training_data = count_vector.fit_transform(X_train)

# Transform testing data and return the matrix. 
testing_data = count_vector.transform(X_test)

#in this case we try multinomial, there are two other methods
from sklearn.naive_bayes import cNB
naive_bayes = MultinomialNB()
naive_bayes.fit(training_data,y_train)
#MultinomialNB(alpha=1.0, class_prior=None, fit_prior=True)

predictions = naive_bayes.predict(testing_data)


from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
print('Accuracy score: {}'.format(accuracy_score(y_test, predictions)))
print('Precision score: {}'.format(precision_score(y_test, predictions, pos_label='Org')))
print('Recall score: {}'.format(recall_score(y_test, predictions, pos_label='Org')))
print('F1 score: {}'.format(f1_score(y_test, predictions, pos_label='Org')))

【问题讨论】:

  • 不知道为什么这被否决了,在我看来这是一个有效的问题。

标签: python machine-learning naivebayes model-fitting


【解决方案1】:

想通了。

# Convert a collection of text documents to a vector of term/token counts. 
cnt_vect_for_new_data = count_vector.transform(df['new_data'])

#RUN Prediction
df['NEW_DATA_PREDICTION'] = naive_bayes.predict(cnt_vect_for_new_data)

【讨论】:

    猜你喜欢
    • 2017-12-14
    • 2018-02-01
    • 2019-05-16
    • 2020-12-03
    • 2017-10-09
    • 1970-01-01
    • 1970-01-01
    • 2018-07-12
    • 1970-01-01
    相关资源
    最近更新 更多