def _test_boston_poly_compare_with_sklearn(params): diabetes = datasets.load_boston() clf = NuSVR(kernel='poly', **params) clf.fit(diabetes.data, diabetes.target) result = clf.score(diabetes.data, diabetes.target) clf = SklearnNuSVR(kernel='poly', **params) clf.fit(diabetes.data, diabetes.target) expected = clf.score(diabetes.data, diabetes.target) assert result > 0.5 assert abs(result - expected) < 1e-3
def _test_boston_rbf_compare_with_sklearn(C, nu, gamma): diabetes = datasets.load_boston() clf = NuSVR(kernel='rbf', gamma=gamma, C=C, nu=nu) clf.fit(diabetes.data, diabetes.target) result = clf.score(diabetes.data, diabetes.target) clf = SklearnNuSVR(kernel='rbf', gamma=gamma, C=C, nu=nu) clf.fit(diabetes.data, diabetes.target) expected = clf.score(diabetes.data, diabetes.target) assert result > 0.4 assert abs(result - expected) < 1e-3
def _test_boston_linear_compare_with_sklearn(C, nu): diabetes = datasets.load_boston() clf = NuSVR(kernel='linear', C=C, nu=nu) clf.fit(diabetes.data, diabetes.target) result = clf.score(diabetes.data, diabetes.target) clf = SklearnNuSVR(kernel='linear', C=C, nu=nu) clf.fit(diabetes.data, diabetes.target) expected = clf.score(diabetes.data, diabetes.target) assert result > 0.5 assert abs(result - expected) < 1e-3
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)
def _test_diabetes_compare_with_sklearn(kernel): diabetes = datasets.load_diabetes() clf_onedal = NuSVR(kernel=kernel, nu=.25, C=10.) clf_onedal.fit(diabetes.data, diabetes.target) result = clf_onedal.score(diabetes.data, diabetes.target) clf_sklearn = SklearnNuSVR(kernel=kernel, nu=.25, C=10.) clf_sklearn.fit(diabetes.data, diabetes.target) expected = clf_sklearn.score(diabetes.data, diabetes.target) assert result > expected - 1e-5 assert_allclose(clf_sklearn.intercept_, clf_onedal.intercept_, atol=1e-4) assert_allclose(clf_sklearn.support_vectors_.shape, clf_sklearn.support_vectors_.shape) assert_allclose(clf_sklearn.dual_coef_, clf_onedal.dual_coef_, atol=1e-2)
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)
def test_diabetes_simple(): diabetes = datasets.load_diabetes() clf = NuSVR(kernel='linear', C=10.) clf.fit(diabetes.data, diabetes.target) assert clf.score(diabetes.data, diabetes.target) > 0.02