Linear Regression - Practice

 

 Go to: The blog of the depths of the cloud: https://blog.csdn.net/pakko/article/details/37527799

Summarizing the theoretical part of linear regression , let 's practice linear regression with the second-hand housing data of Pudong Tangqiao .

Data and code connection download , tools use Octave .

1. Data acquisition

Crawl from the website to the data and organize it into what we need. The crawling method is inconvenient to talk about, I use jsoup.

2. Data filtering

After climbing to the data, the data of the room area less than 30 square meters and greater than 150 square meters were filtered, and the total price greater than 800w was also filtered. (these data are too small or too large)

3. Univariate Linear Regression

x represents the area of ​​the house and y represents the house price, which is calculated using the method of the normal equation system.

After getting the code, execute one.

The execution result is as follows:

 

4. Multiple Linear Regression

 

x represents the area of ​​the house, the number of rooms, and the floor, and y represents the house price, which is calculated using the method of the normal equation system.

 

After you get the code, you can execute multi.

The execution result is as follows:

 

 

% Load the data from our text file
data = load('house.txt');

% Define x and y
x = data(:,1:3);
y = data(:,4);

% Create a function to plot the data
function plotData(x,y)
plot(x,y,'rx','MarkerSize',8); % Plot the data
end
% Plot the data
plotData(x,y);
xlabel('Square Feet'); % Set the x-axis label
ylabel('Price'); % Set the y-axis label
fprintf('Program paused. Press enter to continue.\n');
pause;

% Count how many data points we have
m = length(x);
% Add a column of all ones (intercept term) to x
X = [ones(m, 1) x];

% Calculate theta
theta = (pinv(X'*X))*X'*y

% Plot the fitted equation we got from the regression
hold on; % this keeps our previous plot of the training data visible
plot(X(:,2), X*theta, '-')
legend('Training data', 'Linear regression')
hold off % Don't put any more plots on this figure

price = [1, 80, 2, 0.5] * theta;

fprintf(['Predicted price of a 80 sq-ft, 2 rooms, middle floor' ...
'(using normal equations):\n %f\n'], price);

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325950388&siteId=291194637