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)]);
相关推荐
Biomamba生信基地4 分钟前
R语言基础| 下载、安装
开发语言·r语言·生信·医药
姜君竹5 分钟前
QT的工程文件.pro文件
开发语言·c++·qt·系统架构
奇树谦10 分钟前
使用VTK还是OpenGL集成到qt程序里哪个好?
开发语言·qt
嘉陵妹妹13 分钟前
深度优先算法学习
学习·算法·深度优先
VBA633721 分钟前
VBA之Word应用第三章第十节:文档Document对象的方法(三)
开发语言
老胖闲聊30 分钟前
Python Rio 【图像处理】库简介
开发语言·图像处理·python
GalaxyPokemon42 分钟前
LeetCode - 53. 最大子数组和
算法·leetcode·职场和发展
码界奇点1 小时前
Python Flask文件处理与异常处理实战指南
开发语言·python·自然语言处理·flask·python3.11
浠寒AI1 小时前
智能体模式篇(上)- 深入 ReAct:LangGraph构建能自主思考与行动的 AI
人工智能·python
乖乖是干饭王1 小时前
Linux系统编程中的_GNU_SOURCE宏
linux·运维·c语言·学习·gnu