scikit-learn SVM

import os
import pickle 
import sklearn
from sklearn import cross_validation, grid_search
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.svm import SVC
from sklearn.externals import joblib
import numpy as np 
import sys


def train_svm_classifer(features, model_save_file):
    """
    train_svm_classifer will train a SVM, saved the trained and SVM model and
    report the classification performance
 
    features: array of input features
    labels: array of labels associated with the input features
    model_output_path: path for storing the trained svm model
    """
    # save 20% of data for performance evaluation
    #X_train, X_test, y_train, y_test = cross_validation.train_test_split(features, labels, test_size=0.2)
 
    X_train = features[:,3:]
    y_train = features[:,2]
    X_test =  features[:,3:]
    y_test =  features[:,2]
    
    print "shape of train",np.shape(X_train),np.shape(y_train)    
    print "shape of test",np.shape(X_test),np.shape(y_test)    
    
    param = [
        {
            "kernel": ["linear"],
            "C": [1, 10, 100, 1000]
        },
        {
            "kernel": ["rbf"],
            "C": [1, 10, 100, 1000],
            "gamma": [1e-2, 1e-3, 1e-4, 1e-5]
        }
    ]
 
    # request probability estimation
    svm = SVC(probability=True)
 
    # 10-fold cross validation, use 4 thread as each fold and each parameter set can be train in parallel
    grid_model = grid_search.GridSearchCV(svm, param,
            cv=5, n_jobs=30, verbose=1)
 
    grid_model.fit(X_train, y_train)
    
    print("\nBest parameters set:")   
    best_parameters = grid_model.best_estimator_.get_params()
    for para, val in best_parameters.items():
        print para, val


 
    model = SVC(kernel='rbf', C=best_parameters['C'], gamma=best_parameters['gamma'], probability=True)
    model.fit(X_train, y_train)


    #pickle.dump(model, open(model_save_file, 'wb'))    


    y_predict=model.predict(X_test)
 
    print("\nConfusion matrix:")
    print(confusion_matrix(y_test, y_predict))
 
    print("\nClassification report:")
    print(classification_report(y_test, y_predict))




def run():
    raw_data = np.loadtxt(sys.argv[1], dtype=np.str,delimiter=" ",comments=None)
    print "shape of input",np.shape(raw_data)    
    train_svm_classifer(raw_data,"./svm.model")


run()

猜你喜欢

转载自blog.csdn.net/mzg12345678/article/details/78295756
今日推荐