コード例 #1
0
ファイル: test_svr.py プロジェクト: michael-smirnov/daal4py
    def dummy(*args, **kwargs):
        pass

    md = sklearn.utils.estimator_checks
    saved = _replace_and_save(md, [
        'check_sample_weights_invariance',  # Max absolute difference: 0.0002
        'check_estimators_fit_returns_self',  # ???
        'check_regressors_train',  # Cannot get data type from empty metadata
        'check_estimators_unfitted',  # expected NotFittedError from sklearn
    ], dummy)
    check_estimator(SVR())
    _restore_from_saved(md, saved)


@pass_if_not_implemented_for_gpu(reason="svr is not implemented")
@pytest.mark.parametrize('queue', get_queues())
def test_run_to_run_fit(queue):
    diabetes = datasets.load_diabetes()
    clf_first = SVR(kernel='linear', C=10.)
    clf_first.fit(diabetes.data, diabetes.target, queue=queue)

    for _ in range(10):
        clf = SVR(kernel='linear', C=10.)
        clf.fit(diabetes.data, diabetes.target, queue=queue)
        assert_allclose(clf_first.intercept_, clf.intercept_)
        assert_allclose(clf_first.support_vectors_, clf.support_vectors_)
        assert_allclose(clf_first.dual_coef_, clf.dual_coef_)


@pass_if_not_implemented_for_gpu(reason="svr is not implemented")
@pytest.mark.parametrize('queue', get_queues())
コード例 #2
0
def _test_simple_dataset(queue, kernel):
    X = np.array([[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1]])
    sparse_X = sp.lil_matrix(X)
    Y = [1, 1, 1, 2, 2, 2]

    X2 = np.array([[-1, -1], [2, 2], [3, 2]])
    sparse_X2 = sp.dok_matrix(X2)

    dataset = sparse_X, Y, sparse_X2
    clf = SVC(kernel=kernel, gamma=1)
    check_svm_model_equal(queue, clf, *dataset)


@pass_if_not_implemented_for_gpu(reason="csr svm is not implemented")
@pytest.mark.parametrize('queue', get_queues('cpu') + [
    pytest.param(get_queues('gpu'),
                 marks=pytest.mark.xfail(
                     reason="raises UnknownError instead of RuntimeError "
                            "with unimplemented message"))])
@pytest.mark.parametrize('kernel', ['linear', 'rbf'])
def test_simple_dataset(queue, kernel):
    _test_simple_dataset(queue, kernel)


def _test_binary_dataset(queue, kernel):
    X, y = make_classification(n_samples=80, n_features=20, n_classes=2, random_state=0)
    sparse_X = sp.csr_matrix(X)

    dataset = sparse_X, y, sparse_X
    clf = SVC(kernel=kernel)
コード例 #3
0
ファイル: test_svc.py プロジェクト: michael-smirnov/daal4py
def _test_libsvm_parameters(queue, array_constr, dtype):
    X = array_constr([[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1]],
                     dtype=dtype)
    y = array_constr([1, 1, 1, 2, 2, 2], dtype=dtype)

    clf = SVC(kernel='linear').fit(X, y, queue=queue)
    assert_array_equal(clf.dual_coef_, [[-0.25, .25]])
    assert_array_equal(clf.support_, [1, 3])
    assert_array_equal(clf.support_vectors_, (X[1], X[3]))
    assert_array_equal(clf.intercept_, [0.])
    assert_array_equal(clf.predict(X), y)


# TODO: investigate sporadic failures on GPU
@pytest.mark.parametrize('queue', get_queues('host,cpu'))
@pytest.mark.parametrize('array_constr', [np.array])
@pytest.mark.parametrize('dtype', [np.float32, np.float64])
def test_libsvm_parameters(queue, array_constr, dtype):
    _test_libsvm_parameters(queue, array_constr, dtype)


@pytest.mark.parametrize(
    'queue',
    get_queues('cpu') + [
        pytest.param(
            get_queues('gpu'),
            marks=pytest.mark.xfail(reason="class weights are not implemented "
                                    "but the error is not raised"))
    ])
def test_class_weight(queue):
コード例 #4
0
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#===============================================================================

import pytest
import numpy as np
from numpy.testing import assert_array_equal

from onedal.neighbors import KNeighborsClassifier
from onedal.tests.utils._device_selection import get_queues

from sklearn import datasets


@pytest.mark.parametrize('queue', get_queues())
def test_iris(queue):
    iris = datasets.load_iris()
    clf = KNeighborsClassifier(2).fit(iris.data, iris.target, queue=queue)
    assert clf.score(iris.data, iris.target, queue=queue) > 0.9
    assert_array_equal(clf.classes_, np.sort(clf.classes_))


# TODO: investigate failures on GPU
@pytest.mark.parametrize('queue', get_queues('host,cpu'))
def test_pickle(queue):
    iris = datasets.load_iris()
    clf = KNeighborsClassifier(2).fit(iris.data, iris.target, queue=queue)
    expected = clf.predict(iris.data, queue=queue)

    import pickle
コード例 #5
0
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#===============================================================================

import pytest
import numpy as np

from onedal.common._policy import _get_policy
from onedal.tests.utils._device_selection import (get_queues, get_memory_usm,
                                                  is_dpctl_available,
                                                  device_type_to_str)


@pytest.mark.parametrize('queue', get_queues())
def test_queue_passed_directly(queue):
    device_name = device_type_to_str(queue)
    assert _get_policy(queue).get_device_name() == device_name


@pytest.mark.parametrize('queue', get_queues())
def test_with_numpy_data(queue):
    X = np.zeros((5, 3))
    y = np.zeros(3)

    device_name = device_type_to_str(queue)
    assert _get_policy(queue, X, y).get_device_name() == device_name


@pytest.mark.skipif(not is_dpctl_available(), reason='depends on dpctl')