6

【Matlab工具箱】十几个机器学习代码

 3 years ago
source link: https://www.guofei.site/2016/07/06/MLMatlab.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

【Matlab工具箱】十几个机器学习代码

2016年07月06日

Author: Guofei

文章归类: 趣文,文章编号:


版权声明:本文作者是郭飞。转载随意,但需要标明原文链接,并通知本人
原文链接:https://www.guofei.site/2016/07/06/MLMatlab.html

Edit

决策树

clear;clc;close all
load fisheriris;
X=meas;Y=species;
Mdl=fitctree(X,Y,'MaxNumSplits',8,'CrossVal','on');
view(Mdl.Trained{1},'Mode','graph');

logit回归

[b,dev,stats]=glmfit(x,y,'binomial', 'link', 'logit');
%b系数,dev表示残差
p = glmval(b,x, 'logit');

SVM

生成一个ClassificationSVM 对象

SVMModel = fitcsvm(X,y)
SVMModel.ClassNames%模型中的类名
%模型的支持向量
SVMModel.SupportVectors
%模型的支持向量
SVMModel.IsSupportVector
label = predict(SVMModel,TBL)
label = predict(SVMModel,X)example
[label,Score] = predict(___)

误差判定:

CVSVMModel = crossval(SVMModel);
classLoss = kfoldLoss(CVSVMModel)

贝叶斯

ObjBayes = NaiveBayes.fit(training,group,'Distribution','kernel')
pre0 = ObjBayes.predict(training);
Mdl = fitcnb(X,Y)
estimates = Mdl.DistributionParameters
load fisheriris
X = meas;
Y = species;
classNames = {'setosa','versicolor','virginica'}; % Class order
prior = [0.5 0.2 0.3];
Mdl = fitcnb(X,Y,'ClassNames',classNames,'Prior',prior)

注意:训练过程不适用prior,因此可以在训练后改prior

CVMdl=crossval(Mdl) loss=kfoldLoss(CVMdl)

判别分析

[class,err,POSTERIOR,logp,coeff] = classify(sample,training,group)

回归:

x=[0:30]';
y=x.*rand(size(x))*100;
one1=ones(size(x));
[b,bint,r,rint,stats]=regress(y,[one1,x]);
b,bint,stats
rcoplot(r,rint)

系统聚类

%系统聚类完整步骤
clear
X=rand(100,4)
Y=pdist(X)%距离向量
Z=linkage(Y,'average')%从距离向量创建聚类树矩阵
[H,T]=dendrogram(Z,'colorthreshold','default')%画树形图
cluster(Z,3)%分3类

pdist
squareform%把pdist生成的距离矩阵转化(或逆转化)成向量形式
linkage
dendrogram
cophenet
cluster
cluserdata
inconsistent

模型验证方法

注1:一般情况下,我们把y=1作为class1;然而,matlab 把第一行作为class1,因此,在二分类问题中,需要做这样的变换:[y;1-y],[p;1-p]
注2:注1的情况,是因为confusion函数中,把单行的处理给弄反了!roc函数中,就可以不用这么麻烦,直接用(y,p)就好了
注3:还缺一个C统计量,自带算法没有
注4:plotroc不支持subplot等运算,还是手动分解一下比较好

confusion matrix

[c,cm,ind,per] = confusion([y;1-y],[p;1-p])   

c:错误率
cm:计数,cm(i,j)表示y落在class i中,p落在class j中
ind:cm对应的原始数据的序号
per:per(i,1:4)分别对应:FN/total actual negative,…

ROC

几种画ROC图的方法:

  1. plotroc
    plotroc(y,p)
  2. roc
    [tpr,fpr,thresholds] = roc([y;1-y],[p;1-p]);
    figure
    plot(fpr{1},tpr{1})
  3. plot
    plot_it_y=[]
    plot_it_y=[]
    for i=1:100
     p1=p>i/100;
     [c,cm,ind,per] = confusion([y;1-y],[p1;1-p1]);
     plot_it_y(i,1)=cm(1,1)/(cm(1,1)+cm(1,2));
     plot_it_x(i,1)=cm(2,1)/(cm(2,1)+cm(2,2));
    end
    plot(plot_it_x,plot_it_y)

【鼠标流1】一键22个机器学习模型

【鼠标流】Matlab一键生成22个机器学习模型的方法:

APPS–>Classification Fitting–>New Session–>Start Session
选择数据后自动开始运行模型

【鼠标流2】用鼠标实现4种神经网络

【鼠标流】Matlab一键使用神经网络 命令行敲入:

nnstart  

会有4个选择按钮,对应4组神经网络模型

  • Fitting(nftool)
  • Pattern recongnition and Classification(nprtool)
  • Clustering (nctool)
  • Dynamic Time series (ntstool)

也可以点击APP按钮调出这4组模型

Fitting

nftool
模型是3层BP神经网络:

  1. input layer. Matlab不把input layer当成1层,因此显示是Two-layer-forward neural network
  2. Hidden layer. 用sigmoid函数
  3. output. 用线性函数

建模过程:

  1. 选择Validating,Testing的比例
  2. 输入Hidden Neurouns数量,推荐值参考我的另一篇博客
  3. 输入算法算法包括3种
    1. Levenberg-Marquardt
    2. Bayesian Regularization
    3. Scaled conjugate Gradient
  4. train! 会给出训练图,图上各个部分的意义很容易理解
    训练完后后可以画各种图 一些意义:
    1. validation=6, 意思是连续6次,validation效果变差,这时算法停止。
    2. regression是预测值和真实值的回归
  5. 回到nftool界面,点Next. 可以保存代码、模型、数据 例如,模型保存为network名为net,下次可以用net(data)命令做预测了 net.b, net.iw, net.Lw

Pattern recongnition and Classification

3层神经网络:

  1. 第一层input layer
  2. 第二层sigmoid
  3. 第三层softmax

操作与regression几乎相同
图多了confusion matrix,ROC

Clustering

用的是SOM神经网络

  • Size of two-dimension Map 如果设为2,结果分为4类
    如果设为n,结果分为n2n2类

结果分析:
Plots->SOM Neighbor Distances
这个图中,颜色越深,表示距离越远
SOM Sample Hits
分组

调整模型:
生成的代码里,改变神经元数量:

dimension1 = 1;
dimension2 = 3;

这样,就能把样本分为3类了

Dynamic Time series

时间序列,下面有3个模型,各自用途写的比较清晰。
建模过程大致与上面类似,不同点是:

  • 要输入滞后阶d
  • 图多了个Time-Series Response

PCA

barttest
pcacov
prncomp
pcares
pca

您的支持将鼓励我继续创作!

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK