matlab中根据分类条件产生带标签的数据(二)

一、实验原理:略
二、实验步骤
1,编辑函数generate_signedData;

%% 本函数的功能是根据所给分类方程,产生一组带标签的数据点(二维)
%  row表示需要的数据点个数;
%  range_x、range_y分别是数据[横纵坐标取值范围],都是 1*2 矩阵.
%  分类方程形式:y = a*x^m + b
function data = generate_signedData(row,range_x,range_y,a,m,b)
data=zeros(row,3);
min_x=range_x(1);
max_x=range_x(2);
min_y=range_y(1);
max_y=range_y(2);
for i=1:1:row
    %rand('seed',0); %初始化高斯随机生成器为0,这对结果的可重复性很重要
    x = min_x+(max_x-min_x)*rand(1,1);
    y = min_y+(max_y-min_y)*rand(1,1);
    sign = zeros(1,1);
    if y>(a*x^m + b) 
        sign = 1;
    else
        sign = -1;
    end
    data(i,:)=[x,y,sign];
end

figure;
%在图片中显示所有数据点
for i=1:1:row
    x1 = data(i,1);
    x2 = data(i,2);
    sign = data(i,3);
    if sign == 1  
        plot(x1,x2,'r+','markersize',6); 
    elseif sign == -1  
        plot(x1,x2,'bo','markersize',6); 
    end 
    hold on;
end;
hold off;
grid on;

2、调用函数,分类线:y=2x+2
X3=generate_signedData(100,[-8,8],[-8,8],2,1,2)
Matlab 中输出:
X3 =

-4.3989 -2.3998 1.0000
-3.4066 6.8398 1.0000
-7.1790 1.4827 1.0000
-5.3936 5.4145 1.0000
-5.3190 0.0352 1.0000
7.9893 -2.3135 -1.0000
-7.2468 -4.5814 1.0000
-1.6346 -2.6613 -1.0000
-4.3264 6.9779 1.0000
2.9310 7.3938 -1.0000
-0.9924 7.0454 1.0000
-7.9067 1.7649 1.0000
4.8172 -4.2723 -1.0000
6.9195 4.2122 -1.0000
5.2232 1.1754 -1.0000
4.6813 -2.7353 -1.0000
-4.4246 -3.0018 1.0000
1.3524 5.2786 1.0000
-3.3526 -1.5591 1.0000
5.7929 1.8358 -1.0000
7.8590 -4.7408 -1.0000
5.2353 2.8138 -1.0000
-4.0168 -0.3874 1.0000
-1.6148 1.5910 1.0000
4.8084 -6.3189 -1.0000
5.1431 5.4574 -1.0000
-2.3279 -1.1189 1.0000
1.1558 3.2132 -1.0000
3.8795 4.1261 -1.0000
-1.7739 -1.1312 1.0000
7.3015 1.1675 -1.0000
5.5956 -3.5785 -1.0000
1.9572 1.4138 -1.0000
7.4155 -6.6256 -1.0000
0.0080 0.3454 -1.0000
-6.5573 6.4747 1.0000
6.1502 -0.9762 -1.0000
4.5076 -5.6246 -1.0000
1.9171 -3.8300 -1.0000
-0.8695 5.5040 1.0000
-4.8607 -3.1384 1.0000
-0.2673 -2.5950 -1.0000
4.7758 7.7998 -1.0000
-5.4552 -4.2099 1.0000
3.2358 -1.9925 -1.0000
7.5793 7.5569 -1.0000
2.2992 5.7616 -1.0000
-1.5699 2.1109 1.0000
7.7638 0.9516 -1.0000
6.9375 3.5255 -1.0000
-0.2554 2.2245 1.0000
6.2022 -4.8202 -1.0000
-1.6741 7.8748 1.0000
-1.5624 2.5417 1.0000
6.4216 7.9261 -1.0000
2.4506 -6.2650 -1.0000
-7.4222 1.8895 1.0000
1.0743 7.3914 1.0000
3.9377 2.6003 -1.0000
0.3730 -3.8417 -1.0000
7.3919 0.6433 -1.0000
-7.5157 3.1410 1.0000
0.3155 -7.0555 -1.0000
6.2406 -2.7168 -1.0000
-4.3248 -6.1768 1.0000
-3.0252 -4.3451 -1.0000
2.4320 -6.9414 -1.0000
-3.5931 -3.4909 1.0000
6.0811 -0.8907 -1.0000
4.0946 1.6527 -1.0000
4.5323 -6.1771 -1.0000
7.6570 5.5775 -1.0000
-7.1897 -0.5408 1.0000
-2.7895 2.0833 1.0000
-4.3152 1.2782 1.0000
1.6505 1.5981 -1.0000
-0.8252 -7.4332 -1.0000
0.2210 -1.4763 -1.0000
-6.2713 -0.6420 1.0000
-0.7859 0.8182 1.0000
4.8865 3.2136 -1.0000
5.9558 -7.1649 -1.0000
-4.4851 -0.6457 1.0000
7.3365 4.6407 -1.0000
-0.7700 -2.6651 -1.0000
-7.0545 3.8545 1.0000
0.1087 -4.8012 -1.0000
-1.1649 -5.3010 -1.0000
4.0271 -2.1064 -1.0000
7.0691 -7.7252 -1.0000
5.2649 2.0255 -1.0000
0.6199 2.4081 -1.0000
3.6261 -6.4882 -1.0000
6.0412 -7.7702 -1.0000
-3.2912 -5.1214 -1.0000
6.8207 -6.9091 -1.0000
1.2975 2.1944 -1.0000
2.4203 5.8340 -1.0000
-7.1048 5.0697 1.0000
0.4628 3.1096 1.0000
绘制出了数据点的图像:
这里写图片描述
3、测试二次函数分类线:y=x^2-4
X3=generate_signedDataForEllipse(100,[-8,8],[-8,8],1,2,-4)
输出结果:
X3 =

