python - 使用 SVM 缩放数据会导致奇怪的混淆矩阵

标签 python pandas scikit-learn svm confusion-matrix

我有一个包含 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


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

enter image description here

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

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

最佳答案

你的混淆矩阵不为零:

enter image description here

如果我们在 x 轴上查看,您将获得预测标签,在 y 轴上查看真实标签,让我们看看从顶部开始的第三行:

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

您的问题是否如此不平衡,以至于您对两个标签只有 0 个预测。

关于python - 使用 SVM 缩放数据会导致奇怪的混淆矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58766514/

相关文章:

python - 使用两个 for 循环矢量化图像生成

python - 在没有 sudo 权限的 Linux 系统上安装 Python TA-lib 包时出错

python - 如何从具有相同标签的列的数据框中删除列?

python - 有没有办法在 sklearn 管道中链接 pd.cut FunctionTransformer?

python - Python中sklearn的线性回归中,莫名其妙的维度不匹配错误

python - 查找 Pandas 移动平均线当前和之前交叉点之间的最小值

python - 如何在 pytest test_functions 之间重置 Python 运行时?

python - 选择 Pandas 列中的日期子集

python - 将多索引 Pandas 数据帧连接到另一个多索引数据帧

python-3.x - 获取叶子节点决策路径中的所有特征(随机森林)