我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用sklearn.metrics.zero_one_loss()。
def test_mdr_custom_score(): """Ensure that the MDR 'score' function outputs the right custom score passed in from the user""" features = np.array([[2, 0], [0, 0], [0, 1], [0, 0], [0, 0], [0, 0], [0, 1], [0, 0], [0, 0], [0, 1], [0, 0], [0, 0], [0, 0], [1, 1], [1, 1]]) classes = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0]) mdr = MDRClassifier() mdr.fit(features, classes) assert mdr.score(features = features, class_labels = classes, scoring_function = accuracy_score) == 12. / 15 assert mdr.score(features = features, class_labels = classes, scoring_function = zero_one_loss) == 1 - 12. / 15 assert mdr.score(features = features, class_labels = classes, scoring_function = zero_one_loss, normalize=False) == 15 - 12
def test_milboost_musk_fitting_lse(): c = MILBoostClassifier( base_estimator=DecisionTreeClassifier(max_depth=1), softmax=LogSumExponential(5.0), n_estimators=30, learning_rate=1.0 ) data = MUSK1() c.fit(data.data, data.labels) assert_array_less(c.estimator_errors_, 0.5) assert zero_one_loss(np.sign(data.labels), c.predict(data.data)) < 0.30
def test_milboost_hastie_fitting(): c = MILBoostClassifier( base_estimator=DecisionTreeClassifier(max_depth=1), softmax=LogSumExponential(5.0), n_estimators=30, learning_rate=1.0 ) data = Hastie_10_2() c.fit(data.data, data.labels) assert_array_less(c.estimator_errors_, 0.5) assert zero_one_loss(np.sign(data.labels), c.predict(data.data)) < 0.40
def test_logitboost_musk_fitting(): c = LogitBoostClassifier( base_estimator=DecisionTreeRegressor(max_depth=1), n_estimators=30, learning_rate=1.0 ) data = MUSK1() c.fit(data.data, np.sign(data.labels)) assert_array_less(c.estimator_errors_, 0.6) assert zero_one_loss(np.sign(data.labels), c.predict(data.data)) < 0.05
def test_logitboost_hastie_fitting(): c = LogitBoostClassifier( base_estimator=DecisionTreeRegressor(max_depth=1), n_estimators=30, learning_rate=1.0 ) data = Hastie_10_2() c.fit(data.data, np.sign(data.labels)) assert_array_less(c.estimator_errors_, 0.5) assert zero_one_loss(np.sign(data.labels), c.predict(data.data)) < 0.2
def test_gentleboost_musk_fitting(): c = GentleBoostClassifier( base_estimator=DecisionTreeRegressor(max_depth=1), n_estimators=30, learning_rate=1.0 ) data = MUSK1() c.fit(data.data, np.sign(data.labels)) assert_array_less(c.estimator_errors_, 0.5) assert zero_one_loss(np.sign(data.labels), c.predict(data.data)) < 0.1
def clf_bias_var(clf, X, y, n_replicas): roc_auc_scorer = get_scorer("roc_auc") # roc_auc_scorer(clf, X_test, y_test) auc_scores = [] error_scores = [] counts = np.zeros(X.shape[0], dtype = np.float64) sum_preds = np.zeros(X.shape[0], dtype = np.float64) for it in xrange(n_replicas): # generate train sets and test sets train_indices = np.random.randint(X.shape[0], size = X.shape[0]) # get test sets in_train = np.unique(train_indices) mask = np.ones(X.shape[0], dtype = np.bool) mask[in_train] = False test_indices = np.arange(X.shape[0])[mask] clf.fit(X[train_indices], y[train_indices]) auc_scores.append(roc_auc_scorer(clf, X[test_indices], y[test_indices])) error_scores.append(zero_one_loss(y[test_indices], clf.predict(X[test_indices]))) preds = clf.predict(X) for index in test_indices: counts[index] += 1 sum_preds[index] += preds[index] test_mask = (counts > 0) # indices of samples that have been tested # print('counts mean: {}'.format(np.mean(counts))) # print('counts standard derivation: {}'.format(np.std(counts))) bias, var = bias_var(y[test_mask], sum_preds[test_mask], counts[test_mask], n_replicas) return auc_scores, error_scores, bias, var
def test_zero_one_loss(): ''' test 0-1 loss function :return: None ''' y_true=[1,1,1,1,1,0,0,0,0,0] y_pred=[0,0,0,1,1,1,1,1,0,0] print("zero_one_loss<fraction>:",zero_one_loss(y_true,y_pred,normalize=True)) print("zero_one_loss<num>:",zero_one_loss(y_true,y_pred,normalize=False))
def test_multilabel_zero_one_loss_subset(): # Dense label indicator matrix format y1 = np.array([[0, 1, 1], [1, 0, 1]]) y2 = np.array([[0, 0, 1], [1, 0, 1]]) assert_equal(zero_one_loss(y1, y2), 0.5) assert_equal(zero_one_loss(y1, y1), 0) assert_equal(zero_one_loss(y2, y2), 0) assert_equal(zero_one_loss(y2, np.logical_not(y2)), 1) assert_equal(zero_one_loss(y1, np.logical_not(y1)), 1) assert_equal(zero_one_loss(y1, np.zeros(y1.shape)), 1) assert_equal(zero_one_loss(y2, np.zeros(y1.shape)), 1)
def test_rfecv(): generator = check_random_state(0) iris = load_iris() X = np.c_[iris.data, generator.normal(size=(len(iris.data), 6))] y = list(iris.target) # regression test: list should be supported # Test using the score function rfecv = RFECV(estimator=SVC(kernel="linear"), step=1, cv=5) rfecv.fit(X, y) # non-regression test for missing worst feature: assert_equal(len(rfecv.grid_scores_), X.shape[1]) assert_equal(len(rfecv.ranking_), X.shape[1]) X_r = rfecv.transform(X) # All the noisy variable were filtered out assert_array_equal(X_r, iris.data) # same in sparse rfecv_sparse = RFECV(estimator=SVC(kernel="linear"), step=1, cv=5) X_sparse = sparse.csr_matrix(X) rfecv_sparse.fit(X_sparse, y) X_r_sparse = rfecv_sparse.transform(X_sparse) assert_array_equal(X_r_sparse.toarray(), iris.data) # Test using a customized loss function scoring = make_scorer(zero_one_loss, greater_is_better=False) rfecv = RFECV(estimator=SVC(kernel="linear"), step=1, cv=5, scoring=scoring) ignore_warnings(rfecv.fit)(X, y) X_r = rfecv.transform(X) assert_array_equal(X_r, iris.data) # Test using a scorer scorer = get_scorer('accuracy') rfecv = RFECV(estimator=SVC(kernel="linear"), step=1, cv=5, scoring=scorer) rfecv.fit(X, y) X_r = rfecv.transform(X) assert_array_equal(X_r, iris.data) # Test fix on grid_scores def test_scorer(estimator, X, y): return 1.0 rfecv = RFECV(estimator=SVC(kernel="linear"), step=1, cv=5, scoring=test_scorer) rfecv.fit(X, y) assert_array_equal(rfecv.grid_scores_, np.ones(len(rfecv.grid_scores_))) # Same as the first two tests, but with step=2 rfecv = RFECV(estimator=SVC(kernel="linear"), step=2, cv=5) rfecv.fit(X, y) assert_equal(len(rfecv.grid_scores_), 6) assert_equal(len(rfecv.ranking_), X.shape[1]) X_r = rfecv.transform(X) assert_array_equal(X_r, iris.data) rfecv_sparse = RFECV(estimator=SVC(kernel="linear"), step=2, cv=5) X_sparse = sparse.csr_matrix(X) rfecv_sparse.fit(X_sparse, y) X_r_sparse = rfecv_sparse.transform(X_sparse) assert_array_equal(X_r_sparse.toarray(), iris.data)