当前位置: 首页>>代码示例>>Python>>正文


Python LinearSVC.decision_function方法代码示例

本文整理汇总了Python中sklearn.svm.LinearSVC.decision_function方法的典型用法代码示例。如果您正苦于以下问题:Python LinearSVC.decision_function方法的具体用法?Python LinearSVC.decision_function怎么用?Python LinearSVC.decision_function使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sklearn.svm.LinearSVC的用法示例。


在下文中一共展示了LinearSVC.decision_function方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: LinearSVCStep

# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import decision_function [as 别名]
class LinearSVCStep(SklearnStep):
    def __init__(self, c=1.0):
        super(LinearSVCStep, self).__init__()

        self._c = c

    def fit_transform(self):
        self._model = LinearSVC(C=self._c)
        x, y = load_svmlight(self.input_path)
        self._model.fit(x, y)
        scores = self._model.decision_function(x)
        save_numpy_txt(scores, self.output_path)

    def transform(self, x=None):
        if x is None:
            _x, y = load_svmlight(self._test_input_path)
            conf_x = self._model.decision_function(_x)
            predicted_x = self._model.predict(_x)
            res = np.vstack((y, conf_x, predicted_x)).T
            save_numpy_txt(res, self._test_output_path)
        else:
            transformed_x = self._model.decision_function(x)
            return transformed_x

    def predict(self, x):
        return self._model.predict(x)

    def decision_function(self, x):
        return self._model.decision_function(x)

    def get_param(self):
        return {'c': self._c}
开发者ID:myungchoi,项目名称:client-py,代码行数:34,代码来源:classifier.py

示例2: evaluate_classifier

# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import decision_function [as 别名]
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
开发者ID:hcbgtd,项目名称:MyTest,代码行数:54,代码来源:classifier.py

示例3: test_grid_search_correct_score_results

# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import decision_function [as 别名]
def test_grid_search_correct_score_results():
    # test that correct scores are used
    n_splits = 3
    clf = LinearSVC(random_state=0)
    X, y = make_blobs(random_state=0, centers=2)
    Cs = [.1, 1, 10]
    for score in ['f1', 'roc_auc']:
        grid_search = GridSearchCV(clf, {'C': Cs}, scoring=score, cv=n_splits)
        results = grid_search.fit(X, y).cv_results_

        # Test scorer names
        result_keys = list(results.keys())
        expected_keys = (("mean_test_score", "rank_test_score") +
                         tuple("split%d_test_score" % cv_i
                               for cv_i in range(n_splits)))
        assert_true(all(in1d(expected_keys, result_keys)))

        cv = StratifiedKFold(n_splits=n_splits)
        n_splits = grid_search.n_splits_
        for candidate_i, C in enumerate(Cs):
            clf.set_params(C=C)
            cv_scores = np.array(
                list(grid_search.cv_results_['split%d_test_score'
                                             % s][candidate_i]
                     for s in range(n_splits)))
            for i, (train, test) in enumerate(cv.split(X, y)):
                clf.fit(X[train], y[train])
                if score == "f1":
                    correct_score = f1_score(y[test], clf.predict(X[test]))
                elif score == "roc_auc":
                    dec = clf.decision_function(X[test])
                    correct_score = roc_auc_score(y[test], dec)
                assert_almost_equal(correct_score, cv_scores[i])
开发者ID:YinongLong,项目名称:scikit-learn,代码行数:35,代码来源:test_search.py

示例4: SVC_on_fold

# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import decision_function [as 别名]
def SVC_on_fold(feature_sets, train, test, y, y_all, X, dim, dimsum, learn_options):
    y_bin = y_all[learn_options["binary target name"]].values[:, None]
    clf = LinearSVC(penalty="l2", dual=False)
    clf.fit(X[train], y_bin[train].flatten())
    # y_pred = clf.predict(X[test])[:, None] # this returns 0/1
    y_pred = clf.decision_function(X[test])[:, None]
    return y_pred, clf
开发者ID:MicrosoftResearch,项目名称:Azimuth,代码行数:9,代码来源:baselines.py

示例5: svm_binary_svc_probability

# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import decision_function [as 别名]
def svm_binary_svc_probability(X, Y, C):
    allp = np.sum(Y>0);
    alln = len(Y) - allp;
    nr_fold = 5;
    perm = list(range(len(Y)));
    random.shuffle(perm);
    dec_values = np.zeros(len(Y), dtype=np.float32);
    for i in range(nr_fold):
        start = i * len(Y) // nr_fold;
        end   = (i+1) * len(Y) // nr_fold;
        trainL = [perm[j] for j in range(len(Y)) if j not in range(start, end)];
        testL  = perm[start:end];
        trainX = X[trainL,:];
        trainY = Y[trainL];
        p_count = np.sum(trainY>0);
        n_count = len(trainY) - p_count;
        if p_count==0 and n_count==0:
            dec_values[start:end] = 0.0;
        elif p_count > 0 and n_count == 0:
            dec_values[start:end] = 1.0;
        elif p_count == 0 and n_count > 0:
            dec_values[start:end] = -1.0;
        else :
            subclf = LinearSVC(C=C, class_weight={1:allp,-1:alln});
            subclf.fit(trainX, trainY);
            dec_values[testL] = subclf.decision_function(X[testL,:]).ravel();
    return sigmoid_train(dec_values, Y);
开发者ID:domainxz,项目名称:pytools,代码行数:29,代码来源:dv2p.py

示例6: try_lvc_clf

# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import decision_function [as 别名]
def try_lvc_clf(train_X,train_y,test_X,test_y):

    train_X=scale(train_X)

    lvc=LinearSVC(C=0.1)
    lvc.fit(train_X,train_y)
    
    dec_y=lvc.decision_function(train_X)
    
    #choose the smallest 90%
    num_sel=int(len(dec_y)*0.8)
    assert len(dec_y)==train_X.shape[0]
    assert num_sel<=train_X.shape[0]
    
    s_idx=np.argsort(np.abs(dec_y))
    
    assert len(s_idx)==train_X.shape[0]

    for i in s_idx:
        if np.isnan(train_y[i])==True:
            print("smoking index:%s"%i)

    n_train_X=train_X[s_idx[0:num_sel],:]
    n_train_y=train_y[s_idx[0:num_sel]]


    n_train_X=scale(n_train_X)

    lvc.fit(n_train_X,n_train_y)
    

    test_X=scale(test_X)
    pred_y=lvc.predict(test_X)
    return pred_y
开发者ID:kanhua,项目名称:Enron-Email-Fraud,代码行数:36,代码来源:feature+preprocessing+and+selection.py

示例7: SVM

# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import decision_function [as 别名]
def SVM(data_train, data_train_vectors, data_test_vectors, **kwargs):
    # Implementing classification model- using LinearSVC
    clf_svc =  LinearSVC()
    clf_svc.fit(data_train_vectors, data_train.target)
    y_pred_score = clf_svc.decision_function(data_test_vectors)
    
    return y_pred_score
开发者ID:RaoUmer,项目名称:docs_classification,代码行数:9,代码来源:ml_docs_classification_2.py

示例8: train

# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import decision_function [as 别名]
    def train(self, messageList):
        # --- prepare variables
        vectorizer = self._vectorizer
        balance = self._balance
        C = self._C
        tol = self._tol

        # ---
        msgList = map(lambda x: x[0], messageList)
        y = np.array(map(lambda x: 1 if x[1] == "pos" else 0, messageList))
        X = vectorizer.fitTransform(msgList)

        class_weight = "auto" if balance else None
        classifier = LinearSVC(
            C=C, loss="l2", penalty="l1", dual=False, tol=tol, random_state=0, class_weight=class_weight
        )

        classifier.fit(X, y)

        # - learn sigmoid
        yDeci = classifier.decision_function(X)
        yy = [1 if v == 1 else -1 for v in y]
        [A, B] = platt.SigmoidTrain(yDeci, yy)
        plattModel = [A, B]

        # - save to instance variable
        self._classifier = classifier
        self._plattModel = plattModel
        pass
开发者ID:pombredanne,项目名称:ushine-learning,代码行数:31,代码来源:classifier.py

示例9: test_calibration_multiclass

# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import decision_function [as 别名]
def test_calibration_multiclass():
    """Test calibration for multiclass """
    # test multi-class setting with classifier that implements
    # only decision function
    clf = LinearSVC()
    X, y_idx = make_blobs(n_samples=100, n_features=2, random_state=42,
                          centers=3, cluster_std=3.0)

    # Use categorical labels to check that CalibratedClassifierCV supports
    # them correctly
    target_names = np.array(['a', 'b', 'c'])
    y = target_names[y_idx]

    X_train, y_train = X[::2], y[::2]
    X_test, y_test = X[1::2], y[1::2]

    clf.fit(X_train, y_train)
    for method in ['isotonic', 'sigmoid']:
        cal_clf = CalibratedClassifierCV(clf, method=method, cv=2)
        cal_clf.fit(X_train, y_train)
        probas = cal_clf.predict_proba(X_test)
        assert_array_almost_equal(np.sum(probas, axis=1), np.ones(len(X_test)))

        # Check that log-loss of calibrated classifier is smaller than
        # log-loss of naively turned OvR decision function to probabilities
        # via softmax
        def softmax(y_pred):
            e = np.exp(-y_pred)
            return e / e.sum(axis=1).reshape(-1, 1)

        uncalibrated_log_loss = \
            log_loss(y_test, softmax(clf.decision_function(X_test)))
        calibrated_log_loss = log_loss(y_test, probas)
        assert_greater_equal(uncalibrated_log_loss, calibrated_log_loss)

    # Test that calibration of a multiclass classifier decreases log-loss
    # for RandomForestClassifier
    X, y = make_blobs(n_samples=100, n_features=2, random_state=42,
                      cluster_std=3.0)
    X_train, y_train = X[::2], y[::2]
    X_test, y_test = X[1::2], y[1::2]

    clf = RandomForestClassifier(n_estimators=10, random_state=42)
    clf.fit(X_train, y_train)
    clf_probs = clf.predict_proba(X_test)
    loss = log_loss(y_test, clf_probs)

    for method in ['isotonic', 'sigmoid']:
        cal_clf = CalibratedClassifierCV(clf, method=method, cv=3)
        cal_clf.fit(X_train, y_train)
        cal_clf_probs = cal_clf.predict_proba(X_test)
        cal_loss = log_loss(y_test, cal_clf_probs)
        assert_greater(loss, cal_loss)
开发者ID:abecadel,项目名称:scikit-learn,代码行数:55,代码来源:test_calibration.py

示例10: trainLinearSVC

# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import decision_function [as 别名]
	def trainLinearSVC(self, feats, y_attribute, dec=False):
		X,y = self.fe.getFeaturesMatrix('train',feats,y_attribute)
		X_test,y_true = self.fe.getFeaturesMatrix('test',feats,y_attribute)
		
		clf = LinearSVC()
		clf = clf.fit(X,y)
		if dec:
			y_pr = clf.decision_function(X_test)
			return clf, y_pr
		else:
			y_pr = clf.predict(X_test)
			return clf, y_pr
开发者ID:rahul003,项目名称:stance-detection,代码行数:14,代码来源:main.py

示例11: LinearSvmModel

# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import decision_function [as 别名]
class LinearSvmModel(BaseEstimator, RocScorerMixin):
    def __init__(self):
        self.model = LinearSVC(C=0.0000000001, loss='l1', penalty='l2')

    def fit(self, X, y):
        self.scaler = preprocessing.StandardScaler().fit(X)
        X = self.scaler.transform(X)
        self.model.fit(X, y)

    def predict(self, X_test):
        X_test = self.scaler.transform(X_test)
        scores = self.model.decision_function(X_test)
        scores.shape = scores.shape[0]
        return scores
开发者ID:srivathsmit,项目名称:robotVshuman,代码行数:16,代码来源:common.py

示例12: __init__

# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import decision_function [as 别名]
class Model_SVC:

  def __init__(self, trainX, trainY, seed):
    self.model = LinearSVC(
        loss='l2',
        C=0.1,
        fit_intercept=True,
        intercept_scaling=1,
        random_state=seed
    )
    self.model.fit(trainX, trainY)

  def predict(self, testX):
    predictions = self.model.decision_function(testX)
    predictions[predictions > 100] = 100 # large number bug in expit
    return expit(predictions)
开发者ID:Manasvini-BS,项目名称:CMPE272Project,代码行数:18,代码来源:model_def.py

