Python sklearn.svm.NuSVC() Examples

The following are 25 code examples of sklearn.svm.NuSVC(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sklearn.svm , or try the search function .
Example #1
Source File: utils.py    From m2cgen with MIT License 6 votes vote down vote up
def __call__(self, estimator):
        fitted_estimator = estimator.fit(self.X_train, self.y_train)

        if isinstance(estimator, (LinearClassifierMixin, SVC, NuSVC,
                                  LightBaseClassifier)):
            y_pred = estimator.decision_function(self.X_test)
        elif isinstance(estimator, DecisionTreeClassifier):
            y_pred = estimator.predict_proba(self.X_test.astype(np.float32))
        elif isinstance(
                estimator,
                (ForestClassifier, XGBClassifier, LGBMClassifier)):
            y_pred = estimator.predict_proba(self.X_test)
        else:
            y_pred = estimator.predict(self.X_test)

        return self.X_test, y_pred, fitted_estimator 
Example #2
Source File: test_svm.py    From Mastering-Elasticsearch-7.0 with MIT License 6 votes vote down vote up
def test_probability():
    # Predict probabilities using SVC
    # This uses cross validation, so we use a slightly bigger testing set.

    for clf in (svm.SVC(gamma='scale', probability=True, random_state=0,
                C=1.0), svm.NuSVC(gamma='scale', probability=True,
                                  random_state=0)):
        clf.fit(iris.data, iris.target)

        prob_predict = clf.predict_proba(iris.data)
        assert_array_almost_equal(
            np.sum(prob_predict, 1), np.ones(iris.data.shape[0]))
        assert np.mean(np.argmax(prob_predict, 1)
                       == clf.predict(iris.data)) > 0.9

        assert_almost_equal(clf.predict_proba(iris.data),
                            np.exp(clf.predict_log_proba(iris.data)), 8) 
Example #3
Source File: test_svm.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_probability():
    # Predict probabilities using SVC
    # This uses cross validation, so we use a slightly bigger testing set.

    for clf in (svm.SVC(probability=True, random_state=0, C=1.0),
                svm.NuSVC(probability=True, random_state=0)):
        clf.fit(iris.data, iris.target)

        prob_predict = clf.predict_proba(iris.data)
        assert_array_almost_equal(
            np.sum(prob_predict, 1), np.ones(iris.data.shape[0]))
        assert_true(np.mean(np.argmax(prob_predict, 1)
                            == clf.predict(iris.data)) > 0.9)

        assert_almost_equal(clf.predict_proba(iris.data),
                            np.exp(clf.predict_log_proba(iris.data)), 8) 
Example #4
Source File: support_vector_machines.py    From Python with MIT License 6 votes vote down vote up
def test(X_new):
    """
    3 test cases to be passed
    an array containing the sepal length (cm), sepal width (cm), petal length (cm),
    petal width (cm) based on which  the target name will be predicted
    >>> test([1,2,1,4])
    'virginica'
    >>> test([5, 2, 4, 1])
    'versicolor'
    >>> test([6,3,4,1])
    'versicolor'
    """
    iris = load_iris()
    # splitting the dataset to test and train
    train_x, test_x, train_y, test_y = train_test_split(
        iris["data"], iris["target"], random_state=4
    )
    # any of the 3 types of SVM can be used
    # current_model=SVC(train_x, train_y)
    # current_model=NuSVC(train_x, train_y)
    current_model = Linearsvc(train_x, train_y)
    prediction = current_model.predict([X_new])
    return iris["target_names"][prediction][0] 
Example #5
Source File: test_svm.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_decision_function_shape_two_class():
    for n_classes in [2, 3]:
        X, y = make_blobs(centers=n_classes, random_state=0)
        for estimator in [svm.SVC, svm.NuSVC]:
            clf = OneVsRestClassifier(estimator(
                decision_function_shape="ovr")).fit(X, y)
            assert_equal(len(clf.predict(X)), len(y)) 
Example #6
Source File: test_svm.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_immutable_coef_property():
    # Check that primal coef modification are not silently ignored
    svms = [
        svm.SVC(kernel='linear').fit(iris.data, iris.target),
        svm.NuSVC(kernel='linear').fit(iris.data, iris.target),
        svm.SVR(kernel='linear').fit(iris.data, iris.target),
        svm.NuSVR(kernel='linear').fit(iris.data, iris.target),
        svm.OneClassSVM(kernel='linear').fit(iris.data),
    ]
    for clf in svms:
        assert_raises(AttributeError, clf.__setattr__, 'coef_', np.arange(3))
        assert_raises((RuntimeError, ValueError),
                      clf.coef_.__setitem__, (0, 0), 0) 
Example #7
Source File: test_sparse.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_error():
    # Test that it gives proper exception on deficient input
    # impossible value of C
    assert_raises(ValueError, svm.SVC(C=-1).fit, X, Y)

    # impossible value of nu
    clf = svm.NuSVC(nu=0.0)
    assert_raises(ValueError, clf.fit, X_sp, Y)

    Y2 = Y[:-1]  # wrong dimensions for labels
    assert_raises(ValueError, clf.fit, X_sp, Y2)

    clf = svm.SVC()
    clf.fit(X_sp, Y)
    assert_array_equal(clf.predict(T), true_result) 
Example #8
Source File: test_svm.py    From pandas-ml with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_objectmapper(self):
        df = pdml.ModelFrame([])
        self.assertIs(df.svm.SVC, svm.SVC)
        self.assertIs(df.svm.LinearSVC, svm.LinearSVC)
        self.assertIs(df.svm.NuSVC, svm.NuSVC)
        self.assertIs(df.svm.SVR, svm.SVR)
        self.assertIs(df.svm.NuSVR, svm.NuSVR)
        self.assertIs(df.svm.OneClassSVM, svm.OneClassSVM) 
Example #9
Source File: test_sklearn_svm_converters.py    From sklearn-onnx with MIT License 5 votes vote down vote up
def test_convert_nusvc_multi_pfalse(self):
        model, X = self._fit_multi_classification(
            NuSVC(probability=False, nu=0.1,
                  decision_function_shape='ovo'))
        model_onnx = convert_sklearn(
            model, "SVC", [("input", FloatTensorType([None, X.shape[1]]))])
        nodes = model_onnx.graph.node
        self.assertIsNotNone(nodes)
        svc_node = nodes[0]
        self._check_attributes(
            svc_node,
            {
                "coefficients": None,
                "kernel_params": None,
                "kernel_type": "RBF",
                "post_transform": None,
                "rho": None,
                "support_vectors": None,
                "vectors_per_class": None,
            },
        )
        dump_data_and_model(
            X, model, model_onnx,
            basename="SklearnMclNuSVCPF-Dec1",  # max relative error is 1e-5
            allow_failure="StrictVersion(onnxruntime.__version__)"
                          " < StrictVersion('0.5.0')") 
Example #10
Source File: test_sklearn_svm_converters.py    From sklearn-onnx with MIT License 5 votes vote down vote up
def test_convert_nusvc_binary_ptrue(self):
        model, X = self._fit_binary_classification(NuSVC(probability=True))
        model_onnx = convert_sklearn(
            model, "SVC", [("input", FloatTensorType([None, X.shape[1]]))])
        nodes = model_onnx.graph.node
        self.assertIsNotNone(nodes)
        svc_node = nodes[0]
        self._check_attributes(
            svc_node,
            {
                "coefficients": None,
                "kernel_params": None,
                "kernel_type": "RBF",
                "post_transform": None,
                "rho": None,
                "support_vectors": None,
                "vectors_per_class": None,
            },
        )
        dump_data_and_model(
            X,
            model,
            model_onnx,
            basename="SklearnBinNuSVCPT",
            allow_failure="StrictVersion(onnxruntime.__version__)"
                          " <= StrictVersion('0.4.0')"
        ) 
Example #11
Source File: test_sklearn_svm_converters.py    From sklearn-onnx with MIT License 5 votes vote down vote up
def test_convert_nusvc_binary_pfalse(self):
        model, X = self._fit_binary_classification(
            NuSVC(probability=False, decision_function_shape='ovo'))
        model_onnx = convert_sklearn(
            model, "SVC", [("input", FloatTensorType([None, X.shape[1]]))])
        nodes = model_onnx.graph.node
        self.assertIsNotNone(nodes)
        svc_node = nodes[0]
        self._check_attributes(
            svc_node,
            {
                "coefficients": None,
                "kernel_params": None,
                "kernel_type": "RBF",
                "post_transform": None,
                "rho": None,
                "support_vectors": None,
                "vectors_per_class": None,
            },
        )
        dump_data_and_model(
            X,
            model,
            model_onnx,
            basename="SklearnBinNuSVCPF-NoProbOpp",
            allow_failure="StrictVersion(onnxruntime.__version__)"
                          " < StrictVersion('0.5.0')"
        ) 
Example #12
Source File: _parse.py    From sklearn-onnx with MIT License 5 votes vote down vote up
def _parse_sklearn_classifier(scope, model, inputs, custom_parsers=None):
    probability_tensor = _parse_sklearn_simple_model(
            scope, model, inputs, custom_parsers=custom_parsers)
    if model.__class__ in [NuSVC, SVC] and not model.probability:
        return probability_tensor
    options = scope.get_options(model, dict(zipmap=True))
    if not options['zipmap']:
        return probability_tensor
    this_operator = scope.declare_local_operator('SklearnZipMap')
    this_operator.inputs = probability_tensor
    label_type = Int64TensorType([None])
    classes = get_label_classes(scope, model)

    if (isinstance(model.classes_, list) and
            isinstance(model.classes_[0], np.ndarray)):
        # multi-label problem
        pass
    elif np.issubdtype(classes.dtype, np.floating):
        classes = np.array(list(map(lambda x: int(x), classes)))
        if set(map(lambda x: float(x), classes)) != set(model.classes_):
            raise RuntimeError("skl2onnx implicitly converts float class "
                               "labels into integers but at least one label "
                               "is not an integer. Class labels should "
                               "be integers or strings.")
        this_operator.classlabels_int64s = classes
    elif np.issubdtype(classes.dtype, np.signedinteger):
        this_operator.classlabels_int64s = classes
    elif np.issubdtype(classes.dtype, np.unsignedinteger):
        this_operator.classlabels_int64s = classes
    else:
        classes = np.array([s.encode('utf-8') for s in classes])
        this_operator.classlabels_strings = classes
        label_type = StringTensorType([None])

    output_label = scope.declare_local_variable('output_label', label_type)
    output_probability = scope.declare_local_variable(
        'output_probability',
        SequenceType(DictionaryType(label_type, scope.tensor_type())))
    this_operator.outputs.append(output_label)
    this_operator.outputs.append(output_probability)
    return this_operator.outputs 
Example #13
Source File: classification.py    From pyImSegm with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def create_classifiers(nb_workers=-1):
    """ create all classifiers with default parameters

    :param int nb_workers: number of parallel if possible
    :return dict: {str: clf}

    >>> classifs = create_classifiers()
    >>> classifs  # doctest: +ELLIPSIS
    {...}
    >>> sum([isinstance(create_clf_param_search_grid(k), dict)
    ...      for k in classifs.keys()])
    7
    >>> sum([isinstance(create_clf_param_search_distrib(k), dict)
    ...      for k in classifs.keys()])
    7
    """
    clfs = {
        'RandForest': ensemble.RandomForestClassifier(n_estimators=20,
                                                      # oob_score=True,
                                                      min_samples_leaf=2,
                                                      min_samples_split=3,
                                                      n_jobs=nb_workers),
        'GradBoost': ensemble.GradientBoostingClassifier(subsample=0.25,
                                                         warm_start=False,
                                                         max_depth=6,
                                                         min_samples_leaf=6,
                                                         n_estimators=200,
                                                         min_samples_split=7),
        'LogistRegr': linear_model.LogisticRegression(solver='sag',
                                                      n_jobs=nb_workers),
        'KNN': neighbors.KNeighborsClassifier(n_jobs=nb_workers),
        'SVM': svm.SVC(kernel='rbf', probability=True,
                       tol=2e-3, max_iter=5000),
        'DecTree': tree.DecisionTreeClassifier(),
        # 'RBM': create_pipeline_neuron_net(),
        'AdaBoost': ensemble.AdaBoostClassifier(n_estimators=5),
        # 'NuSVM-rbf': svm.NuSVC(kernel='rbf', probability=True),
    }
    return clfs 
Example #14
Source File: test_NuSVC.py    From coremltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_conversion_bad_inputs(self):
        from sklearn.preprocessing import OneHotEncoder

        # Error on converting an untrained model
        with self.assertRaises(TypeError):
            model = NuSVC()
            spec = scikit_converter.convert(model, "data", "out")

        # Check the expected class during conversion
        with self.assertRaises(TypeError):
            model = OneHotEncoder()
            spec = scikit_converter.convert(model, "data", "out") 
Example #15
Source File: _NuSVC.py    From coremltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def convert(model, feature_names, target):
    """Convert a Nu-Support Vector Classification (NuSVC) model to the protobuf spec.
    Parameters
    ----------
    model: NuSVC
        A trained NuSVC encoder model.

    feature_names: [str], optional (default=None)
        Name of the input columns.

    target: str, optional (default=None)
        Name of the output column.

    Returns
    -------
    model_spec: An object of type Model_pb.
        Protobuf representation of the model
    """

    if not (_HAS_SKLEARN):
        raise RuntimeError(
            "scikit-learn not found. scikit-learn conversion API is disabled."
        )

    _sklearn_util.check_expected_type(model, _NuSVC)
    return _SVC.convert(model, feature_names, target) 
Example #16
Source File: support_vector_machines.py    From Python with MIT License 5 votes vote down vote up
def NuSVC(train_x, train_y):
    svc_NuSVC = svm.NuSVC()
    svc_NuSVC.fit(train_x, train_y)
    return svc_NuSVC 
Example #17
Source File: test_svm.py    From Mastering-Elasticsearch-7.0 with MIT License 5 votes vote down vote up
def test_decision_function_shape_two_class():
    for n_classes in [2, 3]:
        X, y = make_blobs(centers=n_classes, random_state=0)
        for estimator in [svm.SVC, svm.NuSVC]:
            clf = OneVsRestClassifier(estimator(gamma='scale',
                decision_function_shape="ovr")).fit(X, y)
            assert_equal(len(clf.predict(X)), len(y)) 
Example #18
Source File: test_svm.py    From Mastering-Elasticsearch-7.0 with MIT License 5 votes vote down vote up
def test_immutable_coef_property():
    # Check that primal coef modification are not silently ignored
    svms = [
        svm.SVC(kernel='linear').fit(iris.data, iris.target),
        svm.NuSVC(kernel='linear').fit(iris.data, iris.target),
        svm.SVR(kernel='linear').fit(iris.data, iris.target),
        svm.NuSVR(kernel='linear').fit(iris.data, iris.target),
        svm.OneClassSVM(kernel='linear').fit(iris.data),
    ]
    for clf in svms:
        assert_raises(AttributeError, clf.__setattr__, 'coef_', np.arange(3))
        assert_raises((RuntimeError, ValueError),
                      clf.coef_.__setitem__, (0, 0), 0) 
Example #19
Source File: test_sparse.py    From Mastering-Elasticsearch-7.0 with MIT License 5 votes vote down vote up
def test_error():
    # Test that it gives proper exception on deficient input
    # impossible value of C
    assert_raises(ValueError, svm.SVC(gamma='scale', C=-1).fit, X, Y)

    # impossible value of nu
    clf = svm.NuSVC(gamma='scale', nu=0.0)
    assert_raises(ValueError, clf.fit, X_sp, Y)

    Y2 = Y[:-1]  # wrong dimensions for labels
    assert_raises(ValueError, clf.fit, X_sp, Y2)

    clf = svm.SVC(gamma="scale")
    clf.fit(X_sp, Y)
    assert_array_equal(clf.predict(T), true_result) 
Example #20
Source File: classifier_evaluator.py    From yelp with GNU Lesser General Public License v2.1 4 votes vote down vote up
def load_pipeline():

    best_hyperparams_file_name = Constants.generate_file_name(
        'best_hyperparameters', 'json', Constants.CACHE_FOLDER, None,
        None, False)

    if not os.path.exists(best_hyperparams_file_name):
        print('Recsys contextual records have already been generated')
        full_cycle()

    with open(best_hyperparams_file_name, 'r') as json_file:
        file_contents = json_file.read()
        parameters = json.loads(file_contents)

        print(parameters)

        classifiers = {
            'logisticregression': LogisticRegression(),
            'svc': SVC(),
            'kneighborsclassifier': KNeighborsClassifier(),
            'decisiontreeclassifier': DecisionTreeClassifier(),
            'nusvc': NuSVC(),
            'randomforestclassifier': RandomForestClassifier()
        }

        classifier = classifiers[parameters['classifier'].lower()]
        # print(classifier)
        classifier_params = get_classifier_params(parameters)
        classifier.set_params(**classifier_params)
        print(classifier)

        resampler = sampler_factory.create_sampler(
            parameters['resampler'], Constants.DOCUMENT_CLASSIFIER_SEED)

        return Pipeline([('resampler', resampler), ('classifier', classifier)])

        # for pname, pval in parameters.items():
        #     print(pname, pval)
        #
        #     if pname.startswith('classifier__'):
        #         step, param = pname.split('__', 1)
        #         # fit_params_steps[step][param] = pval
        #         print(step, param) 
Example #21
Source File: support_vector_machines.py    From sklearn-onnx with MIT License 4 votes vote down vote up
def calculate_sklearn_svm_output_shapes(operator):
    """
    For SVM classifiers, allowed input/output patterns are
        1. [N, C] ---> [N], A sequence of map
    Note that the second case is not allowed as long as ZipMap only
    produces dictionary.

    For SVM regressors, allowed input/output patterns are
        1. [N, C] ---> [N]

    For both of SVC and SVR, the inputs should numerical tensor(s).
    For SVC with batch size 1, the first output is the label and the
    second output is a map used to store all class probabilities (For
    a key-value pair, the value is assigned to the class specified by
    the key). If batch size is larger than 1, we need to use a sequence
    of maps to denote class probabilities. Regarding SVR, we just
    produce a scalar for each example. If there are N examples, the
    output shape would be [N, 1].
    """
    op = operator.raw_operator

    N = operator.inputs[0].type.shape[0]
    if operator.type in ['SklearnOneClassSVM']:
        operator.outputs[0].type = Int64TensorType([N, 1])
        operator.outputs[1].type.shape = [N, 1]
    elif operator.type in ['SklearnSVC'] or isinstance(op, (SVC, NuSVC)):
        number_of_classes = len(op.classes_)
        check_input_and_output_numbers(operator, input_count_range=[1, None],
                                       output_count_range=[1, 2])

        if all(isinstance(i, (six.string_types, six.text_type))
               for i in op.classes_):
            operator.outputs[0].type = StringTensorType([N])
            operator.outputs[1].type.shape = [N, number_of_classes]
        elif all(isinstance(i, (numbers.Real, bool, np.bool_))
                 for i in op.classes_):
            operator.outputs[0].type = Int64TensorType([N])
            operator.outputs[1].type.shape = [N, number_of_classes]
        else:
            raise RuntimeError('Class labels should be either all strings or '
                               'all integers. C++ backends do not support '
                               'mixed types.')

    elif operator.type in ['SklearnSVR']:
        check_input_and_output_numbers(operator, input_count_range=[1, None],
                                       output_count_range=1)

        operator.outputs[0].type.shape = [N, 1]
    else:
        raise RuntimeError(
            "New kind of SVM, no shape calculer exist for '{}'.".format(
                operator.type)) 
Example #22
Source File: nusvm.py    From driverlessai-recipes with Apache License 2.0 4 votes vote down vote up
def fit(self, X, y, sample_weight=None, eval_set=None, sample_weight_eval_set=None, **kwargs):
        X = dt.Frame(X)

        orig_cols = list(X.names)

        if self.num_classes >= 2:
            feature_model = NuSVC(kernel='linear', nu=self.params['nu'])
            model = NuSVC(nu=self.params['nu'], kernel=self.params['kernel'],
                          degree=self.params['degree'], probability=self.params['probability'])

            lb = LabelEncoder()
            lb.fit(self.labels)
            y = lb.transform(y)
        else:
            feature_model = NuSVR(kernel='linear', nu=self.params['nu'])
            model = NuSVR(nu=self.params['nu'], kernel=self.params['kernel'],
                          degree=self.params['degree'])

        self.means = dict()

        for col in X.names:
            XX = X[:, col]
            self.means[col] = XX.mean1()
            if self.means[col] is None:
                self.means[col] = 0
            XX.replace(None, self.means[col])
            X[:, col] = XX
            assert X[dt.isna(dt.f[col]), col].nrows == 0

        X = X.to_numpy()

        # nu is infeasible sometimes
        # doing quaternary search on both sides of selected nu
        valid_nu = None
        while valid_nu is None:
            try:
                model.fit(X, y)
                valid_nu = self.params['nu']
            except:
                if self.params['nu'] > 0.5:
                    self.params['nu'] = 1.0 - self.params['nu']
                else:
                    self.params['nu'] = (4.0 - 3.0 * self.params['nu']) / 4.0
                if self.num_classes >= 2:
                    feature_model = NuSVC(kernel='linear', nu=self.params['nu'])
                    model = NuSVC(nu=self.params['nu'], kernel=self.params['kernel'],
                                  degree=self.params['degree'], probability=self.params['probability'])
                else:
                    feature_model = NuSVR(kernel='linear', nu=self.params['nu'])
                    model = NuSVR(nu=self.params['nu'], kernel=self.params['kernel'],
                                  degree=self.params['degree'])

        feature_model.fit(X, y)
        importances = np.array(abs(feature_model.coef_)).ravel()

        self.set_model_properties(model=model,
                                  features=orig_cols,
                                  importances=importances.tolist(),
                                  iterations=0) 
Example #23
Source File: test_svm.py    From Mastering-Elasticsearch-7.0 with MIT License 4 votes vote down vote up
def test_bad_input():
    # Test that it gives proper exception on deficient input
    # impossible value of C
    assert_raises(ValueError, svm.SVC(gamma='scale', C=-1).fit, X, Y)

    # impossible value of nu
    clf = svm.NuSVC(gamma='scale', nu=0.0)
    assert_raises(ValueError, clf.fit, X, Y)

    Y2 = Y[:-1]  # wrong dimensions for labels
    assert_raises(ValueError, clf.fit, X, Y2)

    # Test with arrays that are non-contiguous.
    for clf in (svm.SVC(gamma="scale"), svm.LinearSVC(random_state=0)):
        Xf = np.asfortranarray(X)
        assert not Xf.flags['C_CONTIGUOUS']
        yf = np.ascontiguousarray(np.tile(Y, (2, 1)).T)
        yf = yf[:, -1]
        assert not yf.flags['F_CONTIGUOUS']
        assert not yf.flags['C_CONTIGUOUS']
        clf.fit(Xf, yf)
        assert_array_equal(clf.predict(T), true_result)

    # error for precomputed kernelsx
    clf = svm.SVC(kernel='precomputed')
    assert_raises(ValueError, clf.fit, X, Y)

    # sample_weight bad dimensions
    clf = svm.SVC(gamma="scale")
    assert_raises(ValueError, clf.fit, X, Y, sample_weight=range(len(X) - 1))

    # predict with sparse input when trained with dense
    clf = svm.SVC(gamma="scale").fit(X, Y)
    assert_raises(ValueError, clf.predict, sparse.lil_matrix(X))

    Xt = np.array(X).T
    clf.fit(np.dot(X, Xt), Y)
    assert_raises(ValueError, clf.predict, X)

    clf = svm.SVC(gamma="scale")
    clf.fit(X, Y)
    assert_raises(ValueError, clf.predict, Xt) 
Example #24
Source File: test_svm.py    From twitter-stock-recommendation with MIT License 4 votes vote down vote up
def test_bad_input():
    # Test that it gives proper exception on deficient input
    # impossible value of C
    assert_raises(ValueError, svm.SVC(C=-1).fit, X, Y)

    # impossible value of nu
    clf = svm.NuSVC(nu=0.0)
    assert_raises(ValueError, clf.fit, X, Y)

    Y2 = Y[:-1]  # wrong dimensions for labels
    assert_raises(ValueError, clf.fit, X, Y2)

    # Test with arrays that are non-contiguous.
    for clf in (svm.SVC(), svm.LinearSVC(random_state=0)):
        Xf = np.asfortranarray(X)
        assert_false(Xf.flags['C_CONTIGUOUS'])
        yf = np.ascontiguousarray(np.tile(Y, (2, 1)).T)
        yf = yf[:, -1]
        assert_false(yf.flags['F_CONTIGUOUS'])
        assert_false(yf.flags['C_CONTIGUOUS'])
        clf.fit(Xf, yf)
        assert_array_equal(clf.predict(T), true_result)

    # error for precomputed kernelsx
    clf = svm.SVC(kernel='precomputed')
    assert_raises(ValueError, clf.fit, X, Y)

    # sample_weight bad dimensions
    clf = svm.SVC()
    assert_raises(ValueError, clf.fit, X, Y, sample_weight=range(len(X) - 1))

    # predict with sparse input when trained with dense
    clf = svm.SVC().fit(X, Y)
    assert_raises(ValueError, clf.predict, sparse.lil_matrix(X))

    Xt = np.array(X).T
    clf.fit(np.dot(X, Xt), Y)
    assert_raises(ValueError, clf.predict, X)

    clf = svm.SVC()
    clf.fit(X, Y)
    assert_raises(ValueError, clf.predict, Xt) 
Example #25
Source File: classifier.py    From python-sklearn-classifier-cookiecutter with MIT License 4 votes vote down vote up
def evaluate_classifier(X_train, X_test, y_train, y_test):
    '''
    Run multiple times with different classifiers to get an idea of the
    relative performance of each configuration.

    Returns a sequence of tuples containing:
        (title, precision, recall)
    for each learner.
    '''

    # Import some classifiers to test
    from sklearn.svm import LinearSVC, NuSVC
    from sklearn.ensemble import AdaBoostClassifier

    # We will calculate the P-R curve for each classifier
    from sklearn.metrics import precision_recall_curve, f1_score
    
    # Here we create classifiers with default parameters. These need
    # to be adjusted to obtain optimal performance on your data set.
    
    # Test the linear support vector classifier
    classifier = LinearSVC(C=1)
    # Fit the classifier
    classifier.fit(X_train, y_train)
    score = f1_score(y_test, classifier.predict(X_test))
    # Generate the P-R curve
    y_prob = classifier.decision_function(X_test)
    precision, recall, _ = precision_recall_curve(y_test, y_prob)
    # Include the score in the title
    yield 'Linear SVC (F1 score={:.3f})'.format(score), precision, recall

    # Test the Nu support vector classifier
    classifier = NuSVC(kernel='rbf', nu=0.5, gamma=1e-3)
    # Fit the classifier
    classifier.fit(X_train, y_train)
    score = f1_score(y_test, classifier.predict(X_test))
    # Generate the P-R curve
    y_prob = classifier.decision_function(X_test)
    precision, recall, _ = precision_recall_curve(y_test, y_prob)
    # Include the score in the title
    yield 'NuSVC (F1 score={:.3f})'.format(score), precision, recall

    # Test the Ada boost classifier
    classifier = AdaBoostClassifier(n_estimators=50, learning_rate=1.0, algorithm='SAMME.R')
    # Fit the classifier
    classifier.fit(X_train, y_train)
    score = f1_score(y_test, classifier.predict(X_test))
    # Generate the P-R curve
    y_prob = classifier.decision_function(X_test)
    precision, recall, _ = precision_recall_curve(y_test, y_prob)
    # Include the score in the title
    yield 'Ada Boost (F1 score={:.3f})'.format(score), precision, recall

# =====================================================================