神经网络只从二进制 class 中预测一个 class
Neural network only predicts one class from binary class
我的任务是了解工厂中的次品。这意味着,我尝试检测有缺陷的商品或优质商品。这导致了一个问题,其中一个 class 支配了其他(一个 class 占数据的 99.7%),因为有缺陷的项目非常罕见。训练精度为 0.9971,验证精度为 0.9970。听起来很棒。
但问题是,该模型只预测一切都是 0 class,这是好货。这意味着,它无法 class 确认任何有缺陷的商品。
我怎么解决这个问题?我查了其他问题也试过了,还是有这种情况。总数据点为 122400 行和 5 个特征。
最终我的测试集混淆矩阵是这样的
array([[30508, 0],
[ 92, 0]], dtype=int64)
做得很糟糕。
我的代码如下:
le = LabelEncoder()
y = le.fit_transform(y)
ohe = OneHotEncoder(sparse=False)
y = y.reshape(-1,1)
y = ohe.fit_transform(y)
scaler = StandardScaler()
x = scaler.fit_transform(x)
x_train, x_test, y_train, y_test = train_test_split(x,y,test_size = 0.25, random_state = 777)
#DNN Modelling
epochs = 15
batch_size =128
Learning_rate_optimizer = 0.001
model = Sequential()
model.add(Dense(5,
kernel_initializer='glorot_uniform',
activation='relu',
input_shape=(5,)))
model.add(Dense(5,
kernel_initializer='glorot_uniform',
activation='relu'))
model.add(Dense(8,
kernel_initializer='glorot_uniform',
activation='relu'))
model.add(Dense(2,
kernel_initializer='glorot_uniform',
activation='softmax'))
model.compile(loss='binary_crossentropy',
optimizer=Adam(lr = Learning_rate_optimizer),
metrics=['accuracy'])
history = model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
y_pred = model.predict(x_test)
confusion_matrix(y_test.argmax(axis=1), y_pred.argmax(axis=1))
谢谢
听起来你的数据集高度不平衡,模型只学习如何对优质商品进行分类。
您可以尝试此处列出的方法之一:
https://machinelearningmastery.com/tactics-to-combat-imbalanced-classes-in-your-machine-learning-dataset/
最好的尝试是首先获取两者几乎相等的数据部分 类,将它们分成 train-test-val,训练分类器并对完整数据集进行彻底测试。您还可以尝试将数据增强技术用于您的其他集合,以从同一集合中获取更多数据。继续迭代,甚至可能尝试更改损失函数以适应您的情况。
我的任务是了解工厂中的次品。这意味着,我尝试检测有缺陷的商品或优质商品。这导致了一个问题,其中一个 class 支配了其他(一个 class 占数据的 99.7%),因为有缺陷的项目非常罕见。训练精度为 0.9971,验证精度为 0.9970。听起来很棒。 但问题是,该模型只预测一切都是 0 class,这是好货。这意味着,它无法 class 确认任何有缺陷的商品。 我怎么解决这个问题?我查了其他问题也试过了,还是有这种情况。总数据点为 122400 行和 5 个特征。
最终我的测试集混淆矩阵是这样的
array([[30508, 0],
[ 92, 0]], dtype=int64)
做得很糟糕。
我的代码如下:
le = LabelEncoder()
y = le.fit_transform(y)
ohe = OneHotEncoder(sparse=False)
y = y.reshape(-1,1)
y = ohe.fit_transform(y)
scaler = StandardScaler()
x = scaler.fit_transform(x)
x_train, x_test, y_train, y_test = train_test_split(x,y,test_size = 0.25, random_state = 777)
#DNN Modelling
epochs = 15
batch_size =128
Learning_rate_optimizer = 0.001
model = Sequential()
model.add(Dense(5,
kernel_initializer='glorot_uniform',
activation='relu',
input_shape=(5,)))
model.add(Dense(5,
kernel_initializer='glorot_uniform',
activation='relu'))
model.add(Dense(8,
kernel_initializer='glorot_uniform',
activation='relu'))
model.add(Dense(2,
kernel_initializer='glorot_uniform',
activation='softmax'))
model.compile(loss='binary_crossentropy',
optimizer=Adam(lr = Learning_rate_optimizer),
metrics=['accuracy'])
history = model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
y_pred = model.predict(x_test)
confusion_matrix(y_test.argmax(axis=1), y_pred.argmax(axis=1))
谢谢
听起来你的数据集高度不平衡,模型只学习如何对优质商品进行分类。 您可以尝试此处列出的方法之一: https://machinelearningmastery.com/tactics-to-combat-imbalanced-classes-in-your-machine-learning-dataset/
最好的尝试是首先获取两者几乎相等的数据部分 类,将它们分成 train-test-val,训练分类器并对完整数据集进行彻底测试。您还可以尝试将数据增强技术用于您的其他集合,以从同一集合中获取更多数据。继续迭代,甚至可能尝试更改损失函数以适应您的情况。