【问题标题】:Values of labels for sklearn binary classifierssklearn 二元分类器的标签值
【发布时间】:2017-02-11 05:55:33
【问题描述】:

我的数据集带有标记为“0”或“1”的样本。

标签应该是“-1”和“1”才能正确分类吗?

我不确定 sklearn 分类器最小化了哪些损失函数。也许它依赖于“1”或“-1”的值?

【问题讨论】:

    标签: python machine-learning scikit-learn classification


    【解决方案1】:

    sklearn 分类器通常可以使用不同的损失函数或惩罚。尽管我无法在任何地方找到此文档,但根据我的经验,您传入的类通常很聪明。实际的求解器使用外部库,因此在后台可能会发生一些清理。但总的来说,我发现这些工作开箱即用:

    >>> from sklearn.linear_model import LogisticRegression
    >>> import numpy as np
    >>> X = np.random.randint(0,10,(20,5))
    >>> y1 = np.random.choice([-1,1], 20)
    >>> y2 = np.random.choice([0,1], 20)
    >>> y1
    array([-1, -1,  1, -1, -1,  1, -1,  1, -1,  1,  1, -1,  1, -1, -1, -1,  1,
            1, -1,  1])
    >>> y2
    array([0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0])
    >>> model1, model2 = LogisticRegression(), LogisticRegression()
    >>> model1.fit(X,y1)
    LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
              intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,
              penalty='l2', random_state=None, solver='liblinear', tol=0.0001,
              verbose=0, warm_start=False)
    >>> model2.fit(X, y2)
    LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
              intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,
              penalty='l2', random_state=None, solver='liblinear', tol=0.0001,
              verbose=0, warm_start=False)
    >>> model1.predict(X)
    array([-1,  1,  1, -1,  1, -1, -1,  1, -1, -1,  1,  1,  1, -1, -1, -1, -1,
            1, -1,  1])
    >>> model2.predict(X)
    array([1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0])
    >>> y1
    array([-1, -1,  1, -1, -1,  1, -1,  1, -1,  1,  1, -1,  1, -1, -1, -1,  1,
            1, -1,  1])
    >>> y2
    array([0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0])
    

    甚至:

    >>> y3 = np.random.choice(['a','b'], 20)
    >>> model3 = LogisticRegression()
    >>> model3.fit(X,y3)
    LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
              intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,
              penalty='l2', random_state=None, solver='liblinear', tol=0.0001,
              verbose=0, warm_start=False)
    >>> model3.classes_
    array(['a', 'b'],
          dtype='<U1')
    >>> model3.predict(X)
    array(['b', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'b', 'b', 'a', 'a', 'a',
           'a', 'b', 'b', 'a', 'a', 'a', 'a'],
          dtype='<U1')
    

    或者,使用支持向量机:

    >>> from sklearn.svm import LinearSVC
    >>> svm1 = LinearSVC()
    >>> svm1.fit(X,y1)
    LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True,
         intercept_scaling=1, loss='squared_hinge', max_iter=1000,
         multi_class='ovr', penalty='l2', random_state=None, tol=0.0001,
         verbose=0)
    >>> svm1.predict(X)
    array([-1,  1, -1, -1,  1, -1, -1,  1, -1, -1,  1,  1,  1, -1, -1, -1,  1,
            1, -1,  1])
    >>> svm2 = LinearSVC()
    >>> svm2.fit(X,y3)
    LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True,
         intercept_scaling=1, loss='squared_hinge', max_iter=1000,
         multi_class='ovr', penalty='l2', random_state=None, tol=0.0001,
         verbose=0)
    >>> svm2.predict(X)
    array(['b', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
           'a', 'a', 'a', 'a', 'a', 'a', 'a'],
          dtype='<U1')
    

    只要经常阅读分类器的文档,我认为如果它只接受特定类型的标签方案,它就会被记录在案。

    【讨论】:

      猜你喜欢
      • 2018-08-01
      • 2020-03-02
      • 2017-10-11
      • 1970-01-01
      • 1970-01-01
      • 2016-06-27
      • 1970-01-01
      • 2021-12-24
      • 2018-06-23
      相关资源
      最近更新 更多