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)]);
相关推荐
7***53348 分钟前
PHP在微服务中的Phalcon
开发语言·微服务·php
ATMQuant10 分钟前
量化指标解码11:挤压动量 - 捕捉低波动后的爆发行情
人工智能·ai·量化交易·vnpy
Ayanami_Reii13 分钟前
进阶数学算法-取石子游戏(ZJOI2009)
数学·算法·游戏·动态规划·区间dp·博弈论
一只小小汤圆14 分钟前
已知圆弧的起点、终点、凸度 求圆弧的圆心
算法
周杰伦fans20 分钟前
在C#中,`StringContent` 是 `HttpContent` 的一个派生类
开发语言·数据库·c#
Aurora-silas21 分钟前
Mac 本地运行 Hugging Face 大模型完全指南:PyTorch (MPS) vs Apple MLX
人工智能·pytorch·macos
DanB2422 分钟前
Java(多线程)
java·开发语言·python
战南诚22 分钟前
Python函数式编程
开发语言·python
石像鬼₧魂石25 分钟前
常用的安全审计工具可以用于靶机学习
学习·安全
O***p60427 分钟前
Java在分布式中的Archaius
java·开发语言·分布式