美文网首页机器学习算法工程师
多变量线性回归~疑问版

多变量线性回归~疑问版

作者: 由简单到简单 | 来源:发表于2017-04-05 18:04 被阅读40次

%% ================ Part 1: Feature Normalization ================

%% Clear and Close Figures

clear ; close all; clc

fprintf('Loading data ...\n');

data = load('ex1data2.txt');

X = data(:, 1:2);

y = data(:, 3);

m = length(y);

plot(X, y, 'rx', 'MarkerSize', 10);

pause;

[X, mu, sigma] = featureNormalize(X);

% Add intercept term to X

X = [ones(m, 1) X];

%% ================ Part 2: Gradient Descent ================

fprintf('Running gradient descent ...\n');

% Choose some alpha value

alpha = 0.01;

num_iters = 400;

% Init Theta and Run Gradient Descent

theta = zeros(3, 1);

[theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters);

% Plot the convergence graph

figure;

plot(1:numel(J_history), J_history, '-b', 'LineWidth', 2);

xlabel('Number of iterations');

ylabel('Cost J');

% Display gradient descent's result

fprintf('Theta computed from gradient descent: \n');

fprintf(' %f \n', theta);

fprintf('\n');

% Estimate the price of a 1650 sq-ft, 3 br house

% ====================== YOUR CODE HERE ======================

% Recall that the first column of X is all-ones. Thus, it does

% not need to be normalized.

te = [1650 3];

te = te - mu;

te = te ./ sigma;

price = [1 te]*theta;% You should change this

这里我算出来是0?!!!

% ============================================================

fprintf(['Predicted price of a 1650 sq-ft, 3 br house ' ... '(using gradient descent):\n $%f\n'], price);

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

pause;

%% ================ Part 3: Normal Equations ================

fprintf('Solving with normal equations...\n');

% ====================== YOUR CODE HERE ======================

% Instructions: The following code computes the closed form

%               solution for linear regression using the normal

%               equations. You should complete the code in

%               normalEqn.m

%

%               After doing so, you should complete this code

%               to predict the price of a 1650 sq-ft, 3 br house.

%

%% Load Data

data = csvread('ex1data2.txt');

X = data(:, 1:2);

y = data(:, 3);

m = length(y);

% Add intercept term to X

X = [ones(m, 1) X];

% Calculate the parameters from the normal equation

theta = normalEqn(X, y);

% Display normal equation's result

fprintf('Theta computed from the normal equations: \n');

fprintf(' %f \n', theta);

fprintf('\n');

% Estimate the price of a 1650 sq-ft, 3 br house

% ====================== YOUR CODE HERE ======================

price = 0; % You should change this

% ============================================================

fprintf(['Predicted price of a 1650 sq-ft, 3 br house ' ...

'(using normal equations):\n $%f\n'], price);

正规化方法?(上图)

相关文章

  • 多变量线性回归~疑问版

    %% ================ Part 1: Feature Normalization =======...

  • [线性回归] 多特征线性回归

    1 多特征线性回归 有多个变量的线性回归也叫做多变量线形回归(multivariate linear regres...

  • 基于pytorch的linear Regression

    线性回归模型 线性回归是分析一个变量与另外一(多)个变量之间关系的方法。因变量是 y,自变量是 x,关系线性:任务...

  • 线性回归

    单变量线性回归 多变量线性回归 局限性 梯度下降法 优点 缺点 单变量线性回归 模型线性回归假设数据集中每个yi和...

  • (16)多重线性回归分析

    一、多重线性回归分析简介 简单线性回归分析:自变量X =1 个 多重线性回归分析:自变量X >=2 个 多元线性回...

  • 10. 线性回归

    回归算法-线性回归分析 线性回归定义:线性回归通过一个或多个自变量与因变量之间进行建模的回归分析,其中可以为一个或...

  • logistics回归分类

    logistics回归分类模型和线性模型的关系非常密切;区分下线性回归模型和线性模型;线性模型:自变量和因变量之间...

  • 吴恩达机器学习(第一周)

    1.单变量线性回归(Linear Regression with One Variable) 1.1线性回归算法 ...

  • DataWhale-01-线性回归

    线性回归的概念 1、线性回归的原理 线性回归的一般形式: 有数据集,其中,其中n表示变量的数量,d表示每个变量的维...

  • 2020-03-27

    线性回归:找寻变量之间的关系 逻辑回归

网友评论

    本文标题:多变量线性回归~疑问版

    本文链接:https://www.haomeiwen.com/subject/ptjxattx.html