我正在用Python开发一个机器学习程序,使用Scikit Learn,可以根据邮件内容将其分类为问题类型。e、 g:有人给我发邮件说“这个程序没有启动”,机器将其归类为“崩溃问题”。
-
第一个程序训练机器,并使用joblib导出训练后的模型。dump(),以便第二个程序可以使用经过训练的模型
-
第二个程序通过导入训练后的模型进行预测。我希望第二个程序能够通过使用接收的新数据重新拟合分类器来更新训练模型。但我不知道如何做到这一点。预测程序要求用户键入电子邮件,然后进行预测。然后,它会询问用户其预测是否正确。在这两种情况下,我希望机器从结果中学习。
import numpy as np
import pandas as pd
from pandas import DataFrame
import os
from sklearn import svm
from sklearn import preprocessing
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.externals import joblib
###### Extract and Vectorize the features from each email in the Training Data ######
features_file = "features.csv" #The CSV file that contains the descriptions of each email. Features will be extracted from this text data
features_df = pd.read_csv(features_file, encoding='ISO-8859-1')
vectorizer = TfidfVectorizer()
features = vectorizer.fit_transform(features_df['Description'].values.astype('U')) #The sole column in the CSV file is labeled "Description", so we specify that here
###### Encode the class Labels of the Training Data ######
labels_file = "labels.csv" #The CSV file that contains the classification labels for each email
labels_df = pd.read_csv(labels_file, encoding='ISO-8859-1')
lab_enc = preprocessing.LabelEncoder()
labels = lab_enc.fit_transform(labels_df)
###### Create a classifier and fit it to our Training Data ######
clf = svm.SVC(gamma=0.01, C=100)
clf.fit(features, labels)
###### Output persistent model files ######
joblib.dump(clf, 'brain.pkl')
joblib.dump(vectorizer, 'vectorizer.pkl')
joblib.dump(lab_enc, 'lab_enc.pkl')
print("Training completed.")
预测程序:
import numpy as np
import os
from sklearn import svm
from sklearn import preprocessing
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.externals import joblib
###### Load our model from our training program ######
clf = joblib.load('brain.pkl')
vectorizer = joblib.load('vectorizer.pkl')
lab_enc = joblib.load('lab_enc.pkl')
###### Prompt user for input, then make a prediction ######
print("Type an email's contents here and I will predict its category")
newData = [input(">> ")]
newDataFeatures = vectorizer.transform(newData)
print("I predict the category is: ", lab_enc.inverse_transform(clf.predict(newDataFeatures)))
###### Feedback loop - Tell the machine whether or not it was correct, and have it learn from the response ######
print("Was my prediction correct? y/n")
feedback = input(">> ")
inputValid = False
while inputValid == False:
if feedback == "y" or feedback == "n":
inputValid = True
else:
print("Response not understood. Was my prediction correct? y/n")
feedback = input(">> ")
if feedback == "y":
print("I was correct. I'll incorporate this new data into my persistent model to aid in future predictions.")
#refit the classifier using the new features and label
elif feedback == "n":
print("I was incorrect. What was the correct category?")
correctAnswer = input(">> ")
print("Got it. I'll incorporate this new data into my persistent model to aid in future predictions.")
#refit the classifier using the new features and label