参考代码:
import keras import numpy as np from keras.losses import CategoricalCrossentropy from keras.optimizers import SGD from keras.utils import to_categorical from numpy import random from sklearn.datasets import load_iris from sklearn.metrics import confusion_matrix, recall_score # 加载数据 (data, target) = load_iris(return_X_y=True) # 数据打乱 indices = list(range(len(target))) random.shuffle(indices) data = data[indices] target = target[indices] tag_count = len(set(target)) # 构造模型 model = keras.models.Sequential( layers=[keras.layers.Dense(512, activation='relu', input_shape=(4,)), keras.layers.Dropout(0.2), keras.layers.Dense(tag_count, activation='softmax')] ) print(model.summary()) # Model: "sequential_1" # _________________________________________________________________ # Layer (type) Output Shape Param # # ================================================================= # dense_1 (Dense) (None, 512) 2560 # _________________________________________________________________ # dropout_1 (Dropout) (None, 512) 0 # _________________________________________________________________ # dense_2 (Dense) (None, 3) 1539 # ================================================================= # Total params: 4,099 # Trainable params: 4,099 # Non-trainable params: 0 model.compile(optimizer=SGD(), loss=CategoricalCrossentropy(), metrics=['accuracy']) target = to_categorical(target, tag_count) # 数据切分为两部分 80% 用于训练,20%用于预测 train_size = int(len(target) * 0.8) train_x, train_y = data[:train_size], target[:train_size] test_x, test_y = data[train_size:], target[train_size:] history = model.fit(train_x, train_y, batch_size=10, epochs=100, validation_split=0.25) pred_y = model.predict(test_x) print(pred_y[:3]) # [[5.1133092e-03 6.2675482e-01 3.6813188e-01] # [5.4596356e-05 4.3929096e-02 9.5601630e-01] # [9.6804839e-01 3.1915616e-02 3.6038000e-05]] # 根据softmax结果获取类别,即:最大softmax值对应的下标 test_y_pred = [np.argmax(y, axis=None, out=None) for y in pred_y] test_y_true = [np.argmax(y, axis=None, out=None) for y in test_y] print(test_y_true) print(test_y_pred) # [1, 2, 0, 2, 0, 1, 2, 0, 1, 0, 2, 2, 1, 2, 1, 0, 0, 1, 2, 2, 2, 2, 1, 2, 0, 1, 1, 1, 2, 2] # [1, 2, 0, 2, 0, 1, 2, 0, 1, 0, 2, 2, 1, 2, 1, 0, 0, 1, 2, 2, 2, 2, 1, 2, 0, 1, 2, 1, 2, 2] print(confusion_matrix(test_y_true, test_y_pred)) # [[ 7 0 0] # [ 0 9 1] # [ 0 0 13]] print(recall_score(test_y_pred, test_y_true, average='micro')) # 0.9666666666666667