私はScikit-Learnを使ってPythonの機械学習プログラムを開発しています。例:誰かが私に「このプログラムは起動していません」と電子メールで通知し、マシンは「クラッシュの問題」と分類します。Scikit-Learn(Python 3)の新しいデータを使用した永続SVMモデルの再学習
私は、2つのCSVファイルから電子メールの内容とそれぞれのカテゴリラベルを読み取るSVMアルゴリズムを使用しています。 2番目のプログラムは、予測を行い
- :私は2つのプログラムを書きました訓練されたモデルをインポートします。 2番目のプログラムでは、分類子に新しいデータを取り込んで訓練されたモデルを更新できるようにしたいと思いますが、これをどうやって達成するのかは分かりません。予測プログラムは、ユーザーに電子メールを入力するように要求し、予測を行います。次に、予測が正しいかどうかをユーザーに確認します。どちらの場合でも、私はマシンに結果から学ぶことを望みます。
研修プログラム:
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
私がやった読書から、私はSVMが本当に追加学習をサポートしていないことを集めています新しいデータを古いトレーニングデータに組み込み、新しいデータがあるたびにモデル全体を最初から再学習する必要があることがわかりますそれに加えなさい。これはうまくいきますが、実際に実装する方法についてはあまりよく分かりません。トレーニングを開始できるように、新しいデータを含むように2つのCSVファイルを更新するための予測プログラムが必要ですか?
はい。 SVCはインクリメンタル学習のための 'partial_fit()'メソッドを持っていません。見積もり者(http://scikit-learn.org/stable/modules/scaling_strategies.html#incremental-learning)のみがサポートしています。更新されたcsvファイルとともに予測のたびに、トレーニングプログラムのコードを呼び出す必要があります。 –
また、 '' labels.csv "'が値として 'y'と' n'しか持っていない場合、LabelEncoderを使ってエンコーディングする必要はなく、SVCによって内部的に処理されます。 'predict()'メソッドは 'y'または' n'を直接出力します。これにより、プログラムの複雑さが軽減されます。 –
洞察に感謝します。私は以来、CSVファイルに新しい情報を追加するコードの一部を実装しました。そのため、トレーニングデータセットに含まれる新しい情報でSVCを後で再訓練することができます。 – Rudy