当前位置:网站首页>机器学习--手写英文字母1--分类流程
机器学习--手写英文字母1--分类流程
2022-07-28 10:15:00 【可爱的我啊】
目录
1.导入数据
手写信件作为单独的文本文件存储。每个文件以逗号分隔,包含四列:时间戳、笔的水平位置、笔的垂直位置和笔的压力。时间戳是自数据收集开始以来经过的毫秒数。其他变量以规范化单位(0到1)表示。对于笔位置,0表示书写表面的下边缘和左边缘,1表示上边缘和右边缘。
letter = readtable("J.txt");
plot(letter.X,letter.Y)
axis equal
letter = readtable("M.txt");
plot(letter.X,letter.Y)
axis equal

2.数据处理
手写数据的笔位置以标准化单位(0到1)测量。不过,用于记录数据的平板电脑并非方形。这意味着1的垂直距离对应10英寸,而相同的水平距离对应15英寸。若要更正此问题,应将水平单位调整到范围[0 1.5]而不是[0 1]。
letter = readtable("M.txt")
letter.X = 1.5*letter.X;
plot(letter.X,letter.Y)
axis equal

时间值没有物理意义。它们表示从数据收集会话开始经过的毫秒数。这使得我们很难通过时间来解释笔迹图。一个更有用的时间变量是从每个字母开始的持续时间(以秒为单位)。
letter.Time = letter.Time - letter.Time(1)
letter.Time = letter.Time/1000
plot(letter.Time,letter.X)
plot(letter.Time,letter.Y)

3.特征计算
这些字母的哪些方面可以用来区分J和M或V?我们的目标不是使用原始信号,而是计算将整个信号提取为简单、有用的信息单位(称为特征)的值。
对于字母J和M,一个简单的特征可能是纵横比(字母相对于宽度的高度)。J可能又高又窄,而M可能更方。
与J和M相比,V的写入速度快,因此信号的持续时间也可能是一个区别特征。

letter = readtable("M.txt");
letter.X = letter.X*1.5;
letter.Time = (letter.Time - letter.Time(1))/1000
plot(letter.X,letter.Y)
axis equal
#上面的是前面重复代码
dur = letter.Time(end)
aratio = range(letter.Y)/range(letter.X)
4.特征提取
MAT文件featuredata.MAT包含一个包含470个字母的提取特征的表格,这些字母由不同的人书写。表格特性有三个变量:AspectRatio和Duration(前一节计算的两个特性)和Character(已知字母)。
load featuredata.mat
features
scatter(features.AspectRatio,features.Duration)
尚不清楚这些特征是否足以区分数据集中的三个字母(J、M和V)。gscatter函数生成一个分组散点图,即根据分组变量对点进行着色的散点图。
gscatter(features.AspectRatio,features.Duration,features.Character)
5.构建模型与预测

load featuredata.mat
features
testdata
knnmodel = fitcknn(features,"Character")
根据数据建立模型后,可以使用它对新的观测进行分类。这只需要计算新观测值的特征,并确定它们在预测空间的哪个区域。
predictions = predict(knnmodel,testdata)
默认情况下,fitcknn适合k=1的kNN模型。也就是说,该模型仅使用已知的最接近的一个示例来对给定的观测进行分类。这使得模型对训练数据中的任何异常值(如上图中突出显示的异常值)敏感。离群值附近的新观测可能被错误分类。解决这个问题的一个简单方法是增加k的值(即使用几个邻居中最常见的类)。
knnmodel = fitcknn(features,"Character","NumNeighbors",5)
predictions = predict(knnmodel,testdata)
6.评价一个模型
kNN模型有多好?testdata表包含测试观察的已知类。您可以将已知类与kNN模型的预测进行比较,以了解模型在新数据上的性能。
load featuredata.mat
testdata
knnmodel = fitcknn(features,"Character","NumNeighbors",5);
predictions = predict(knnmodel,testdata)
iscorrect = predictions == testdata.Character
通过将正确预测数除以预测总数来计算正确预测的比例。将结果存储在名为accurity的变量中。可以使用sum函数确定正确预测的数量,使用numel函数确定预测的总数。
accuracy = sum(iscorrect)/numel(predictions)![]()
错误率计算
iswrong = predictions ~= testdata.Character
misclassrate = sum(iswrong)/numel(predictions)
准确度和错误分类率为模型的整体性能提供了一个单一的值,但是可以看到模型混淆的类的更详细的细分。混淆矩阵显示了真实类和预测类的每个组合的观察数。

混淆矩阵通常通过根据元素的值对元素进行着色来可视化。通常对角线元素(正确的分类)用一种颜色着色,而其他元素(错误的分类)用另一种颜色着色。您可以使用confusionchart函数可视化混淆矩阵。

7.review
现在,您有了一个简单的双特征模型,它可以很好地处理三个特定的字母(J、M和V)。这种模式也适用于整个字母表吗?在这个交互中,您将创建和测试与之前相同的kNN模型,但是13个字母(英文字母表的一半)。
load featuredata13letters.mat
features
testdata
gscatter(features.AspectRatio,features.Duration,features.Character)
xlim([0 10])
knnmodel = fitcknn(features,"Character","NumNeighbors",5);
predictions = predict(knnmodel,testdata);
misclass = sum(predictions ~= testdata.Character)/numel(predictions)
confusionchart(testdata.Character,predictions);

边栏推荐
- 用K-means聚类分类不同行业的关税模型
- 吴雄昂遭Arm罢免内幕:建私人投资公司,损害了股东利益?
- Differences among pipes, pipe passes and pipe States
- 16. String inversion
- 发力大核、独显!英众科技2020十代酷睿独显产品发布
- SQL Server 2016 learning record - Data Definition
- ogg参数filter的使用问题【急】
- Talk about the problem of preventing others from debugging websites through console based on JS implementation
- SQL Server 2016 学习记录 --- 数据更新
- 16、字符串反转
猜你喜欢

15、判断二维数组中是否存在目标值

django-celery-redis异步发邮件

gcc: error trying to exec 'as': execvp: No such file or directory

7、二分法——寻找一组重复或者有序但是旋转的数组

Record a parent-child project in idea, modify the name of project and module, and test it personally!

数据库安全 --- 创建登录名 用户+配置权限【笔记】

11、链表反转

C语言 二级指针详解及示例代码

初识SuperMap iDesktop

SQL Server 2016学习记录 --- 连接查询
随机推荐
利用正则表达式从文件路径中匹配文件名
Double pointer technique
Deadlock algorithm: banker algorithm and security algorithm
Hurun released the 2020 top 10 Chinese chip design private enterprises: Huawei Hisilicon did not appear on the list!
India plans to ban China Telecom equipment! Can we really do without Huawei and ZTE?
15. Judge whether the target value exists in the two-dimensional array
Differences among pipes, pipe passes and pipe States
6. Double pointer -- the sum of the two numbers of the incremental array is equal to the target number
印度计划禁用中国电信设备!真离得开华为、中兴?
[cloud based co creation] Huawei cloud: metastudio digital content production line, which seamlessly integrates the virtual world with the real world
简介
配置树莓派,过程和遇到问题
Context values traps and how to avoid or mitigate these traps in go
6、双指针——递增数组两数之和与目标数相等
中芯国际科创板IPO顺利过会,市值有望突破2000亿!
中兴通讯总裁徐子阳:5nm芯片将在2021年推出
ACM寒假集训#5
Prometheus operation and maintenance tool promtool (IV) TSDB function
SQL Server 2016 learning records - View
集群为什么需要root权限