• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python utils.compute_class_weight函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中sklearn.utils.compute_class_weight函数的典型用法代码示例。如果您正苦于以下问题:Python compute_class_weight函数的具体用法?Python compute_class_weight怎么用?Python compute_class_weight使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



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

示例1: classify

    def classify(self):
        y_data = self.get_result(self.task.label)
        X_data = self.get_result(self.task.features)

        y = np.array(y_data.data).ravel()
        X = np.array(pd.get_dummies(X_data.data))
        #X = MinMaxScaler().fit_transform(X)

        X_train = X[:-TILE_SIZE]
        y_train = y[:-TILE_SIZE]
        X_test = X[-TILE_SIZE:]
        y_test = y[-TILE_SIZE:]

        cw = compute_class_weight('auto', np.array([0,1]), y)
        cw = {0:cw[0],1:cw[1]}

        b = get_classifier(self.task.classifier, cw)
        b.partial_fit(X_train, y_train, classes=np.array([0,1]))

        y_prob = None
        y_pred = None
        if self.task.classifier in ['perceptron','svm']:
            y_pred = b.predict(X_test)
            y_prob = np.array([[0,y] for y in y_pred])
        else:
            y_prob = b.predict_proba(X_test)
            y_pred = [1 if t[0] >= 0.5 else 0 for t in y_prob]

        cm = confusion_matrix(y_test, y_pred)
        stats = classify_stats(cm, y_test, y_prob, TILE_SIZE)

        result = ClassifyResult(self.task, 1.0, b, stats)
        self.results[self.task.uuid] = result
开发者ID:twareproj,项目名称:tware,代码行数:33,代码来源:executor.py


示例2: test_auto_weight

def test_auto_weight():
    # Test class weights for imbalanced data
    from sklearn.linear_model import LogisticRegression
    # We take as dataset the two-dimensional projection of iris so
    # that it is not separable and remove half of predictors from
    # class 1.
    # We add one to the targets as a non-regression test: class_weight="balanced"
    # used to work only when the labels where a range [0..K).
    from sklearn.utils import compute_class_weight
    X, y = iris.data[:, :2], iris.target + 1
    unbalanced = np.delete(np.arange(y.size), np.where(y > 2)[0][::2])

    classes = np.unique(y[unbalanced])
    class_weights = compute_class_weight('balanced', classes, y[unbalanced])
    assert_true(np.argmax(class_weights) == 2)

    for clf in (svm.SVC(kernel='linear'), svm.LinearSVC(random_state=0),
                LogisticRegression()):
        # check that score is better when class='balanced' is set.
        y_pred = clf.fit(X[unbalanced], y[unbalanced]).predict(X)
        clf.set_params(class_weight='balanced')
        y_pred_balanced = clf.fit(X[unbalanced], y[unbalanced],).predict(X)
        assert_true(metrics.f1_score(y, y_pred, average='weighted')
                    <= metrics.f1_score(y, y_pred_balanced,
                                        average='weighted'))
开发者ID:abhisg,项目名称:scikit-learn,代码行数:25,代码来源:test_svm.py


示例3: test_multiclass_classifier_class_weight

def test_multiclass_classifier_class_weight():
    """tests multiclass with classweights for each class"""
    alpha = .1
    n_samples = 20
    tol = .00001
    max_iter = 50
    class_weight = {0: .45, 1: .55, 2: .75}
    fit_intercept = True
    X, y = make_blobs(n_samples=n_samples, centers=3, random_state=0,
                      cluster_std=0.1)
    step_size = get_step_size(X, alpha, fit_intercept, classification=True)
    classes = np.unique(y)

    clf1 = LogisticRegression(solver='sag', C=1. / alpha / n_samples,
                              max_iter=max_iter, tol=tol, random_state=77,
                              fit_intercept=fit_intercept,
                              class_weight=class_weight)
    clf2 = clone(clf1)
    clf1.fit(X, y)
    clf2.fit(sp.csr_matrix(X), y)

    le = LabelEncoder()
    class_weight_ = compute_class_weight(class_weight, np.unique(y), y)
    sample_weight = class_weight_[le.fit_transform(y)]

    coef1 = []
    intercept1 = []
    coef2 = []
    intercept2 = []
    for cl in classes:
        y_encoded = np.ones(n_samples)
        y_encoded[y != cl] = -1

        spweights1, spintercept1 = sag_sparse(X, y_encoded, step_size, alpha,
                                              n_iter=max_iter, dloss=log_dloss,
                                              sample_weight=sample_weight)
        spweights2, spintercept2 = sag_sparse(X, y_encoded, step_size, alpha,
                                              n_iter=max_iter, dloss=log_dloss,
                                              sample_weight=sample_weight,
                                              sparse=True)
        coef1.append(spweights1)
        intercept1.append(spintercept1)
        coef2.append(spweights2)
        intercept2.append(spintercept2)

    coef1 = np.vstack(coef1)
    intercept1 = np.array(intercept1)
    coef2 = np.vstack(coef2)
    intercept2 = np.array(intercept2)

    for i, cl in enumerate(classes):
        assert_array_almost_equal(clf1.coef_[i].ravel(),
                                  coef1[i].ravel(),
                                  decimal=2)
        assert_almost_equal(clf1.intercept_[i], intercept1[i], decimal=1)

        assert_array_almost_equal(clf2.coef_[i].ravel(),
                                  coef2[i].ravel(),
                                  decimal=2)
        assert_almost_equal(clf2.intercept_[i], intercept2[i], decimal=1)
