matlab多分类,机器学习-一对多(多分类)代码实现(matlab)

%% Machine Learning Online Class - Exercise 3 | Part 1: One-vs-all

% Instructions

% ------------

%

% This file contains code that helps you get started on the

% linear exercise. You will need to complete the following functions

% in this exericse:

%

% lrCostFunction.m (logistic regression cost function)

% oneVsAll.m

% predictOneVsAll.m

% predict.m

%

% For this exercise, you will not need to change any code in this file,

% or any other files other than those mentioned above.

%

%% Initialization

clear ; close all; clc

%% Setup the parameters you will use for this part of the exercise

input_layer_size = 400; % 20x20 Input Images of Digits

num_labels = 10; % 10 labels, from 1 to 10

% (note that we have mapped "0" to label 10)

%% =========== Part 1: Loading and Visualizing Data =============

% We start the exercise by first loading and visualizing the dataset.

% You will be working with a dataset that contains handwritten digits.

%

% Load Training Data

fprintf(‘Loading and Visualizing Data ...\n‘)

load(‘ex3data1.mat‘); % training data stored in arrays X, y

m = size(X, 1);

2b65ef29a5872cc0e4771c25889edd04.gif

6a087676c59fa8b19d76e6bb55a32902.gif

size(X, 1);

X=5000*400size(X,1) = 5000取行

size(X,2) = 400取列

解释

% Randomly select 100 data points to display

rand_indices = randperm(m);

sel = X(rand_indices(1:100), :);

displayData(sel);

fprintf(‘Program paused. Press enter to continue.\n‘);

pause;

%% ============ Part 2: Vectorize Logistic Regression ============

% In this part of the exercise, you will reuse your logistic regression

% code from the last exercise. You task here is to make sure that your

% regularized logistic regression implementation is vectorized. After

% that, you will implement one-vs-all classification for the handwritten

% digit dataset.

%

fprintf(‘\nTraining One-vs-All Logistic Regression...\n‘)

lambda = 0.1;

[all_theta] = oneVsAll(X, y, num_labels, lambda);

fprintf(‘Program paused. Press enter to continue.\n‘);

pause;

%% ================ Part 3: Predict for One-Vs-All ================

% After ...

pred = predictOneVsAll(all_theta, X);

fprintf(‘\nTraining Set Accuracy: %f\n‘, mean(double(pred == y)) * 100);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值