逻辑回归模型matlab代码
时间: 2023-11-25 16:37:01 浏览: 28
Sure! Here's an example of a logistic regression model implemented in MATLAB:
```matlab
% Load the dataset
data = load('data.mat');
X = data.X; % Input features
y = data.y; % Target labels
% Initialize parameters
theta = zeros(size(X, 2), 1); % Coefficients
alpha = 0.01; % Learning rate
num_iters = 1000; % Number of iterations
% Gradient descent
for iter = 1:num_iters
% Compute hypothesis
h = sigmoid(X * theta);
% Compute cost function
J = (-1 / m) * sum(y .* log(h) + (1 - y) .* log(1 - h));
% Update parameters using gradient descent
grad = (1 / m) * X' * (h - y);
theta = theta - alpha * grad;
end
% Sigmoid function
function g = sigmoid(z)
g = 1 ./ (1 + exp(-z));
end
```
This code assumes that you have a dataset stored in a file named `data.mat` with input features `X` and target labels `y`. It initializes the model's parameters, performs gradient descent to minimize the cost function, and updates the parameters iteratively. The `sigmoid` function is used to compute the logistic function.
Please note that this is a basic implementation, and you may need to modify it according to your specific dataset and requirements.