开发者ID:AlexisMignon,项目名称:scikit-learn,代码行数:60,代码来源:test_sag.py


示例4: load_training_data

def load_training_data():
    raw_training_data = pd.read_csv('train.csv')

    # convert types to ints
    raw_training_data['target'] = raw_training_data['target'].apply(class_to_int)
    raw_training_data = raw_training_data.astype('float32')
    raw_training_data['target'] = raw_training_data['target'].astype('int32')

    raw_training_data = raw_training_data.iloc[np.random.permutation(len(raw_training_data))] #shuffle data
    # Get the features and the classes
    features = np.log(raw_training_data.iloc[:, 1:94] + 1).values # apply log function

    classes = raw_training_data['target'].values

    print np.unique(classes)

    #split train/validate
    feat_train, feat_test, class_train, class_test = cross_validation.train_test_split(features, classes,
                                                                                       test_size=0.3,
                                                                                       random_state=1232)

    feat_train, feat_val, class_train, class_val = cross_validation.train_test_split(feat_train, class_train,
                                                                                     test_size=0.3,
                                                                                     random_state=1232)


    #scale the features
    std_scale = preprocessing.StandardScaler().fit(feat_train)
    feat_train = std_scale.transform(feat_train)
    feat_val = std_scale.transform(feat_val)
    feat_test = std_scale.transform(feat_test)

    #class weights
    weights = compute_class_weight('auto', np.unique(classes), class_train)
    weights = weights.astype('float32')
    print weights
    train_weights = []
    val_weights = []
    for i in class_train:
        train_weights.append(weights[i])

    for i in list(class_val):
        val_weights.append(weights[i])

    #convert to np array for theanets
    training_data = [feat_train, class_train, np.array(train_weights)]
    validation_data = [feat_val, class_val, np.array(val_weights)]
    test_data = [feat_test, class_test]

    return training_data, validation_data, test_data, std_scale
开发者ID:maym86,项目名称:otto_theanets,代码行数:50,代码来源:theanets_test.py


示例5: fit

    def fit(self, X, y):
        from sklearn.preprocessing import LabelEncoder
        from sklearn.utils import compute_class_weight

        label_encoder = LabelEncoder().fit(y)
        classes = label_encoder.classes_
        class_weight = compute_class_weight(self.class_weight, classes, y)

        # Intentionally modify the balanced class_weight
        # to simulate a bug and raise an exception
        if self.class_weight == "balanced":
            class_weight += 1.

        # Simply assigning coef_ to the class_weight
        self.coef_ = class_weight
        return self
开发者ID:daniel-perry,项目名称:scikit-learn,代码行数:16,代码来源:test_estimator_checks.py


示例6: test_binary_classifier_class_weight

def test_binary_classifier_class_weight():
    """tests binary classifier with classweights for each class"""
    alpha = .1
    n_samples = 50
    n_iter = 20
    tol = .00001
    fit_intercept = True
    X, y = make_blobs(n_samples=n_samples, centers=2, random_state=10,
                      cluster_std=0.1)
    step_size = get_step_size(X, alpha, fit_intercept, classification=True)
    classes = np.unique(y)
    y_tmp = np.ones(n_samples)
    y_tmp[y != classes[1]] = -1
    y = y_tmp

    class_weight = {1: .45, -1: .55}
    clf1 = LogisticRegression(solver='sag', C=1. / alpha / n_samples,
                              max_iter=n_iter, tol=tol, random_state=77,
                              fit_intercept=fit_intercept,
                              class_weight=class_weight)
    clf2 = clone(clf1)

    clf1.fit(X, y)
    clf2.fit(sp.csr_matrix(X), y)

    le = LabelEncoder()
    class_weight_ = compute_class_weight(class_weight, np.unique(y), y)
    sample_weight = class_weight_[le.fit_transform(y)]
    spweights, spintercept = sag_sparse(X, y, step_size, alpha, n_iter=n_iter,
                                        dloss=log_dloss,
                                        sample_weight=sample_weight,
                                        fit_intercept=fit_intercept)
    spweights2, spintercept2 = sag_sparse(X, y, step_size, alpha,
                                          n_iter=n_iter,
                                          dloss=log_dloss, sparse=True,
                                          sample_weight=sample_weight,
                                          fit_intercept=fit_intercept)

    assert_array_almost_equal(clf1.coef_.ravel(),
                              spweights.ravel(),
                              decimal=2)
    assert_almost_equal(clf1.intercept_, spintercept, decimal=1)

    assert_array_almost_equal(clf2.coef_.ravel(),
                              spweights2.ravel(),
                              decimal=2)
    assert_almost_equal(clf2.intercept_, spintercept2, decimal=1)
