我为这个幼稚的问题表示歉意,我已经在python中训练了一个模型(朴素贝叶斯),效果很好(95%的准确性)。它需要一个输入字符串(即“ Apple Inc.”或“ John Doe”),并识别出它是公司名称还是客户名称。
我实际上如何在另一个数据集上实现它?如果引入另一个熊猫数据框,如何将模型从训练数据中学到的内容应用于新数据框?
新数据框具有一个全新的填充和字符串集,它需要用来预测它是公司名称还是客户名称。
理想情况下,我想在新数据框中插入具有模型预测的列。
任何代码片段都值得赞赏。
当前模型的示例代码:
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')))
答案 0 :(得分:0)
想通了。
# 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)