-6.1257 -4.1532 -1.0000
2.9585 5.4281 1.0000
7.5223 -4.5573 -1.0000
4.1655 1.3456 -1.0000
-1.5528 0.1606 1.0000
-0.0697 2.4219 1.0000
3.8993 -3.1687 -1.0000
-6.5662 5.2154 -1.0000
-1.7666 4.4049 1.0000
-5.1301 -6.2502 -1.0000
6.4825 6.0216 -1.0000
7.9967 5.8281 -1.0000
-7.4100 0.7149 -1.0000
7.9619 0.1761 -1.0000
5.9762 -6.8765 -1.0000
7.8006 6.7634 -1.0000
1.0282 -1.0966 1.0000
-2.5945 3.5316 1.0000
-7.7815 -2.0150 -1.0000
6.7630 0.7437 -1.0000
-0.4178 -0.0554 1.0000
-3.0565 7.2134 1.0000
7.7119 0.2170 -1.0000
7.8814 -0.7065 -1.0000
-1.1833 -4.5886 -1.0000
-4.9080 5.3241 -1.0000
3.6262 0.4759 -1.0000
5.2653 0.1900 -1.0000
0.8314 -4.5874 -1.0000
1.4051 -5.7158 -1.0000
-7.1644 2.9329 -1.0000
1.7370 -4.4855 -1.0000
-1.4995 2.0785 1.0000
0.8849 -5.9587 -1.0000
-5.2928 -7.9840 -1.0000
-1.3094 -0.1842 1.0000
-5.4421 2.6694 -1.0000
-7.7133 -6.0852 -1.0000
7.2340 7.6137 -1.0000
-7.5054 -0.0979 -1.0000
5.8036 -4.1140 -1.0000
5.3483 5.0175 -1.0000
2.0636 -7.9642 -1.0000
-1.9244 6.4705 1.0000
2.8856 -1.9388 -1.0000
2.1113 -4.1077 -1.0000
1.1426 7.7077 1.0000
5.5949 -3.4649 -1.0000
2.9193 -2.2700 -1.0000
7.7908 -6.6562 -1.0000
-3.9951 4.9815 -1.0000
-6.6492 0.5001 -1.0000
4.8100 3.8212 -1.0000
-5.7334 -0.9937 -1.0000
-2.3939 -0.3440 -1.0000
1.3985 -5.6671 -1.0000
6.4853 2.2431 -1.0000
-5.3930 1.0546 -1.0000
6.9059 4.5296 -1.0000
2.9710 -0.5405 -1.0000
-3.8349 1.1083 -1.0000
-4.0197 -2.8912 -1.0000
6.5728 6.1635 -1.0000
4.7134 6.8130 -1.0000
-5.1386 0.2807 -1.0000
2.0321 6.6109 1.0000
2.6235 -1.7729 -1.0000
3.8401 5.0822 -1.0000
1.6055 -6.6400 -1.0000
6.7577 -7.1424 -1.0000
0.4324 -6.0983 -1.0000
-1.9177 5.0053 1.0000
-4.0945 6.1508 -1.0000
3.4023 -1.9496 -1.0000
-4.0173 -3.9543 -1.0000
4.2759 -7.2022 -1.0000
2.9646 1.9244 -1.0000
3.9470 7.6361 -1.0000
-1.8574 -3.8367 -1.0000
6.0395 4.8975 -1.0000
-0.6221 -6.5446 -1.0000
1.0283 -5.0019 -1.0000
0.5070 -2.3195 1.0000
-2.9635 3.6279 -1.0000
0.2524 4.6503 1.0000
-4.7281 2.8497 -1.0000
-7.1602 4.8188 -1.0000
2.8571 7.1361 1.0000
-6.5351 6.5350 -1.0000
0.1592 1.8385 1.0000
-2.9429 -6.7602 -1.0000
5.6098 -5.6876 -1.0000
-2.0722 1.9583 1.0000
7.9608 0.2775 -1.0000
7.8482 -4.3754 -1.0000
-1.6319 3.1451 1.0000
-6.9657 3.9626 -1.0000
-1.2736 4.9811 1.0000
-1.9263 -2.8949 -1.0000
7.7768 3.4909 -1.0000
显示图片:
这里写图片描述
四、总结与思考:
从实验结果看到,本函数对于二元一次、抛物线形式的分类条件具有较好的效果。

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页