コード例 #1
0
ファイル: classifier.py プロジェクト: yamins81/simffa
def train_liblinear_classifier_core(trainXy,
                     classifier_type = "liblinear",
                     trace_normalize=False,
                     **kwargs
                     ):
    """ Classifier training using SVMs

    Input:
    train_features = training features (both positive and negative)
    train_labels = corresponding label vector
    svm_eps = eps of svm
    svm_C = C parameter of svm
    classifier_type = liblinear or libsvm"""

    #do normalization
    (train_features, train_labels), train_mean, train_std, trace = normalize([trainXy],
                                                                              trace_normalize=trace_normalize)
    if classifier_type == 'liblinear':
        clf = sklearn_svm.LinearSVC(**kwargs)
    if classifier_type == 'libSVM':
        clf = sklearn_svm.SVC(**kwargs)
    elif classifier_type == 'LRL':
        clf = LogisticRegression(**kwargs)
    elif classifier_type == 'MCC':
        clf = CorrelationClassifier(**kwargs)
    elif classifier_type.startswith('svm.'):
        ct = classifier_type.split('.')[-1]
        clf = getattr(sklearn_svm,ct)(**kwargs)
    elif classifier_type.startswith('linear_model.'):
        ct = classifier_type.split('.')[-1]
        clf = getattr(sklearn_linear_model,ct)(**kwargs)

    clf.fit(train_features, train_labels)

    return clf, train_mean, train_std, trace
コード例 #2
0
ファイル: svm.py プロジェクト: yamins81/ecc
def classifier_train(train_features,
                     train_labels,
                     test_features,
                     svm_eps = 1e-5,
                     svm_C = 10**4,
                     classifier_type = "liblinear",
                     multi_class=False,
                     sphere = True
                     ):
    """ Classifier training using SVMs

    Input:
    train_features = training features (both positive and negative)
    train_labels = corresponding label vector
    svm_eps = eps of svm
    svm_C = C parameter of svm
    classifier_type = liblinear or libsvm"""

    #sphering
    if sphere:
        train_features, test_features = __sphere(train_features, test_features)

    if classifier_type == 'liblinear':
        clf = svm.LinearSVC(eps = svm_eps, C = svm_C,multi_class=multi_class)
    if classifier_type == 'libSVM':
        clf = svm.SVC(eps = svm_eps, C = svm_C, probability = True)
    elif classifier_type == 'LRL1':
        clf = LogisticRegression(C=svm_C, penalty = 'l1')
    elif classifier_type == 'LRL2':
        clf = LogisticRegression(C=svm_C, penalty = 'l1')

    clf.fit(train_features, train_labels)
    
    return clf
コード例 #3
0
ファイル: svm.py プロジェクト: yamins81/v1framework
def classifier_train(train_features,
                     train_labels,
                     test_features,
                     classifier_type = "liblinear",
                     sphere = True,
                     **kwargs
                     ):
    """ Classifier training using SVMs

    Input:
    train_features = training features (both positive and negative)
    train_labels = corresponding label vector
    svm_eps = eps of svm
    svm_C = C parameter of svm
    classifier_type = liblinear or libsvm"""
       
    #sphering
    if sphere:
        train_features, test_features,fmean,fstd = __sphere(train_features, test_features)
    else:
        fmean = None
        fstd = None

    if classifier_type == 'liblinear':
        clf = svm.LinearSVC(**kwargs)
    if classifier_type == 'libSVM':
        clf = svm.SVC(**kwargs)
    elif classifier_type == 'LRL':
        clf = LogisticRegression(**kwargs)
    elif classifier_type == 'MCC':
        clf = CorrelationClassifier(**kwargs)

    clf.fit(train_features, train_labels)
    
    return clf,fmean, fstd, sphere
コード例 #4
0
ファイル: classifier.py プロジェクト: yamins81/simffa
def train_liblinear_classifier_core(trainXy,
                                    classifier_type="liblinear",
                                    trace_normalize=False,
                                    **kwargs):
    """ Classifier training using SVMs

    Input:
    train_features = training features (both positive and negative)
    train_labels = corresponding label vector
    svm_eps = eps of svm
    svm_C = C parameter of svm
    classifier_type = liblinear or libsvm"""

    #do normalization
    (train_features, train_labels), train_mean, train_std, trace = normalize(
        [trainXy], trace_normalize=trace_normalize)
    if classifier_type == 'liblinear':
        clf = sklearn_svm.LinearSVC(**kwargs)
    if classifier_type == 'libSVM':
        clf = sklearn_svm.SVC(**kwargs)
    elif classifier_type == 'LRL':
        clf = LogisticRegression(**kwargs)
    elif classifier_type == 'MCC':
        clf = CorrelationClassifier(**kwargs)
    elif classifier_type.startswith('svm.'):
        ct = classifier_type.split('.')[-1]
        clf = getattr(sklearn_svm, ct)(**kwargs)
    elif classifier_type.startswith('linear_model.'):
        ct = classifier_type.split('.')[-1]
        clf = getattr(sklearn_linear_model, ct)(**kwargs)

    clf.fit(train_features, train_labels)

    return clf, train_mean, train_std, trace
コード例 #5
0
def check_l1_min_c(X, y, loss, fit_intercept=True, intercept_scaling=None):
    min_c = l1_min_c(X, y, loss, fit_intercept, intercept_scaling)

    clf = {
        ('log', False): LogisticRegression(penalty='l1'),
        ('log', True): SparseLogRegression(penalty='l1'),
        ('l2', False): LinearSVC(loss='l2', penalty='l1', dual=False),
        ('l2', True): SparseSVC(loss='l2', penalty='l1', dual=False),
    }[loss, sp.issparse(X)]

    clf.fit_intercept = fit_intercept
    clf.intercept_scaling = intercept_scaling

    clf.C = min_c
    clf.fit(X, y)
    assert (np.asanyarray(clf.coef_) == 0).all()
    assert (np.asanyarray(clf.intercept_) == 0).all()

    clf.C = min_c * 1.01
    clf.fit(X, y)
    assert (np.asanyarray(clf.coef_) != 0).any() or \
           (np.asanyarray(clf.intercept_) != 0).any()