【人工智能】机器学习及与智能数据处理之降维算法PCA及其应用手写字体识别以及【自定义数据集】

简介: 利用PCA算法实现手写字体识别,要求: 1、实现手写数字数据集的降维; 2、比较两个模型(64维和10维)的准确率; 3、对两个模型分别进行10次10折交叉验证,绘制评分对比曲线。

降维算法PCA及其应用

利用PCA算法实现手写字体识别,要求:

1、实现手写数字数据集的降维;

2、比较两个模型(64维和10维)的准确率;

3、对两个模型分别进行10次10折交叉验证,绘制评分对比曲线。

实验步骤

1. 导入数据集

from sklearn.datasets import load_digits
digits = load_digits()
train = digits.data
target = digits.target

2. 实现手写数字数据集的降维;

pca = PCA(n_components=10,whiten=True)
pca.fit(x_train,y_train)
x_train_pca = pca.transform(x_train)
x_test_pca = pca.transform(x_test)

3. 比较两个模型(64维和10维)的准确率;

64维

svc = SVC(kernel = 'rbf')
svc.fit(x_train,y_train)
y_predict = svc.predict(x_test)
print('The Accuracy of SVC is', svc.score(x_test, y_test))
print("classification report of SVC\n",classification_report(y_test, y_predict,
target_names=digits.target_names.astype(str)))

10维

svc = SVC(kernel = 'rbf')
svc.fit(x_train_pca,y_train)
y_pre_svc = svc.predict(x_test_pca)
print("The Accuracy of PCA_SVC is ", svc.score(x_test_pca,y_test))
print("classification report of PCA_SVC\n", classification_report(y_test, y_pre_svc,
target_names=digits.target_names.astype(str)))

4. 对两个模型分别进行10次10折交叉验证,绘制评分对比曲线。

for i in range(100):
    # 创建子图
    plt.subplot(10,10,i+1)
    # 显示灰度图像
    plt.imshow(samples[i].reshape(8,8),cmap='gray')
    title = str(y_pre[i])
    plt.title(title,color='red')
    # 关闭坐标轴
    plt.axis('off')
plt.show()

代码详解

import matplotlib.pyplot as plt
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_digits
digits = load_digits()
train = digits.data
target = digits.target
x_train,x_test,y_train,y_test = train_test_split(train,target,test_size=0.2,random_state=33)
ss = StandardScaler()
x_train = ss.fit_transform(x_train)
x_test = ss.transform(x_test)
svc = SVC(kernel = 'rbf')
svc.fit(x_train,y_train)
y_predict = svc.predict(x_test)
print('The Accuracy of SVC is', svc.score(x_test, y_test))
print("classification report of SVC\n",classification_report(y_test, y_predict,
target_names=digits.target_names.astype(str)))
# 实现手写数字数据集的降维实现手写数字数据集的降维
pca = PCA(n_components=10,whiten=True)
pca.fit(x_train,y_train)
x_train_pca = pca.transform(x_train)
x_test_pca = pca.transform(x_test)
svc = SVC(kernel = 'rbf')
svc.fit(x_train_pca,y_train)
# 比较两个模型(64维和10维)的准确率
y_pre_svc = svc.predict(x_test_pca)
print("The Accuracy of PCA_SVC is ", svc.score(x_test_pca,y_test))
print("classification report of PCA_SVC\n", classification_report(y_test, y_pre_svc,
target_names=digits.target_names.astype(str)))
samples = x_test[:100]
y_pre = y_pre_svc[:100]
plt.figure(figsize=(12,38))
# 对两个模型分别进行10次10折交叉验证,绘制评分对比曲线
for i in range(100):
    plt.subplot(10,10,i+1)
    plt.imshow(samples[i].reshape(8,8),cmap='gray')
    title = str(y_pre[i])
    plt.title(title)
    plt.axis('off')
plt.show()

结果:

在这里插入图片描述

SVC

在这里插入图片描述

PCA

在这里插入图片描述

降维算法PCA及其应用手写识别【自定义数据集】

利用PCA算法实现手写字体识别,要求:

1、实现手写数字数据集的降维;

2、比较两个模型(64维和10维)的准确率;

3、对两个模型分别进行10次10折交叉验证,绘制评分对比曲线。

实验步骤

1. 导入自定义数据集

可以事先下载,也可以联网下载!
下载地址:

http://deeplearning.net/data/mnist/

保存如下:
在这里插入图片描述

from pathlib import Path
DATA_PATH = Path("data")
PATH = DATA_PATH / "mnist"
PATH.mkdir(parents=True, exist_ok=True)
URL = "http://deeplearning.net/data/mnist/"
FILENAME = "mnist.pkl.gz"
# 如果未下载,则创建目录下载数据
if not (PATH / FILENAME).exists():
    content = requests.get(URL + FILENAME).content
    (PATH / FILENAME).open("wb").write(content)
# 读取数据集
with gzip.open((PATH / FILENAME).as_posix(), "rb") as f:
    ((x_train, y_train), (x_test, y_test), _) = pickle.load(f, encoding="latin-1")
x_train = x_train[:5000,:]
y_train = y_train[:5000,]
x_test = x_test[:360,:]
y_test = y_test[:360,]

其他步骤和上一个相同【人工智能之手写字体识别】机器学习及与智能数据处理之降维算法PCA及其应用手写字体识别

代码详解

import matplotlib.pyplot as plt
from pathlib import Path
from sklearn.svm import SVC
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import classification_report
import requests
import pickle
import gzip
DATA_PATH = Path("data")
PATH = DATA_PATH / "mnist"
PATH.mkdir(parents=True, exist_ok=True)
URL = "http://deeplearning.net/data/mnist/"
FILENAME = "mnist.pkl.gz"
if not (PATH / FILENAME).exists():
    content = requests.get(URL + FILENAME).content
    (PATH / FILENAME).open("wb").write(content)
# 读取数据集
with gzip.open((PATH / FILENAME).as_posix(), "rb") as f:
    ((x_train, y_train), (x_test, y_test), _) = pickle.load(f, encoding="latin-1")
x_train = x_train[:5000,:]
y_train = y_train[:5000,]
x_test = x_test[:360,:]
y_test = y_test[:360,]
#################################################################
# Each image is 28 x 28, and is being stored as a flattened row of length
# 784 (=28x28). Let's take a look at one; we need to reshape it to 2d
# first.
ss = StandardScaler()
x_train = ss.fit_transform(x_train)
x_test = ss.transform(x_test)
svc = SVC(kernel = 'rbf')
svc.fit(x_train,y_train)
y_predict = svc.predict(x_test)
print('The Accuracy of SVC is', svc.score(x_test, y_test))
print("classification report of SVC\n",classification_report(y_test, y_predict))
samples = x_test[:100]
y_pre = y_predict[:100]
plt.figure(figsize=(12,38))
for i in range(100):
    # 创建子图
    plt.subplot(10,10,i+1)
    # 显示灰度图像
    plt.imshow(samples[i].reshape(28,28),cmap='gray')
    title = str(y_pre[i])
    plt.title(title,color='red')
    # 关闭坐标轴
    plt.axis('off')
plt.show()
# 实现手写数字数据集的降维实现手写数字数据集的降维
pca = PCA(n_components=10,whiten=True)
pca.fit(x_train,y_train)
x_train_pca = pca.transform(x_train)
x_test_pca = pca.transform(x_test)
svc = SVC(kernel = 'rbf')
svc.fit(x_train_pca,y_train)
# 比较两个模型(64维和10维)的准确率
y_pre_svc = svc.predict(x_test_pca)
print("The Accuracy of PCA_SVC is ", svc.score(x_test_pca,y_test))
print("classification report of PCA_SVC\n", classification_report(y_test, y_pre_svc))
samples = x_test[:100]
y_pre = y_pre_svc[:100]
plt.figure(figsize=(12,38))
# 对两个模型分别进行10次10折交叉验证,绘制评分对比曲线
for i in range(100):
    plt.subplot(10,10,i+1)
    plt.imshow(samples[i].reshape(28,28),cmap='gray')
    title = str(y_pre[i])
    plt.title(title)
    plt.axis('off')
plt.show()

结果:

在这里插入图片描述

SVC

在这里插入图片描述

PCA

在这里插入图片描述

目录
相关文章
|
机器学习/深度学习 人工智能 算法
猫狗宠物识别系统Python+TensorFlow+人工智能+深度学习+卷积网络算法
宠物识别系统使用Python和TensorFlow搭建卷积神经网络,基于37种常见猫狗数据集训练高精度模型,并保存为h5格式。通过Django框架搭建Web平台,用户上传宠物图片即可识别其名称,提供便捷的宠物识别服务。
1592 55
|
机器学习/深度学习 人工智能 算法
基于Python深度学习的眼疾识别系统实现~人工智能+卷积网络算法
眼疾识别系统,本系统使用Python作为主要开发语言,基于TensorFlow搭建卷积神经网络算法,并收集了4种常见的眼疾图像数据集(白内障、糖尿病性视网膜病变、青光眼和正常眼睛) 再使用通过搭建的算法模型对数据集进行训练得到一个识别精度较高的模型,然后保存为为本地h5格式文件。最后使用Django框架搭建了一个Web网页平台可视化操作界面,实现用户上传一张眼疾图片识别其名称。
804 5
基于Python深度学习的眼疾识别系统实现~人工智能+卷积网络算法
|
机器学习/深度学习 算法 数据挖掘
K-means聚类算法是机器学习中常用的一种聚类方法,通过将数据集划分为K个簇来简化数据结构
K-means聚类算法是机器学习中常用的一种聚类方法,通过将数据集划分为K个簇来简化数据结构。本文介绍了K-means算法的基本原理,包括初始化、数据点分配与簇中心更新等步骤,以及如何在Python中实现该算法,最后讨论了其优缺点及应用场景。
1897 6
|
机器学习/深度学习 算法 数据可视化
利用SVM(支持向量机)分类算法对鸢尾花数据集进行分类
本文介绍了如何使用支持向量机(SVM)算法对鸢尾花数据集进行分类。作者通过Python的sklearn库加载数据,并利用pandas、matplotlib等工具进行数据分析和可视化。
1475 70
|
机器学习/深度学习 人工智能 算法
【宠物识别系统】Python+卷积神经网络算法+深度学习+人工智能+TensorFlow+图像识别
宠物识别系统,本系统使用Python作为主要开发语言,基于TensorFlow搭建卷积神经网络算法,并收集了37种常见的猫狗宠物种类数据集【'阿比西尼亚猫(Abyssinian)', '孟加拉猫(Bengal)', '暹罗猫(Birman)', '孟买猫(Bombay)', '英国短毛猫(British Shorthair)', '埃及猫(Egyptian Mau)', '缅因猫(Maine Coon)', '波斯猫(Persian)', '布偶猫(Ragdoll)', '俄罗斯蓝猫(Russian Blue)', '暹罗猫(Siamese)', '斯芬克斯猫(Sphynx)', '美国斗牛犬
835 29
【宠物识别系统】Python+卷积神经网络算法+深度学习+人工智能+TensorFlow+图像识别
|
机器学习/深度学习 人工智能 算法
探索人工智能中的强化学习:原理、算法与应用
探索人工智能中的强化学习:原理、算法与应用
|
机器学习/深度学习 人工智能 算法
基于Python深度学习的【垃圾识别系统】实现~TensorFlow+人工智能+算法网络
垃圾识别分类系统。本系统采用Python作为主要编程语言,通过收集了5种常见的垃圾数据集('塑料', '玻璃', '纸张', '纸板', '金属'),然后基于TensorFlow搭建卷积神经网络算法模型,通过对图像数据集进行多轮迭代训练,最后得到一个识别精度较高的模型文件。然后使用Django搭建Web网页端可视化操作界面,实现用户在网页端上传一张垃圾图片识别其名称。
718 0
基于Python深度学习的【垃圾识别系统】实现~TensorFlow+人工智能+算法网络
|
机器学习/深度学习 人工智能 算法
【手写数字识别】Python+深度学习+机器学习+人工智能+TensorFlow+算法模型
手写数字识别系统,使用Python作为主要开发语言,基于深度学习TensorFlow框架,搭建卷积神经网络算法。并通过对数据集进行训练,最后得到一个识别精度较高的模型。并基于Flask框架,开发网页端操作平台,实现用户上传一张图片识别其名称。
1085 0
【手写数字识别】Python+深度学习+机器学习+人工智能+TensorFlow+算法模型
|
机器学习/深度学习 人工智能 算法
基于深度学习的【蔬菜识别】系统实现~Python+人工智能+TensorFlow+算法模型
蔬菜识别系统,本系统使用Python作为主要编程语言,通过收集了8种常见的蔬菜图像数据集('土豆', '大白菜', '大葱', '莲藕', '菠菜', '西红柿', '韭菜', '黄瓜'),然后基于TensorFlow搭建卷积神经网络算法模型,通过多轮迭代训练最后得到一个识别精度较高的模型文件。在使用Django开发web网页端操作界面,实现用户上传一张蔬菜图片识别其名称。
1037 0
基于深度学习的【蔬菜识别】系统实现~Python+人工智能+TensorFlow+算法模型
|
机器学习/深度学习 人工智能 算法
【车辆车型识别】Python+卷积神经网络算法+深度学习+人工智能+TensorFlow+算法模型
车辆车型识别,使用Python作为主要编程语言,通过收集多种车辆车型图像数据集,然后基于TensorFlow搭建卷积网络算法模型,并对数据集进行训练,最后得到一个识别精度较高的模型文件。再基于Django搭建web网页端操作界面,实现用户上传一张车辆图片识别其类型。
652 0
【车辆车型识别】Python+卷积神经网络算法+深度学习+人工智能+TensorFlow+算法模型

热门文章

最新文章