数据集和标签都准备好了,怎么用卷积神经网络进行分类识别,加入混淆矩阵python代码
时间: 2023-12-03 10:45:31 浏览: 70
基于python实现利用卷积神经网络进行定位和识别
首先,需要导入相应的库:
```python
import numpy as np
import tensorflow as tf
from tensorflow import keras
from sklearn.metrics import confusion_matrix
```
然后,需要加载数据集和标签,并进行预处理:
```python
# 加载数据集和标签
train_data = np.load('train_data.npy')
train_labels = np.load('train_labels.npy')
test_data = np.load('test_data.npy')
test_labels = np.load('test_labels.npy')
# 数据预处理
train_data = train_data.astype('float32') / 255
test_data = test_data.astype('float32') / 255
train_labels = keras.utils.to_categorical(train_labels)
test_labels = keras.utils.to_categorical(test_labels)
```
接下来,构建卷积神经网络模型:
```python
model = keras.Sequential([
keras.layers.Conv2D(32, (3, 3), padding='same', activation='relu', input_shape=train_data.shape[1:]),
keras.layers.MaxPooling2D((2, 2)),
keras.layers.Conv2D(64, (3, 3), padding='same', activation='relu'),
keras.layers.MaxPooling2D((2, 2)),
keras.layers.Flatten(),
keras.layers.Dense(64, activation='relu'),
keras.layers.Dense(10, activation='softmax')
])
```
然后,编译模型并训练:
```python
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(train_data, train_labels, epochs=10, batch_size=32, validation_data=(test_data, test_labels))
```
最后,使用测试集对模型进行评估,并输出混淆矩阵:
```python
# 对测试集进行评估
test_loss, test_acc = model.evaluate(test_data, test_labels)
print('Test accuracy:', test_acc)
# 输出混淆矩阵
test_pred = model.predict(test_data)
test_pred_labels = np.argmax(test_pred, axis=1)
test_true_labels = np.argmax(test_labels, axis=1)
confusion_mtx = confusion_matrix(test_true_labels, test_pred_labels)
print(confusion_mtx)
```
完整代码如下:
```python
import numpy as np
import tensorflow as tf
from tensorflow import keras
from sklearn.metrics import confusion_matrix
# 加载数据集和标签
train_data = np.load('train_data.npy')
train_labels = np.load('train_labels.npy')
test_data = np.load('test_data.npy')
test_labels = np.load('test_labels.npy')
# 数据预处理
train_data = train_data.astype('float32') / 255
test_data = test_data.astype('float32') / 255
train_labels = keras.utils.to_categorical(train_labels)
test_labels = keras.utils.to_categorical(test_labels)
# 构建卷积神经网络模型
model = keras.Sequential([
keras.layers.Conv2D(32, (3, 3), padding='same', activation='relu', input_shape=train_data.shape[1:]),
keras.layers.MaxPooling2D((2, 2)),
keras.layers.Conv2D(64, (3, 3), padding='same', activation='relu'),
keras.layers.MaxPooling2D((2, 2)),
keras.layers.Flatten(),
keras.layers.Dense(64, activation='relu'),
keras.layers.Dense(10, activation='softmax')
])
# 编译模型并训练
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(train_data, train_labels, epochs=10, batch_size=32, validation_data=(test_data, test_labels))
# 对测试集进行评估
test_loss, test_acc = model.evaluate(test_data, test_labels)
print('Test accuracy:', test_acc)
# 输出混淆矩阵
test_pred = model.predict(test_data)
test_pred_labels = np.argmax(test_pred, axis=1)
test_true_labels = np.argmax(test_labels, axis=1)
confusion_mtx = confusion_matrix(test_true_labels, test_pred_labels)
print(confusion_mtx)
```
阅读全文