Matlab:BP神经网络算法,二叉决策树

1、BP神经网络算法

(1)步骤

1.准备训练数据和目标值

2.创建并配置BP神经网络模型

3.训练BP神经网络模型

4.用BP神经网络模型预测数据

例:某企业第一年度营业额为132468,第二年度为158948,第三年度为183737,预测第四年度的营业额

复制代码
%准备训练数据和目标值
x = [1 2 3]'; %年度
y = [132468 158948 183737]'; %营业额

%创建BP神经网络模型
net = feedforwardnet(10);

%配置BP神经网络模型
net.trainParam.showWindow = false;
net.trainParam.epochs = 1000;
net.divideFcn = '';
net.performFcn = 'mse';

%调整输入输出数据的格式
x_train = x';
y_train = y';

%训练BP神经网络模型
net = train(net, x_train, y_train);

%预测第四年度的营业额
x_pred = 4; %第四年度
y_pred = sim(net, x_pred);

%输出预测结果
disp(y_pred);

(2)+可视化

复制代码
format long
p=1:16;      %输入矢量
t=0.00001*[114333 115823 117171 118517 119850 121121 122389 123626 124761 125786 126743 127627 128453 129227 129988 130756]    %目标矢量
net = newff([0 8],[10 1],{'tansig' 'purelin'},'trainlm');  %初始化神经网络 net.trainParam.epochs=2500    %确定最大训练次数
net.trainParam.goal = 0.00000001;    %确定预期误差            
net.trainParam.lr=0.02     %确定学习速率,即权值
net = train(net,p,t);       %进行训练
p2=1:120
y2 = sim(net,p2)
p=1989+p;
p2=1989+p2;
plot(p,t,'o',p2,y2,'*')   %绘制拟合曲线
grid on

2、二叉决策树

(1)步骤

1.加载数据

2.设置特征和标签

3.构建二叉决策树模型

4.预测一个新样本的标签

(2)例:

复制代码
data = [1, 2, 0;
        2, 3, 1;
        3, 4, 0;
        4, 5, 1;
        5, 6, 0;
        6, 7, 1;
        7, 8, 0;
        8, 9, 1];
X = data(:, 1:2);  %特征(第1列和第2列作为特征X)
Y = data(:, 3);    %标签(第3列作为标签Y)
tree = fitctree(X, Y);
new_sample = [9, 10];  %新样本的特征
predicted_label = predict(tree, new_sample);
disp(predicted_label);
view(tree, 'Mode', 'Graph');

2.鸢尾花数据集

复制代码
%准备数据
load fisheriris;            %加载鸢尾花数据集
X = meas(:, 3:4);           %选择两个特征作为输入
Y = species;                %类别标签

tree = fitctree(X, Y);      %构建决策树模型

view(tree, 'Mode', 'graph');%可视化决策树

%预测新样本
newX = [5 1.5];             %新样本的特征值
predictedClass = predict(tree, newX);
disp(['预测类别:' char(predictedClass)]);
相关推荐
黑眼圈子2 分钟前
动态规划问题专项练习(未编辑完成...
学习·算法·动态规划
Aliex_git3 分钟前
Nuxt 学习笔记(一)
前端·笔记·学习
探物 AI4 分钟前
【感知·车道线检测】UFLDv2车道线检测与车道偏离预警(LDWS)实战
人工智能·算法·目标检测·计算机视觉
烤麻辣烫5 分钟前
json与fastjson
前端·javascript·学习·json
Swilderrr6 分钟前
学术研读报告:MEM1面向长视距智能体的记忆 - 推理协同框架
人工智能
菜鸟丁小真8 分钟前
LeetCode hot100 -54.螺旋矩阵
算法·leetcode·矩阵·知识点总结
aLTttY15 分钟前
Spring Boot整合AI大模型实现智能问答系统实战
人工智能·spring boot·后端
weixin_4684668516 分钟前
排列组合算法之隔板问题与错排公式
c++·算法·数学建模·排列组合·竞赛·错排·隔板
tryqaaa_19 分钟前
学习日志(二)【linux全部命令,http请求头{有例题},Php语法学习】
linux·学习·http·php·web
wsoz27 分钟前
Leetcode链表-day9
c++·算法·leetcode·链表