Matlab学习笔记(二)

三:

数据建模

云模型

  • p51

Logistic回归

  • 商业机构贷款 p54

主成分分析(PCA)

  • 企业综合实力排序 p59
  • 讲解

支持向量机(SVM)

  • emmm代码没看懂,先放这吧 p64

K-means

  • 这个比较简单,之前写过

朴素贝叶斯判断模型

  • 等以后搞机器学习的时再学吧=_=
  • 估计现在也用不到。。。

四:

灰色预测

先等等再学这个。。。

五:

遗传算法

  • 无约束目标函数最大值求解 p113
%主程序: 用遗传算法求解y = 200 * exp(-0.05 * x) .* sin(x) 再[-2 2]上的最大值
clc;
clear all;
close all;
global bitLength;
global st;
global ed;
bounds = [-2 2];  %一维变量的取值范围
precision = 0.0001; %运算精度
st = bounds(:, 1);
ed = bounds(:, 2);

%计算需要多长的染色体
bitLength = ceil(log2((ed - st)' ./precision));
popSize = 50; %初始种群大小
generationMax = 12; %最大代数
proCro = 0.90; %交配概率
proMut = 0.09; %变异概率

%产生初始种群
population = round(rand(popSize, bitLength));
%计算适应度, 返回适应度fitVal和累积概率proSum;
[fitVal, proSum] = fitnessFun(population);
cur = 1;
while(cur < generationMax +1)
    for j = 1 : 2 : popSize
        %选择操作
        sel = selection(population, proSum);
        %交叉操作
        cro = cross(population, sel, proCro);
        temp(j, :) = cro(1, :);
        temp(j + 1, :) = cro(2, :);
        %变异操作
        new(j, :) = mutation(temp(j, :), proMut);
        new(j + 1, :) = mytation(temp(j + 1, :), proMut);  
    end
    population = new; %新种群
    %计算新种群的最好的适应度和平均适应度
    [fitVal, proSum] = fitnessFun(popalation);
    [fmax, nmax] = max(fitVal);
    fmean = mean(fitVal);
    ymax(cur) = fmax;
    ymean(cur) = fmean;
    %记录当前代最佳染色体个体
    x = tr2to10(population(nmax, :));
    %自变量的取值范围是[-2 2], 需要把经过遗传运算的最佳染色体整合到对应区间
    xx = st + x * (ed - st) / (power(2, bitLength) - 1);
    xmax(cur) = xx;
    cur = cur + 1;
end
cur = cur - 1;
bestPopulation = xx;
besetTargetFunVal = targetFun(xx);
%绘制曲线
figure(1);
hand1 = plot(1 : cur, ymax);
set(head1, 'linestyle', '-', 'linewidth', 1.8, 'marker', '*', 'markersize', 6);
hold on;
hand2 = plot(1 : cur, ymean);
set(hand1, 'color', 'r', 'linestyle', '-', 'linewidth', 1.8, 'marker', 'h', 'markersize', 6);
xlabel('进化代数'); ylabel('最大\平均适应度');
xlim([1 generationMax]);
legend('最大适应度', '平均适应度');
box off; 
hold off;

%交叉操作
function temp = cross(population, sel, pro);
bitLength = size(population, 2);
pcc = calPro(pro);
if pcc == 1
    pos = round(rand * (bitLength - 2)) + 1; %在[1, bitLength - 1]范围内随机pos
    temp(1, :) = [population(sel(1), 1 : pos) population(sel(2), pos + 1 : bitLength)];
    temp(2, :) = [populayion(sel(2), 1 : pos) population(sel(1), pos + 1 : bitLength)];
else
    temp(1, :) = population(sel(1), :);
    temp(2, :) = population(sel(2), :);
end


%计算适应度函数
function [fitVal, proSum] = fitnessFun(population);
global bitLength;
global st;
global ed;
popSize = size(population, 1);
for i = 1 : popSize
    x = tr2to10(population(i, :));  
    xx = st + x * (ed - st) / (power(2, bitLength) - 1);
    fitVal(i) = targetFun(xx);
end
fitVal = fitVal' +233;
%计算选择概率
fsum = sum(fitVal);
temp = fitVal / fsum;
%计算累积概率
proSum(1) = temp(1);
for i = 2 : popSize
    proSum(i) = proSum(i - 1) + temp(i);
end
proSum = proSum';
end

%新种群变异操作
function new = mutation(temp, proMut)
bitLength = size(temp, 2);
new = temp;
pmm = calPro(proMut);
if(pmm == 1)
    pos = round(rand * (bitLength - 1)) + 1;
    new(pos) = abs(temp(pos) - 1);
end
end
%计算概率
function pcc = calPro(pc)
temp(1 : 100) = 0;
cnt = round(100 * pc);
test(1 : cnt) = 1;
pos = round(rand * 99) + 1;
pcc =  test(pos);
end

%选择操作
function sel = selection(population, proSum)
for i = 1 : 2
    r = rand;
    prand = proSum - r;
    j = 1;
    while prand(j) < 0
        j = j + 1;
    end
    sel(i) = j;
end
end

%进制转换
function x = tr2to10(population)
bitLength = size(population, 2);
x = population(bitLength);
for i = 1 : bitLength - 1
    x = x + population(bitLength - i)* power(2, i);
end
end
%目标函数
function y = targetFun(x)
y = 200 * exp(-0.05 * x) .* sin(x);
end
end
  • 多约束线性规划问题 p116
    二次接力
    这里写图片描述

神经网络

  • 。。。p140

猜你喜欢

转载自blog.csdn.net/yijiull/article/details/79266231