このライブラリは既にカプセル化されており、対応する関数を呼び出すだけでよいため、sklearnライブラリを使用してSVM分類器を構築するのは非常に簡単です。
#-*-コーディング:utf-8-*- "" " Created on Fri Nov 23 18:44:37 2018 @author:13260 " "" import os import numpy as np import matplotlib.pyplot as plt from itertools import cycle from前処理sklearnインポートSVM、メトリクス、 sklearn.metricsからroc_curve、AUC、classification_reportインポート sklearn.model_selectionインポートtrain_test_splitから インポートLabelBinarizerをsklearn.preprocessingから sklearn.multiclassインポートからOneVsRestClassifier sklearn.externalsからはJOBLIBをインポート INTERP scipyのダウンロードインポートから #加载图像特征及标签 ''」 デフread_features(FILEDIR): file_listの= os.listdir(FILEDIR) X = [] tmp_y = os.listdir( "F:/ shiyan / TensorFlow / retrain / data / train") #print(len(y)) y = [] for file in file_list: tmp_file = filedir + "/" +ファイル tmp = np.loadtxt(tmp_file、dtype = str)#np 形式変換 機能= tmp.astype(np.float) X.append(機能) old_filename = file [:-3] .split( "_") filename = "_"。join(old_filename [:-1]) #tmp_filename = filter(str.isalpha、file [:-3]) #print(filename) y.append(tmp_y.index(filename)) #機能データを保存txtファイルのフォーマットはstrなので、操作 tmp = "F:/python/airplane_001.txt"を実行するときにフォーマット変換を実行する必要があります tmp_data = np.loadtxt(tmp、dtype = str) res = tmp_data.astype(np.float) X.append(res) '' ' #加载特征和标签文件 def load_features_and_labels(features_path、labels_path): features = np.load(features_path) labels = np.load(labels_path ) print( "[INFO] Feature and label file has been loaded!") return features、labels def train_and_test_model(feature、label): #X_scaled = preprocessing.scale(X)#print (y) #y = label_binarize(y、 classes = list(range(45))) label_list = os.listdir( "F:/ shiyan / TensorFlow / retrain / data / train") #print(label.shape)#print (label) label = LabelBinarizer()。fit_transform (ラベル) #print(ラベル) (label [:45]) #print(label.shape [1]) #print #print(y [:45]) #モデルをトレーニングして予測を行うrandom_state = np.random.RandomState(0) n_samples、n_feature = feature.shape #データをランダム化してトレーニングセットを分割するそして、テストセット X_train、X_test、y_train、y_test = train_test_split(feature、label、test_size = .2、random_state = 0) #トレーニングモデル モデル= OneVsRestClassifier(svm.SVC(kernel = 'linear'、確率= True、random_state = random_state )) print( "[INFO]新しいモデルを正常に初期化します!") print( "[INFO]モデルのトレーニング......") clt = model.fit(X_train、y_train) print( "[INFO] Model training completed!" ) #トレーニング済みモデルを保存し、次回 joblib.dump(clt、 "F:/ python / model / conv_19_80%。pkl ") print( "[INFO]モデルが保存されました!")mean(acc_for_each_class) print( "平均精度:%f"%(avg_acc)) '' ' #保存したモデルを読み込む clt = joblib.load( "F:/python/model/SVM.pkl") print( "モデルがロードされました!") #y_train_pred = clt.predict(X_train) '' ' y_test_pred = clt.predict(X_test) ov_acc = metrics.accuracy_score(y_test_pred、y_test) print( "全体的な精度:%f"%(ov_acc)) print( "========================= =================== ") acc_for_each_class = metrics.precision_score(y_test、y_test_pred、average = None) print(" acc_for_each_class:\ n "、acc_for_each_class) print(" = ==========================================) avg_acc = np.mean( acc_for_each_class)s分類全体の精度!") if __name__ ==" __main__ ": features_path = "F:/python/features/DenseNet/densenet_fv_flatten.npy" labels_path = "F:/python/features/VGG19/VGG19_labels.npy" feature、label = load_features_and_labels(features_path、labels_path) train_and_test_model(feature、label)