给定二分类问题的训练集,格式为excel,数据为2列,40行,用matlab采用Logistic回归算法进行分类学习,画出散点图(正例和负例分别用不同点型表示)训练集数据如下: 0 2.702322665 0.128205128 -3.589058608 0.256410256 -4.550460521 0.384615385 7.304893959 0.512820513 23.57088367 0.641025641 -9.839502823 0.769230769 0.851106844 0.897435897 -13.08450254 1.025641026 9.121431921 1.153846154 -8.499686643 1.282051282 -18.71591223 1.41025641 -14.55395357 1.538461538 -24.18646052 1.666666667 -16.33541495 1.794871795 -32.37571767 1.923076923 -44.61882082 2.051282051 -33.53417305 2.179487179 -22.62830333 2.307692308 -52.02766457 2.435897436 -43.82721886 2.564102564 -32.15413243 2.692307692 -61.59543192 2.820512821 -35.66137019 2.948717949 -33.17455469 3.076923077 -36.56111219 3.205128205 -43.53032165 3.333333333 -32.10838356 3.461538462 -26.32803462 3.58974359 -12.0808398 3.717948718 -7.858996341 3.846153846 -9.881480058 3.974358974 19.17370654 4.102564103 13.12296761 4.230769231 17.5261927 4.358974359 34.71954482 4.487179487 62.63235831 4.615384615 72.49608288 4.743589744 97.54857872 4.871794872 109.149795 5 154.8682132
时间: 2023-08-09 16:04:19 浏览: 147
Logistic回归统计算法的matlab实现.rar_Java__Java_
好的,根据您提供的数据,我可以为您提供代码实现。
1. 读取数据
```matlab
data = xlsread('data.xlsx');
X = data(:,1);
Y = data(:,2);
```
2. Logistic回归算法的分类学习
```matlab
m = length(Y);
X = [ones(m,1) X];
initial_theta = zeros(size(X,2),1);
options = optimset('GradObj', 'on', 'MaxIter', 400);
[theta, cost] = fminunc(@(t)(costFunction(t, X, Y)), initial_theta, options);
```
其中,costFunction是自己编写的cost函数,用于计算代价函数J和梯度。
3. 画出散点图
```matlab
positive = find(Y == 1);
negative = find(Y == 0);
scatter(X(positive,2), X(positive,3), 'k+', 'LineWidth', 2, 'MarkerSize', 7);
hold on;
scatter(X(negative,2), X(negative,3), 'ko', 'MarkerFaceColor', 'y', 'MarkerSize', 7);
hold on;
```
其中,positive和negative分别是正例和负例的索引。
完整代码如下:
```matlab
data = xlsread('data.xlsx');
X = data(:,1);
Y = data(:,2);
m = length(Y);
X = [ones(m,1) X];
initial_theta = zeros(size(X,2),1);
options = optimset('GradObj', 'on', 'MaxIter', 400);
[theta, cost] = fminunc(@(t)(costFunction(t, X, Y)), initial_theta, options);
positive = find(Y == 1);
negative = find(Y == 0);
scatter(X(positive,2), X(positive,3), 'k+', 'LineWidth', 2, 'MarkerSize', 7);
hold on;
scatter(X(negative,2), X(negative,3), 'ko', 'MarkerFaceColor', 'y', 'MarkerSize', 7);
hold on;
xlabel('Feature 1');
ylabel('Feature 2');
legend('Positive', 'Negative');
```
需要注意的是,该代码中的costFunction函数需要自己编写,它的作用是计算代价函数J和梯度。另外,散点图的x轴和y轴标签需要根据实际数据进行修改。
阅读全文