Sklearn actual construction of SVM multi-classifier

It is very simple to build an SVM classifier using the sklearn library, because this library is already encapsulated and you only need to call the corresponding function.

# -*- coding: 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 import svm, metrics, preprocessing
from sklearn.metrics import roc_curve, auc, classification_report
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelBinarizer
from sklearn.multiclass import OneVsRestClassifier
from sklearn.externals import joblib
from scipy import interp

# 加载图像特征及标签
'''
def read_features(filedir):
    file_list = os.listdir(filedir)
    X = [] 
    tmp = "F: /python/airplane_001.txt"
    tmp_y = os.listdir ("F: / shiyan / TensorFlow / retrain / data / train") 
    # print (len (y)) 
    y = [] 
    for file in file_list: 
        
        tmp_file = filedir + "/" + file 
        tmp = np .loadtxt (tmp_file, dtype = str) 
        # np format conversion 
        feature = tmp.astype (np.float) 
        X.append (feature) 
        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)) 
    # The format of feature data saved to txt file It is str, so format conversion should be performed when performing the operation     
    
    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 have 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(label)
    # print(label)
    # print (label [: 45]) 
    # print(label.shape[1])
    # print (y [: 45]) 
    # Train the model and make predictions 
    random_state = np.random.RandomState (0) 
    n_samples, n_feature = feature.shape 
    # Randomize the data and divide the training set And test set 
    X_train, X_test, y_train, y_test = train_test_split (feature, label, test_size = .2, random_state = 0) 
    # training model 
    model = OneVsRestClassifier (svm.SVC (kernel = 'linear', probability = True, random_state = random_state )) 
    print ("[INFO] Successfully initialize a new model!") 
    print ("[INFO] Training the model ......") 
    clt = model.fit (X_train, y_train) 
    print ("[INFO] Model training completed!" ) 
    # Save the trained model and load it directly the next time you use it 
    joblib.dump (clt, "F: / python / model / conv_19_80%.pkl ") 
    Print ("[INFO] Model has been saved!") 
    '' '
    # Load the saved model
    clt = joblib.load("F:/python/model/SVM.pkl")
    print("model has been loaded !")
    # 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("overall accuracy: %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)
    print("average accuracy:%f"%(avg_acc))
    print("===========================================")
    classification_rep = classification_report(y_test,y_test_pred,
                                               target_names=label_list)
    print("classification report: \n",classification_rep)
    print("===========================================")
    #print("===========================================")
    confusion_matrix = metrics.confusion_matrix(y_test.argmax(axis=1),y_test_pred.argmax(axis=1))
    print("confusion metrix:\n",confusion_matrix)
    print("===========================================")
    # print("accuracy: %f"%(acc_r))
    print("[INFO] Successfully get SVM's classification overall accuracy ! ")


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)
   
    
    

Published 50 original articles · Likes5 · Visits 20,000+

Guess you like

Origin blog.csdn.net/qq_31207499/article/details/88635833