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)]);
相关推荐
古城小栈几秒前
Spring Boot + 代理 AI:解锁供应链自动化决策新范式
人工智能·spring boot·自动化
ULTRA??几秒前
STL deque 的详细特征
c++·算法
像风一样自由2020几秒前
基于PyTorch实现U-Net的路面裂缝检测系统
人工智能·pytorch·python
mys5518几秒前
杨建允:AI搜索趋势对互联网营销的影响
人工智能·geo·ai搜索优化·geo优化·ai引擎优化
九死九歌1 分钟前
【Sympydantic】使用sympydantic,利用pydantic告别numpy与pytorch编程中,tensor形状带来的烦人痛点!
开发语言·pytorch·python·机器学习·numpy·pydantic
Kiri霧2 分钟前
Go切片详解
开发语言·后端·golang
yongui478342 分钟前
MATLAB 二维方腔自然对流 SIMPLE 算法
人工智能·算法·matlab
翔云 OCR API4 分钟前
API让文档信息“活”起来:通用文档识别接口-开发者文字识别API
前端·数据库·人工智能·mysql·ocr
540_5407 分钟前
ADVANCE Day22_复习日
人工智能·python·机器学习
二进制coder9 分钟前
C++ 中的 Interface:概念、实现与应用详解
开发语言·c++