gpt4 book ai didi

python - keras:准确度为 98%,但神经网络始终预测相同。可能是什么原因?

转载 作者:太空宇宙 更新时间:2023-11-03 19:42:31 24 4
gpt4 key购买 nike

我们在训练深度学习模型来预测贷款评分(分为 0、1 或 3)时遇到以下问题。

步骤如下:

第1步:创建新列“评分”(输出)

conditions = [
(df2['Credit Score'] >= 0) & (df2['Credit Score'] < 1000),
(df2['Credit Score'] >= 1000) & (df2['Credit Score'] < 6000),
(df2['Credit Score'] >= 6000) & (df2['Credit Score'] <= 7000)]
choices = [0,1,2]
df2['Scoring'] = np.select(conditions, choices)

第2步:准备训练

array = df2.values
X = np.vstack((array[:,2:3].T, array[:,5:15].T)).T
Y = array[:,15:]
N = Y.shape[0]
T = np.zeros((N, np.max(Y)+1))
for i in range(N):
T[i,Y[i]] = 1

x_train, x_test, y_train, y_test = train_test_split(X, T, test_size=0.2, random_state=42)

第 3 步:拓扑

model = Sequential()

model.add(Dense(80, input_shape=(11,), activation='tanh'))
model.add(Dropout(0.2))
model.add(Dense(80, activation='tanh'))
model.add(Dropout(0.1))
model.add(Dense(40, activation='relu'))
model.add(Dense(3, activation='softmax'))

epochs =200
learning_rate = 0.00001
decay_rate = learning_rate / epochs
momentum = 0.002
sgd = SGD(lr=learning_rate, momentum=momentum, decay=decay_rate, nesterov=False)
ad = Adamax(lr=learning_rate)

第 4 步:训练

 epochs = 200 
batch_size = 16

history = model.fit(x_train, y_train, validation_data=(x_test, y_test), nb_epoch=epochs,
batch_size=batch_size,validation_split=0.1)
print ('fit done!')

指标

365/365 [================================] - 0s 60us/样本 - 损耗:0.0963 - acc: 0.9808测试集 损失:0.096 准确度:0.981

accuracy

第5步:预测

text1 = [1358,1555,1,3,1741,8,0,1596,1518,0,0] #scoring 0 
text2 = [1454,1601,3,11,1763,10,0,685,1044,0,0] #scoring 1
text3 = [1209,1437,3,11,199,18,1,761,1333,1,0] #scoring 2

tmp = np.vstack(text1).T
textA = tmp.reshape(1,-1)

tmp = np.vstack(text2).T
textB = tmp.reshape(1,-1)

tmp = np.vstack(text3).T
print(tmp)
textC = tmp.reshape(1,-1)

p = model.predict(textA)
t = p[0]
print(textA,np.argmax(t))


p = model.predict(textB)
t = p[0]
print(textB,np.argmax(t))

p = model.predict(textC)
t = p[0]
print(textC,np.argmax(t))

问题:预测中的输出始终相同!!!

[9.9205679e-01 3.8634153e-04 7.5568780e-03] [[1358 1555 1 3 1741 8 0 1596 1518 0 0]] 0 --- scoring 0

[0.9862417 0.00205712 0.01170125] [[1454 1601 3 11 1763 10 0 685 1044 0 0]] 0 --- scoring 0

[9.9251783e-01 2.5733517e-04 7.2247880e-03] [[1209 1437 3 11 199 18 1 761 1333 1 0]] 0 ---- scoring 0

这种行为的原因是什么?

提前致谢!

最佳答案

您的数据集极其不平衡。一个很好的看待它的方法是:如果总是预测 0 可以让你达到 98% 的准确率,那么说某个东西属于不同的类别是相当危险的(或者必须非常明显)。神经网络可能发现的使任何少数类别不同于多数类别 (0) 的每种模式都必须非常独特,因为即使重叠很小,不预测 0 的成本也太高。

考虑以下示例:您有一个包含两个类 A 和 B 的数据集,这两个类都遵循正态分布。 A 类的平均值为 1,标准差为 1,B 类的平均值为 3,标准差为 0.1。您有 1,000,000 个 0 类样本和 20,000 个 1 类样本,因此始终预测 A 的准确度为 98%。 B 类的所有样本都将位于 2.743 和 3.257 之间,置信度为 99%。在这些值之间,A 类预计有 29,300 个样本,因此预测 B 类任何观测值的成本是在 29,300 个 A 样本中出错,但预测 A 类所有内容的成本是仅在 20,000 个 B 样本中出错.

该示例的图形外观如下:

import numpy as np
import matplotlib.pyplot as plt

# Get A and B
A = np.random.normal(1, 1, 1000000)
B = np.random.normal(3, 0.1, 20000)

# Count the number of observations in A for each B
B.sort()
a = A[np.logical_and(A >= B.min(), A <= B.max())]
a = [(a<i).sum() for i in B]

# Plot results
plt.plot(B, np.arange(B.shape[0]), label='Class B')
plt.plot(B, a, label='Class A')
plt.ylabel('Count of samples')
plt.xlabel('Values')
plt.legend()
plt.show()

Unbalance example

请参阅这篇有关平衡数据集的文章:https://www.kdnuggets.com/2017/06/7-techniques-handle-imbalanced-data.html

关于python - keras:准确度为 98%,但神经网络始终预测相同。可能是什么原因?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60351164/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com