为什么sklearn.metrics支持值每次都会更改?

问题描述 投票:0回答:1

我正在训练一种监督学习的keras模型,以将数据分类为以下三类之一。训练后,我运行这个:

dataset = pandas.read_csv(filename, header=[0], encoding='utf-8-sig', sep=',')

# split X and Y (last column)
array = dataset.values
columns = array.shape[1] - 1
np.random.shuffle(array)
x_orig = array[:, 1:columns]
testy = array[:, columns]
columns -= 1

# normalize data
scaler = StandardScaler()
testx= scaler.fit_transform(x_orig)

#onehot
testy = to_categorical(testy)

# load weights
save_path = "[filepath]"
model = tf.keras.models.load_model(save_path)

# gets class breakdown
y_pred = model.predict(testx, verbose=1)
y_pred_bool = np.argmax(y_pred, axis=1)
y_true = np.argmax(testy, axis=1)
print(sklearn.metrics.precision_recall_fscore_support(y_true, y_pred))

sklearn.metrics.precision_recall_fscore_support除其他度量外,还显示对每个类的支持。通过该链接,支持的是y_true中每个类的出现次数,这是true标签。https://scikit-learn.org/stable/modules/generated/sklearn.metrics.precision_recall_fscore_support.html

我的问题:每次运行,支持都不同。我使用的是相同的数据,并且对每个课程的支持总会得出相同的结果(但与文件总数有所不同-我也不明白),但是每个课程的人数却有所不同。

例如,一个运行可能会显示[16870、16299、7807],而下一运行可能会显示[17169、15923、7884]。它们加起来相同,但每个类都不相同。

由于我的数据在两次运行之间没有变化,因此我希望每次都支持相同的内容。我错了吗?如果没有,那是怎么回事?我曾尝试使用Google搜索,但未获得任何有用的结果。

可能有用的信息:当我运行sklearn.metrics.classification_report时,我遇到了同样的问题,并且其中的数字与precision_recall_fscore_support中的数字匹配。

旁注:与上述问题无关,但我也无法用谷歌找到这个问题的答案,我希望可以在这里包括在内。当我运行model.evaluate时,部分打印输出是例如74us /样本。我们/样本是什么意思?

python machine-learning scikit-learn supervised-learning
1个回答
0
投票

添加:

np.random.seed(42)

在将数组拖到]之前>

np.random.shuffle(array)

The reason for this is without seeding np.shuffle每次都会创建不同的结果。因此,当您将数组输入模型时,它将返回不同的结果。播种使您每次都可以对其进行随机播放,从而产生可重复的结果。

否则,您将无法重新整理并每次获得相同的数组以馈入模型。两种方法中的一种或两种都将确保模型内的可重复性。

© www.soinside.com 2019 - 2024. All rights reserved.