Linear regression as a basic model

Case 1. Apartment valuation based on parameters


Implementation with RubixML

Now we will do the same thing, but the way it is usually done in real projects. We will use linear regression trained with the least squares method. The library itself will solve the optimization problem and find the weights analytically.

 
<?php

use Rubix\ML\Datasets\Labeled;
use 
Rubix\ML\Datasets\Unlabeled;
use 
Rubix\ML\Regressors\Ridge;

// Data: [area, floor, distance to city center, building age]
$samples = [
    [
503510],
    [
701035],
    [
402830],
];

$targets = [
    
66_000,
    
95_000,
    
45_000,
];

// Create labeled dataset
$dataset = new Labeled($samples$targets);

// Create linear regression model (Ridge)
$regression = new Ridge(1.0);

// Train the model
$regression->train($dataset);

// Make a prediction for a new apartment
// [square footage, number of bedrooms, number of bathrooms, number of floors]
$newApartment = [605412];

// Ridge::predict expects a Dataset and returns an array of predictions
$dataset = new Unlabeled([$newApartment]);
$predictions $regression->predict($dataset);
$predictedPrice $predictions[0];

echo 
'Apartment valuation: $' number_format($predictedPrice) . "\n";

Charts:

Chart Type:

Result: Memory: 1.067 Mb Time running: 0.014 sec.
Apartment valuation: $78,016

The block above shows the result of the script: the predicted apartment price for the following features:

  • Apartment area: 60 m²
  • Number of rooms: 5
  • Number of bathrooms: 4
  • Number of floors: 12