Machine Learning

import

        1.使用readtable指令可以读出xls,txt等格式的文件,并以table形式储存在matlab种,例如输入data=readtable(“M.txt”),会把储存名为M的txt的文件读出,并以table形式储存在变量data中。
        2.axis equal命令可使plot的x轴与y轴的坐标尺度对齐

Process Data

1.对于使用readtable读出的table数据,可以直接对其中的变量进行加减乘除的操作,例如输入data.time=data.time*1.5,即可将data中的time列数据乘以1.5后重新赋值给time列
2.想要index其中的数据也很方便,例如输入first=data.time(1),即可把time列中的第一个元素的值赋值给变量first
 

Calculating Features

1.处理数据时提取数据的feature很重要,例如对于手写字母,字母的长宽比,持续时间都可以作为feature来判断是哪个字母

2.使用scatter函数可以作点图,使用gscatter函数则可以把不同variable以不同颜色在图中显示,例如输入gscatter(features.aspectratio, features.duration, features.character),则会以数据feature中的aspecratio,duration为x,y轴,根据character中元素的不同来作图。
Machine Learning_第1张图片Machine Learning_第2张图片

以上两张图分别为使用scatter和gscatter函数对相同数据作图,gscatter函数根据不同group的元素使用了不同颜色

下图为局部放大后,可以发现,使用以上两种数据,基本可以区分J,M,V三个字母
Machine Learning_第3张图片

 Build a Model

1.knn

k-nearest neighbor model,k-近邻算法,用于分类和统计的算法,一个输入的分类由其最近邻元素的分类决定。
matlab种可以使用fitcknn函数来生成一个kNN model,基本句式为mdl=fitcknn(data,“ResponseVariable”),其中ResponseVariable为data中想要分类的变量
使用predict函数则可以使用已经训练的model来决定新输入变量的分类,基本句式为predictions=predict(model,newdata)
在fitcknn函数中可以用"NumNeighbors"来修改通过几个最近邻元素来新变量的分类,默认为1,如果输入mdl=fitcknn(data,“ResponseVariable”,“NumNeighbors”,5),则通过最近邻的5个元素来决定新变量的分类,这可以减少由于某些离散点导致的误差

Evaluate the Model

load featuredata.mat
testdata
knnmodel = fitcknn(features,"Character","NumNeighbors",5);
predictions = predict(knnmodel,testdata)
iscorrect = predictions == testdata.Character
accuracy = sum(iscorrect)/numel(predictions)  //计算准确率,使用了sum和numel函数,numel是元素总数
iswrong = predictions ~= testdata.Character   //~=是不等号
misclassrate = sum(iswrong)/numel(predictions)
figure
confusionchart(testdata.Character,predictions);   //confusionchart是以真实值为y轴,预测值为x轴作图
Machine Learning_第4张图片

看起来还行,然后来尝试使用不正确分类的逻辑数组对 testdata 和 predictions 进行索引,以获得被误分类的观测值的数据。找到这些观测值在特征空间中处于什么位置

 idx=find(iscorrect==0)   //使用find函数来输出iscorrect=0的index
originalLetter=testdata.Character(idx)
fulseprediction=predictions(idx)
gscatter(features.AspectRatio, features.Duration, features.Character)
hold on
scatter(testdata.AspectRatio(5),testdata.Duration(5))
scatter(testdata.AspectRatio(6),testdata.Duration(6))
Machine Learning_第5张图片

 然后来尝试更多字母

load featuredata13letters.mat
features
testdata
gscatter(features.AspectRatio,features.Duration,features.Character)
xlim([0 5])
ylim([0 1.5])
knnmodel=fitcknn(features,"Character","NumNeighbors",5)
predictions=predict(knnmodel,testdata)
incorrect=testdata.Character~=predictions
misclass=sum(incorrect)/numel(incorrect)
confusionchart(testdata.Character,predictions)
Machine Learning_第6张图片

Machine Learning_第7张图片

 这个混淆图就有点。。。,所以单凭两个特征很难分清这么多字母


 

你可能感兴趣的:(机器学习,人工智能)