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()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值