SVM对MNIST数据集分类——python实现

python实现SVM分类且进行调参

问题描述

从 MNIST 数据集中任意选择两类,对其进行 SVM 分类,可调用现有的 SVM 工具如 LIBSVM,展示超参数 C 以及核函数参数的选择过程。

求解过程

第一步:数据集下载,代码如下:

mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()

第二步:选定核函数为 RBF
第三步:手动进行调参,调参过程如下,显示的准确率是测试集的,可以看出当 gamma 的值为 0.0001、0.001;C 的值为 0.1 以上时泛化性能较好。
在这里插入图片描述
手动调参的代码


from sklearn import svm
import numpy as np
import tensorflow as tf
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
import time
mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
x_train = x_train.reshape([x_train.shape[0], -1])
x_test = x_test.reshape([x_test.shape[0], -1])
train_x = []
train_y = []
for i in range(x_train.shape[0]):
    if y_train[i] == 0 or y_train[i] == 1:
        train_x.append(x_train[i])
        train_y.append(y_train[i])
train_x = np.array(train_x)
train_y = np.array(train_y)
test_x = []
test_y = []
for i in range(x_test.shape[0]):
    if y_test[i] == 0 or y_test[i] == 1:
        test_x.append(x_test[i])
        test_y.append(y_test[i])
test_x = np.array(test_x)
test_y = np.array(test_y)
train_x = train_x[0:1000]
train_y = train_y[0:1000]
test_x = test_x[0:300]
test_y = test_y[0:300]
scaler = StandardScaler()
train_x = scaler.fit_transform(train_x)
test_x = scaler.fit_transform(test_x)


score = np.zeros([10,10])
C_range = np.logspace(-3, 6, 10)
gamma_range = np.logspace(-4, 5, 10)
for i,c in enumerate(C_range):
    for j,g in enumerate(gamma_range):
        start = time.time()
        svc = svm.SVC(C=c, kernel='rbf', gamma=g, decision_function_shape='ovo')
        svc.fit(train_x, train_y)
        score[i,j] = svc.score(test_x, test_y)
        end = time.time()
        print('c:',c,'g:',g,'time:',(end-start))


plt.figure(figsize=(8, 6))
plt.subplots_adjust(left=.2, right=0.95, bottom=0.15, top=0.95)
plt.imshow(score, interpolation='nearest', cmap=plt.cm.hot)
plt.xlabel('gamma')
plt.ylabel('C')
plt.colorbar()
plt.xticks(np.arange(len(gamma_range)), gamma_range, rotation=45)
plt.yticks(np.arange(len(C_range)), C_range)
plt.title('Test accuracy')
plt.show()

第四步:网格调参,参数如下
输出为:The best parameters are {‘C’: 3, ‘gamma’: 0.0001, ‘kernel’: ‘rbf’} with a score of 1.00
在这里插入图片描述
代码


from sklearn import svm
import numpy as np
import tensorflow as tf
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
from sklearn.model_selection import GridSearchCV
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
x_train = x_train.reshape([x_train.shape[0], -1])
x_test = x_test.reshape([x_test.shape[0], -1])
train_x = []
train_y = []
for i in range(x_train.shape[0]):
    if y_train[i] == 0 or y_train[i] == 1:
        train_x.append(x_train[i])
        train_y.append(y_train[i])
train_x = np.array(train_x)
train_y = np.array(train_y)
test_x = []
test_y = []
for i in range(x_test.shape[0]):
    if y_test[i] == 0 or y_test[i] == 1:
        test_x.append(x_test[i])
        test_y.append(y_test[i])
test_x = np.array(test_x)
test_y = np.array(test_y)
train_x = train_x[0:1000]
train_y = train_y[0:1000]
test_x = test_x[0:300]
test_y = test_y[0:300]
scaler = StandardScaler()
train_x = scaler.fit_transform(train_x)
test_x = scaler.fit_transform(test_x)

parameters = [
	{
		'C': [1, 3, 5, 7, 9, 11, 13, 15, 17, 19],
	    'gamma': [0.00001, 0.0001, 0.001, 0.1, 1, 10, 100, 1000],
	    'kernel': ['rbf']
	},
	{
		'C': [1, 3, 5, 7, 9, 11, 13, 15, 17, 19],
	    'kernel': ['linear']
	}
]

svc = svm.SVC()
clf = GridSearchCV(svc, parameters, cv=3, n_jobs=8)
clf.fit(train_x,train_y)

print("The best parameters are %s with a score of %0.2f"
      % (clf.best_params_, clf.best_score_))

scores = clf.cv_results_['mean_test_score'][0:80].reshape(8,10)

plt.figure(figsize=(8, 6))
plt.subplots_adjust(left=.2, right=0.95, bottom=0.15, top=0.95)
plt.imshow(scores, interpolation='nearest', cmap=plt.cm.hot)
plt.xlabel('gamma')
plt.ylabel('C')
plt.colorbar()
plt.xticks(np.arange(8), [0.00001, 0.0001, 0.001, 0.1, 1, 10, 100, 1000], rotation=45)
plt.yticks(np.arange(10), [1, 3, 5, 7, 9, 11, 13, 15, 17, 19])
plt.title('RBF Validation accuracy')
plt.show()

说明

tensorflow==2.4

  • 3
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
SVM (Support Vector Machine) 是一种强大的监督学习算法,尤其在小样本、非线性和高维数据集上表现优异,比如用于图像识别任务,如MNIST手写数字数据集MNIST是一个非常经典的计算机视觉数据集,包含60,000个训练样本和10,000个测试样本,每个样本都是28x28像素的灰度图像,对应0-9十个数字类别。 要使用SVMMNIST数据集进行分类,你需要遵循以下步骤: 1. **数据预处理**: - 对输入图像进行归一化或标准化,确保所有像素值落在一个合适的范围内(通常0-1之间)。 - 将图像转换为一维向量,因为SVM是基于线性或核函数的,不是基于像素位置的。 2. **加载和分割数据**: - 使用如TensorFlow、Keras或scikit-learn等库中的函数加载MNIST数据集。 - 划分训练集和验证集,一般会用一部分训练数据进行模型调参。 3. **构建SVM模型**: - 如果数据线性可分,可以选择标准的SVM。否则,可以使用SVC或LinearSVC的kernel参数选择核函数(如'linear', 'poly', 'rbf'等)。 - SVM模型中包括决策边界和支持向量。 4. **训练模型**: - 使用训练集数据拟合模型,优化模型参数,如正则化参数C。 5. **评估和调整**: - 使用验证集评估模型性能,可能需要调整参数以达到最好的性能。 - 记录模型的精度、召回率等指标。 6. **测试**: - 最后,用测试集数据评估模型在未见过的数据上的泛化能力。 7. **输出和可视化**: - 可视化分类结果,查看模型预测的准确性和错误案例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值