开发者ID:AlexisMignon,项目名称:scikit-learn,代码行数:47,代码来源:test_sag.py


示例7: test_auto_weight

def test_auto_weight():
    """Test class weights for imbalanced data"""
    from sklearn.linear_model import LogisticRegression
    # we take as dataset a the two-dimensional projection of iris so
    # that it is not separable and remove half of predictors from
    # class 1
    from sklearn.utils import compute_class_weight
    X, y = iris.data[:, :2], iris.target
    unbalanced = np.delete(np.arange(y.size), np.where(y > 1)[0][::2])

    classes = np.unique(y[unbalanced])
    class_weights = compute_class_weight('auto', classes, y[unbalanced])
    assert_true(np.argmax(class_weights) == 2)

    for clf in (svm.SVC(kernel='linear'), svm.LinearSVC(random_state=0),
                LogisticRegression()):
        # check that score is better when class='auto' is set.
        y_pred = clf.fit(X[unbalanced], y[unbalanced]).predict(X)
        clf.set_params(class_weight='auto')
        y_pred_balanced = clf.fit(X[unbalanced], y[unbalanced],).predict(X)
        assert_true(metrics.f1_score(y, y_pred)
                    <= metrics.f1_score(y, y_pred_balanced))
开发者ID:RONNCC,项目名称:scikit-learn,代码行数:22,代码来源:test_svm.py


示例8: _compute_class_weight_dictionary

def _compute_class_weight_dictionary(y):
    # helper for returning a dictionary instead of an array
    classes = np.unique(y)
    class_weight = compute_class_weight("balanced", classes, y)
    class_weight_dict = dict(zip(classes, class_weight))
    return class_weight_dict
开发者ID:huafengw,项目名称:scikit-learn,代码行数:6,代码来源:test_logistic.py


示例9: load_iris

from sklearn.svm import LinearSVC
from sklearn.metrics import average_precision_score
from sklearn.utils import compute_class_weight
import numpy as np
import logging


logging.basicConfig(level=logging.DEBUG)

iris = load_iris()
X = iris.data
y = iris.target
y[y != 1] = -1
y[y == 1] = 1

weights = compute_class_weight("auto", np.unique(y), y)
sample_weight = np.zeros(y.shape, dtype=np.float)
sample_weight[y==1] = weights[0]
sample_weight[y==-1] = weights[1]

# n_iter = int(1e6 / X.shape[0])
vw_clf = VWClassifier(quiet=False, loss_function="hinge", passes=500)
vw_clf.fit(X, y.astype(np.double), sample_weight)
scores = vw_clf.decision_function(X)
print "VW AP: %.3f" % average_precision_score(y, scores)

vw_clf.set_params(l2=0.1)
vw_clf.fit(X, y.astype(np.double), sample_weight)
print "VW AP: %.3f" % average_precision_score(y, scores)

# vw_clf.fit(X, y.astype(np.double), sample_weight)
开发者ID:chicham,项目名称:pyvw,代码行数:31,代码来源:test_classifier.py


示例10: print

# path to image folder
base_path = os.path.join(base_path, caltech101.config.tar_inner_dirname)

# X_test contain only paths to images
(X_test, y_test) = util.load_paths_from_files(base_path, 'X_test.txt', 'y_test.txt')

for cv_fold in [0]: # on which cross val folds to run; cant loop over several folds due to some bug
    print("fold {}".format(cv_fold))

    experiment_name = '_bn_triangular_cv{}_e{}'.format(cv_fold, nb_epoch)

    # load cross val split
    (X_train, y_train), (X_val, y_val) = util.load_cv_split_paths(base_path, cv_fold)

    # compute class weights, since classes are highly imbalanced
    class_weight = compute_class_weight('auto', range(nb_classes), y_train)

    if normalize_data:
        print("Load mean and std...")
        X_mean, X_std = util.load_cv_stats(base_path, cv_fold)
        normalize_data = (X_mean, X_std)

    nb_train_sample = X_train.shape[0]
    nb_val_sample = X_val.shape[0]
    nb_test_sample = X_test.shape[0]

    print('X_train shape:', X_train.shape)
    print(nb_train_sample, 'train samples')
    if X_val is not None:
        print(nb_val_sample, 'validation samples')
    print(nb_test_sample, 'test samples')
开发者ID:bentanust,项目名称:ini_caltech101,代码行数:31,代码来源:caltech101_cnn_training.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python utils.gen_even_slices函数代码示例发布时间:2022-05-27
下一篇:
Python utils.column_or_1d函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap