利用逻辑回归分类模型,针对鸢尾花属性中的花瓣长和花瓣宽,实现鸢尾花数据集中山鸢尾和变色鸢尾的分类,使用matlab进行编程
时间: 2024-06-01 18:13:00 浏览: 69
以下是利用逻辑回归分类模型,针对鸢尾花属性中的花瓣长和花瓣宽,实现鸢尾花数据集中山鸢尾和变色鸢尾的分类的matlab代码:
% 导入数据集
data = load('iris_dataset.mat');
X = data.meas(51:150,3:4); % 取花瓣长和花瓣宽这两列属性
y = data.species(51:150); % 取山鸢尾和变色鸢尾这两个类别
% 将标签转化为数值
y(y == 'versicolor') = 0;
y(y == 'virginica') = 1;
y = str2double(y);
% 划分训练集和测试集
cv = cvpartition(size(X,1),'HoldOut',0.3);
X_train = X(cv.training,:);
y_train = y(cv.training,:);
X_test = X(cv.test,:);
y_test = y(cv.test,:);
% 标准化数据
[X_train, mu, sigma] = zscore(X_train);
X_test = (X_test - mu) ./ sigma;
% 添加偏置项
X_train = [ones(size(X_train,1),1) X_train];
X_test = [ones(size(X_test,1),1) X_test];
% 初始化参数
theta = zeros(size(X_train,2),1);
% 训练模型
alpha = 0.01; % 学习率
num_iters = 1000; % 迭代次数
[theta, J_history] = gradientDescent(X_train, y_train, theta, alpha, num_iters);
% 绘制代价函数随迭代次数的变化图
figure;
plot(1:num_iters, J_history, '-b', 'LineWidth', 2);
xlabel('Number of iterations');
ylabel('Cost J');
% 预测
y_pred = predict(theta, X_test);
% 计算准确率
accuracy = mean(double(y_pred == y_test)) * 100;
fprintf('Accuracy: %f%%\n', accuracy);
% 定义代价函数
function [J, grad] = costFunction(theta, X, y)
m = length(y);
J = 0;
grad = zeros(size(theta));
h = sigmoid(X * theta);
J = (-1 / m) * sum(y .* log(h) + (1 - y) .* log(1 - h));
grad = (1 / m) * X' * (h - y);
end
% 定义sigmoid函数
function g = sigmoid(z)
g = 1 ./ (1 + exp(-z));
end
% 定义梯度下降函数
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
m = length(y);
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
[J, grad] = costFunction(theta, X, y);
theta = theta - alpha * grad;
J_history(iter) = J;
end
end
% 定义预测函数
function p = predict(theta, X)
p = sigmoid(X * theta) >= 0.5;
end
阅读全文