Ejemplo n.º 1
0
def m_svm():
  if ARGV.one_vs:
    svm_model = svm.svm_to_binary(svm.svm_raw())
  else:
    svm_model = multi.one_against_one(svm.svm_to_binary(svm.svm_raw()))
  # return milk.defaultclassifier(mode='slow', multi_strategy='1-vs-1')
  learner = milk.supervised.classifier.ctransforms(
    # remove nans
    supervised.normalise.chkfinite(),
    # normalize to [-1,1]
    supervised.normalise.interval_normalise(),
    # feature selection
    featureselection.featureselector(
      featureselection.linear_independent_features),
    # sda filter
    featureselection.sda_filter(),
    # same parameter range as 'medium'
    supervised.gridsearch(
      svm_model,
      params = {
        'C': 2.0 ** np.arange(-2, 4),
        'kernel': [ svm.rbf_kernel(2.0 ** i) for i in xrange(-4, 4) ]
      }
    )
  )
  return learner
Ejemplo n.º 2
0
def test_rbf_kernel_call_many():
    from milk.supervised.svm import rbf_kernel
    np.random.seed(232)
    X = np.random.random((32,16))
    k = rbf_kernel(1.)
    pre = k.preprocess(X)
    qs = np.random.random((8,16))
    mapped = np.array(list(map(pre, qs)))
    manyed = pre.call_many(qs)
    assert np.allclose(manyed, mapped)
Ejemplo n.º 3
0
def test_rbf_kernel_call_many():
    from milk.supervised.svm import rbf_kernel
    np.random.seed(232)
    X = np.random.random((32, 16))
    k = rbf_kernel(1.)
    pre = k.preprocess(X)
    qs = np.random.random((8, 16))
    mapped = np.array(map(pre, qs))
    manyed = pre.call_many(qs)
    assert np.allclose(manyed, mapped)
Ejemplo n.º 4
0
def bench_milk(X, y, T, valid):
#
#       .. milk ..
#
    from milk.supervised import svm
    start = datetime.now()
    learner = svm.svm_raw(
        kernel=svm.rbf_kernel(sigma=1./X.shape[1]), C=1.)
    model = learner.train(X,y)
    pred = np.sign(map(model.apply, T))
    score = np.mean(pred == valid)
    return score, datetime.now() - start
Ejemplo n.º 5
0
def test_nfoldcrossvalidation():
    np.random.seed(22)
    features = np.array(
        [np.random.rand(8 + (i % 3), 12) * (i // 20) for i in range(40)],
        dtype=object)
    labels = np.zeros(40, int)
    labels[20:] = 1
    classifier = milk.supervised.grouped.voting_classifier(
        milk.supervised.svm_simple(C=1., kernel=rbf_kernel(1. / 12)))
    cmat, names = milk.nfoldcrossvalidation(features,
                                            labels,
                                            classifier=classifier)
    assert cmat.shape == (2, 2)
    assert sorted(names) == list(range(2))
Ejemplo n.º 6
0
def m_svm():
    if ARGV.one_vs:
        svm_model = svm.svm_to_binary(svm.svm_raw())
    else:
        svm_model = multi.one_against_one(svm.svm_to_binary(svm.svm_raw()))
    # return milk.defaultclassifier(mode='slow', multi_strategy='1-vs-1')
    learner = milk.supervised.classifier.ctransforms(
        # remove nans
        supervised.normalise.chkfinite(),
        # normalize to [-1,1]
        supervised.normalise.interval_normalise(),
        # feature selection
        featureselection.featureselector(
            featureselection.linear_independent_features),
        # sda filter
        featureselection.sda_filter(),
        # same parameter range as 'medium'
        supervised.gridsearch(
            svm_model,
            params={
                'C': 2.0**np.arange(-2, 4),
                'kernel': [svm.rbf_kernel(2.0**i) for i in xrange(-4, 4)]
            }))
    return learner
Ejemplo n.º 7
0
def test_nfoldcrossvalidation():
    np.random.seed(22)
    features = np.array([np.random.rand(8+(i%3), 12)*(i//20) for i in xrange(40)], dtype=object)
    labels = np.zeros(40, int)
    labels[20:] = 1
    classifier = milk.supervised.grouped.voting_classifier(milk.supervised.svm_simple(C=1., kernel=rbf_kernel(1./12)))
    cmat, names = milk.nfoldcrossvalidation(features, labels, classifier=classifier)
    assert cmat.shape == (2,2)
    assert sorted(names) == range(2)