【问题标题】:Scaled data with SVM causing strange Confusion Matrix使用 SVM 缩放数据导致奇怪的混淆矩阵
【发布时间】:2020-03-05 02:00:29
【问题描述】:

我有一个包含 X.shape (104481, 34) 和 y.shape (104481,) 的数据集,我想在其上训练一个 SVM 模型。

我所做的步骤是 (1) 拆分数据,(2) 缩放数据,以及 (3) 训练 SVM:

(1) 拆分数据: 功能:

from sklearn.model_selection import train_test_split

def split_data(X,y):
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=12, stratify=y)
    return X_train, X_test, y_train, y_test
X_train, X_test, y_train, y_test = split_data_set.split_data(X,y)

4 类如下。数据集非常不平衡,但这是以后的问题。

y_train.value_counts()
out:
Status_9_Substatus_8      33500
Other                     33500
Status_62_Substatus_7      2746
Status_62_Substatus_30      256
Name: Status, dtype: int64

y_test.value_counts()
out:
Status_9_Substatus_8      16500
Other                     16500
Status_62_Substatus_7      1352
Status_62_Substatus_30      127
Name: Status, dtype: int64

(2) 缩放数据:

from sklearn.preprocessing import MinMaxScaler

from sklearn import preprocessing

scaler  = MinMaxScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
print(X_train_scaled.shape)
print(y_train.shape)

(3) 使用 SVM 进行训练和预测:

svm_method.get_svm_model(X_train_scaled, X_test_scaled, y_train, y_test)

调用这个方法:

def get_svm_model(X_train, X_test, y_train, y_test):
    print('Loading...')
    print('Training...')
    svm, y_train_pred, y_test_pred = train_svm_model(X_train,y_train, X_test)
    print('Training Complete')
    print('Plotting Confusion Matrix...')
    performance_measure.plot_confusion_matrix(y_test,y_test_pred, normalize=True)
    print('Plotting Performance Measure...')
    performance_measure.get_performance_measures(y_test, y_test_pred)
    return svm

调用此方法:

def train_svm_model(X_train,y_train, X_test):
    # 
    svm = SVC(kernel='poly', gamma='auto', random_state=12)

    # Fitting the model
    svm.fit(X_train, y_train)

    # Predicting values
    y_train_pred = svm.predict(X_train)
    y_test_pred = svm.predict(X_test)

    return svm, y_train_pred, y_test_pred


生成的“输出”就是这个屏幕截图。

奇怪的是,所有四个类的样本都存在(因为我在调用 train_test_split 时使用了分层参数),但是,看起来有些类消失了?

SVM 和混淆矩阵函数在玩具数据集上运行良好:

from sklearn.datasets import load_wine
data = load_wine()


X = pd.DataFrame(data.data, columns = data.feature_names)
y = pd.DataFrame(data.target)
y = np.array(y)
y = np.ravel(y)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)

svm, y_train_pred, y_test_pred = train_svm_model(X_train, y_train, X_test)
get_svm_model(X_train, X_test, y_train, y_test)

知道这里发生了什么吗?

提前致谢。

CM 代码:

def plot_confusion_matrix(y_true, y_pred,
                          normalize=False,
                          title=None,
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    if not title:
        if normalize:
            title = 'Normalized confusion matrix'
        else:
            title = 'Confusion matrix, without normalization'

    # Compute confusion matrix
    cm = confusion_matrix(y_true, y_pred)
    # Only use the labels that appear in the data
    #classes = classes[unique_labels(y_true, y_pred)]
    classes = unique_labels(y_pred)
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(cm)

    fig, ax = plt.subplots()
    im = ax.imshow(cm, interpolation='nearest', cmap=cmap)
    ax.figure.colorbar(im, ax=ax)
    # We want to show all ticks...
    ax.set(xticks=np.arange(cm.shape[1]),
           yticks=np.arange(cm.shape[0]),
           # ... and label them with the respective list entries
           xticklabels=classes, yticklabels=classes,
           title=title,
           ylabel='True label',
           xlabel='Predicted label')

    # Rotate the tick labels and set their alignment.
    plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
             rotation_mode="anchor")

    # Loop over data dimensions and create text annotations.
    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i in range(cm.shape[0]):
        for j in range(cm.shape[1]):
            ax.text(j, i, format(cm[i, j], fmt),
                    ha="center", va="center",
                    color="white" if cm[i, j] > thresh else "black")
    fig.tight_layout()
    plt.show()
    return ax

【问题讨论】:

    标签: python pandas scikit-learn svm confusion-matrix


    【解决方案1】:

    你的混淆矩阵不为零:

    如果我们在 x 轴上看这个,你有预测的标签,在 y 轴上是真实的标签,让我们看看从顶部算起的第三行:

    • 0.94:真实标签的 0.94:Status_62_Substatus_7 被预测为其他类,这是错误的
    • 0.00 相同的真实标签预测也错误
    • 0.00 相同的真实标签被预测错误(这应该是正确的预测值(越高越好)
    • 0.06 再次预测错误

    你的问题是不是太不平衡了,你对两个标签只有 0 个预测。

    【讨论】:

      猜你喜欢
      • 2018-11-20
      • 2021-07-02
      • 2014-07-09
      • 2020-03-30
      • 2018-02-20
      • 2015-12-17
      • 2020-10-01
      • 2012-01-20
      • 2019-11-23
      相关资源
      最近更新 更多