

【Matlab工具箱】十几个机器学习代码
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.

【Matlab工具箱】十几个机器学习代码
2016年07月06日Author: Guofei
文章归类: 趣文,文章编号:
版权声明:本文作者是郭飞。转载随意,但需要标明原文链接,并通知本人
原文链接:https://www.guofei.site/2016/07/06/MLMatlab.html
决策树
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图的方法:
- plotroc
plotroc(y,p)
- roc
[tpr,fpr,thresholds] = roc([y;1-y],[p;1-p]); figure plot(fpr{1},tpr{1})
- 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神经网络:
- input layer. Matlab不把input layer当成1层,因此显示是Two-layer-forward neural network
- Hidden layer. 用sigmoid函数
- output. 用线性函数
建模过程:
- 选择Validating,Testing的比例
- 输入Hidden Neurouns数量,推荐值参考我的另一篇博客
- 输入算法算法包括3种
- Levenberg-Marquardt
- Bayesian Regularization
- Scaled conjugate Gradient
- train!
会给出训练图,图上各个部分的意义很容易理解
训练完后后可以画各种图 一些意义:- validation=6, 意思是连续6次,validation效果变差,这时算法停止。
- regression是预测值和真实值的回归
- 回到nftool界面,点Next. 可以保存代码、模型、数据 例如,模型保存为network名为net,下次可以用net(data)命令做预测了 net.b, net.iw, net.Lw
Pattern recongnition and Classification
3层神经网络:
- 第一层input layer
- 第二层sigmoid
- 第三层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
-
9
Matlab 的优化工具箱 作者: 张志强 , 发表于 2...
-
11
【Matlab工具箱】REF径向基网络 2016年05月06日 Author: Guofei 文章归类: 趣文,文章编号: 版权声明:本文作者是郭飞。转载随意,但需要标明...
-
4
【Matlab工具箱】线性神经网络 2016年05月06日 Author: Guofei 文章归类: 趣文,文章编号: 版权声明:本文作者是郭飞。转载随意,但需要标明原...
-
7
【Matlab工具箱】BP神经网络 2016年05月06日 Author: Guofei 文章归类: 趣文,文章编号: 版权声明:本文作者是郭飞。转载随意,但需要标明原文...
-
1
【Matlab工具箱】感知机 2016年05月06日 Author: Guofei 文章归类: 趣文,文章编号: 版权声明:本文作者是郭飞。转载随意,但需要标明原文链接...
-
5
与通用应用程序编程相比,机器学习(Machine Learning)是一个相对较新的工作领域。如今,硬件和软件均已支持大型机器学习项目,以使公司能够更好地进行决策,而机器学习的工具和解决方案已席卷了技术领域。这产生了一个称为MLOps的新领域。MLOps把来自DevOp...
-
3
分享一个很不错的车辆垂向动力学matlab工具箱 精选 原创 domi+1 2022-09-22 12:34:07
-
7
Matlab遗传算法工具箱的使用及实例(非线性规划) 推荐 原创 domi+1 2022-12-14 12:05:48
-
7
MATLAB人工神经网络ANN代码 本文介绍基于
-
2
你好,这里是 Dotnet 工具箱,定期分享 Dotnet 有趣,实用的工具和组件,希望对您有用! 【JIEJIE.NET - 强大的 .NET 代码混淆工具】 JIEJIE.NET JIEJIE.NET 是一个使用 C# 开发...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK