Ejemplo n.º 1
0
def test_predict():
    iris = datasets.load_iris()
    X = iris.data
    y = iris.target

    reg = NuSVR(kernel='linear', C=0.1).fit(X, y)

    linear = np.dot(X, reg.support_vectors_.T)
    dec = np.dot(linear, reg.dual_coef_.T) + reg.intercept_
    assert_array_almost_equal(dec.ravel(), reg.predict(X).ravel())

    reg = NuSVR(kernel='rbf', gamma=1).fit(X, y)

    rbfs = rbf_kernel(X, reg.support_vectors_, gamma=reg.gamma)
    dec = np.dot(rbfs, reg.dual_coef_.T) + reg.intercept_
    assert_array_almost_equal(dec.ravel(), reg.predict(X).ravel())
Ejemplo n.º 2
0
def test_pickle():
    diabetes = datasets.load_diabetes()

    clf = NuSVR(kernel='rbf', C=10.)
    clf.fit(diabetes.data, diabetes.target)
    expected = clf.predict(diabetes.data)

    import pickle
    dump = pickle.dumps(clf)
    clf2 = pickle.loads(dump)

    assert type(clf2) == clf.__class__
    result = clf2.predict(diabetes.data)
    assert_array_equal(expected, result)
Ejemplo n.º 3
0
def test_input_format_for_diabetes():
    diabetes = datasets.load_diabetes()

    c_contiguous_numpy = np.asanyarray(diabetes.data, dtype='float', order='C')
    assert c_contiguous_numpy.flags.c_contiguous
    assert not c_contiguous_numpy.flags.f_contiguous
    assert not c_contiguous_numpy.flags.fnc

    clf = NuSVR(kernel='linear', C=10.)
    clf.fit(c_contiguous_numpy, diabetes.target)
    dual_c_contiguous_numpy = clf.dual_coef_
    res_c_contiguous_numpy = clf.predict(c_contiguous_numpy)

    f_contiguous_numpy = np.asanyarray(diabetes.data, dtype='float', order='F')
    assert not f_contiguous_numpy.flags.c_contiguous
    assert f_contiguous_numpy.flags.f_contiguous
    assert f_contiguous_numpy.flags.fnc

    clf = NuSVR(kernel='linear', C=10.)
    clf.fit(f_contiguous_numpy, diabetes.target)
    dual_f_contiguous_numpy = clf.dual_coef_
    res_f_contiguous_numpy = clf.predict(f_contiguous_numpy)
    assert_allclose(dual_c_contiguous_numpy, dual_f_contiguous_numpy)
    assert_allclose(res_c_contiguous_numpy, res_f_contiguous_numpy)