示例13: train_custom_one_vs_all

# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import decision_function [as 别名]
def train_custom_one_vs_all(X_train,X_test,Y_train,topk):

    #convert matrix to row for efficient splicing
    Y_train = Y_train.tocsc()
    tag_classifiers = []
    num_training,numclasses = Y_train.shape
    num_test_examples = X_test.shape[0]


    # hold a vector mxk, containing top k prediction classes for each example, maintain m heaps for that
    num_examples = X_test.shape[0]
    num_classes = len(tag_classifiers)
    topk_class_distances = []
    for i in xrange(num_examples):
        heap = []
        topk_class_distances += [heap]
    

    for j in xrange(numclasses):
        # train on each class label for all the training examples
        y = numpy.ravel(Y_train.getcol(j).todense());

        clf = LinearSVC(C=0.8,loss='l2')
    
        clf.fit(X_train,y);
        print "Trained for class",j
        # get the decision for all test examples
        decision = clf.decision_function(X_test)
        # for each test example add its decision value to the heap of top k decision values
        for i in xrange(num_test_examples):
            h = topk_class_distances[i]
            if len(h) < topk: heapq.heappush(h,(decision[i],j))
            else:             heapq.heappushpop(h,(decision[i],j))
        print "Predicted for class",j

    #clean the decision values and store the class labels
    class_label_indices = []
    for i in xrange(num_examples):
        topk_labels = [label for dist,label in topk_class_distances[i]]
        class_label_indices += [topk_labels]

    return class_label_indices
开发者ID:adirastogi,项目名称:so_project,代码行数:44,代码来源:multi_class_classifier_fp.py

示例14: evaluate

# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import decision_function [as 别名]
    def evaluate(self):
        """
        Returns True on success

        Arguments:

        Keyword arguments:
       
        """
        svm = LinearSVC(C=self.C, class_weight=self.class_weight)
        svm.fit(self.XTrain, self.YTrain)
        prob = svm.decision_function(self.XTest)
        # To make the binary case consistent with non-binary cases
        if prob.ndim == 1:
            prob = np.vstack((-prob, prob)).T
        YPred = svm.predict(self.XTest)
        joblib.dump(YPred, self.output_path + '/ypred.jbl')
        joblib.dump(prob, self.output_path + '/prob.jbl')
                    
        return True
开发者ID:dchall88,项目名称:DIGITS,代码行数:22,代码来源:svm_train_test.py

示例15: linearsvc_outlier_rm

# 需要导入模块: from sklearn.svm import LinearSVC [as 别名]
# 或者: from sklearn.svm.LinearSVC import decision_function [as 别名]
def linearsvc_outlier_rm(train_X,train_y,discard=0.1,lvc_C=0.1,take_abs=True):
    """
    Remove the outliers in the data.
    It rescaled the data, use linear SVC to do the classification,
    and then remove the data with farthest distances
    :param train_X: train data
    :param train_y: label
    :param discard: the ratio of the outliers to be removed
    :return: tuple of new X,y
    """

    assert isinstance(train_X,np.ndarray)
    assert isinstance(train_y,np.ndarray)

    # LinearSVC requires the features to be scaled
    # Here we scaled the input data, but the output data are note rescaled
    scaled_train_X=scale(train_X)

    lvc=LinearSVC(C=lvc_C)
    lvc.fit(scaled_train_X,train_y)

    dec_y=lvc.decision_function(scaled_train_X)

    #choose the smallest 90%
    num_sel=int(len(dec_y)*(1-discard))
    assert len(dec_y)==scaled_train_X.shape[0]
    assert num_sel<=scaled_train_X.shape[0]

    if take_abs==True:
        s_idx=np.argsort(np.abs(dec_y))
    else:
        s_idx=np.argsort(dec_y)

    assert len(s_idx)==scaled_train_X.shape[0]


    n_train_X=train_X[s_idx[0:num_sel],:]
    n_train_y=train_y[s_idx[0:num_sel]]

    return n_train_X,n_train_y,dec_y
开发者ID:kanhua,项目名称:Enron-Email-Fraud,代码行数:42,代码来源:preprocess_data.py


注:本文中的sklearn.svm.LinearSVC.decision_function方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。