以mnist数据为例,对数据进行初步分析和可视化,并对二类和多类分类器的训练和模型效果评估。根据要求编写源程序清单及实例的运行结果(源程序应该附有必要的注释)
时间: 2024-10-19 16:03:35 浏览: 23
以MNIST手写数字数据集为例,首先我们需要对数据进行初步分析和可视化:
**数据预处理与分析**
```python
import numpy as np
import matplotlib.pyplot as plt
from keras.datasets import mnist
# 加载数据
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# 数据预处理(归一化)
x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255
y_train = np.eye(10)[y_train] # 将标签转为one-hot编码
y_test = np.eye(10)[y_test]
# 初步数据分析
print("Train data shape:", x_train.shape)
print("Test data shape:", x_test.shape)
```
**数据可视化**
```python
plt.figure(figsize=(10, 4))
for i in range(10):
plt.subplot(2, 5, i+1)
plt.imshow(x_train[i], cmap='gray')
plt.title(f"Class {np.argmax(y_train[i])}")
plt.tight_layout()
```
接下来我们构建一个简单的二分类和多分类模型(这里使用Keras举例):
**二分类分类器(例如将0和1分开)**
```python
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
model_binary = Sequential([
Flatten(input_shape=(28, 28)), # 展平输入
Dense(128, activation='relu'),
Dense(1, activation='sigmoid')
])
model_binary.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# 训练模型
history_binary = model_binary.fit(x_train, y_train[:, [0, 1]], epochs=10, validation_split=0.2)
# 模型评估
score_binary = model_binary.evaluate(x_test, y_test[:, [0, 1]])
print(f"Binary classification test score: {score_binary[1]:.4f}")
```
**多分类分类器(所有10个类别)**
```python
model_multi = Sequential([
Flatten(input_shape=(28, 28)),
Dense(128, activation='relu'),
Dense(10, activation='softmax')
])
model_multi.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# 训练模型
history_multi = model_multi.fit(x_train, y_train, epochs=10, validation_split=0.2)
# 模型评估
score_multi = model_multi.evaluate(x_test, y_test)
print(f"Multi-class classification test score: {score_multi[1]:.4f}")
```
阅读全文