Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Gebaseerd op: Airbnb : price prediction using XGBoost ๐Ÿ”ฅ

Probleem definitieยถ

Airbnb gebruikt machine learning om optimale prijsvoorstellen te doen aan hosts. Een goed gekalibreerd prijsmodel helpt hosts hun omzet te maximaliseren en tegelijkertijd concurrerend te blijven.

Taak, Ervaringยถ

De bedoeling is om numerieke waarden te voorspellen (prijs). Dit gaat over een regressietaak. We willen trainen aan de hand van effectieve prijzen. We hebben dus te maken met gesuperviseerd leren.

Data collectionยถ

We gebruiken een dataset van Airbnb die beschikbaar is op Kaggle met prijzen uit verschillende grote Amerikaanse steden. We richten ons op volgende variabelen:

  • Numeriek: bedrooms, bathrooms, review scores, etc.

  • Categorisch: property type, room type, city

  • Text: amenities

  • Geografisch: latitude, longitude, neighborhood

  • Target variable: log_price

Downloading from https://www.kaggle.com/api/v1/datasets/download/stevezhenghp/airbnb-price-prediction?dataset_version_number=1...
100%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 31.3M/31.3M [00:02<00:00, 13.1MB/s]
Extracting files...

Loading...

Data explorationยถ

Dataset Information:
==================================================
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 74111 entries, 0 to 74110
Data columns (total 29 columns):
 #   Column                  Non-Null Count  Dtype  
---  ------                  --------------  -----  
 0   id                      74111 non-null  int64  
 1   log_price               74111 non-null  float64
 2   property_type           74111 non-null  object 
 3   room_type               74111 non-null  object 
 4   amenities               74111 non-null  object 
 5   accommodates            74111 non-null  int64  
 6   bathrooms               73911 non-null  float64
 7   bed_type                74111 non-null  object 
 8   cancellation_policy     74111 non-null  object 
 9   cleaning_fee            74111 non-null  bool   
 10  city                    74111 non-null  object 
 11  description             74111 non-null  object 
 12  first_review            58247 non-null  object 
 13  host_has_profile_pic    73923 non-null  object 
 14  host_identity_verified  73923 non-null  object 
 15  host_response_rate      55812 non-null  object 
 16  host_since              73923 non-null  object 
 17  instant_bookable        74111 non-null  object 
 18  last_review             58284 non-null  object 
 19  latitude                74111 non-null  float64
 20  longitude               74111 non-null  float64
 21  name                    74111 non-null  object 
 22  neighbourhood           67239 non-null  object 
 23  number_of_reviews       74111 non-null  int64  
 24  review_scores_rating    57389 non-null  float64
 25  thumbnail_url           65895 non-null  object 
 26  zipcode                 73145 non-null  object 
 27  bedrooms                74020 non-null  float64
 28  beds                    73980 non-null  float64
dtypes: bool(1), float64(7), int64(3), object(18)
memory usage: 15.9+ MB

Target Variable Statistics:
==================================================
count    74111.000000
mean         4.782069
std          0.717394
min          0.000000
25%          4.317488
50%          4.709530
75%          5.220356
max          7.600402
Name: log_price, dtype: float64

Missing Values:
==================================================
host_response_rate        18299
review_scores_rating      16722
first_review              15864
last_review               15827
thumbnail_url              8216
neighbourhood              6872
zipcode                     966
bathrooms                   200
host_has_profile_pic        188
host_identity_verified      188
host_since                  188
beds                        131
bedrooms                     91
dtype: int64

Verdeling van de target variabele log_priceยถ

De log-transformatie zorgt voor een meer symmetrische verdeling. Bij lagere prijzen zijn verschillen van een bepaalde grootte belangrijker dan in de hogere regionen. De log-transformatie vertaalt dit naar de numerieke schaal van de target variabele.

Loading...
Actual Price Statistics:
Mean: $160.37
Median: $111.00
Min: $1.00
Max: $1999.00
Loading...

Categorische featuresยถ

Loading...
Loading...
Loading...
Loading...

Geografische analyseยถ

Loading...
Loading...
Loading...
Loading...

Numerieke features: Correlatie analyseยถ

Loading...
Loading...

Data preparationยถ

Missing valuesยถ

Bathrooms missing: 200
Bathrooms distribution:
bathrooms
1.0    58099
2.0     7936
1.5     3801
2.5     1567
3.0     1066
3.5      429
4.0      286
0.5      209
0.0      198
4.5      116
5.0       72
8.0       41
5.5       39
6.0       24
6.5       12
7.0       10
7.5        6
Name: count, dtype: int64
After filling: 0 missing

Review scores missing: 16722
Review scores distribution:
review_scores_rating
100.0    16215
98.0      4374
97.0      4087
96.0      4081
95.0      3713
Name: count, dtype: int64
After filling: 0 missing

Bedrooms missing: 91
After filling: 0 missing

Beds missing: 131
After filling: 0 missing

Host response rate missing: 18299
After filling: 0 missing

Host has profile pic missing: 188
After filling: 0 missing

Host identity verified missing: 188
After filling: 0 missing

Final missing values check:
first_review           15864
last_review            15827
thumbnail_url           8216
neighbourhood           6872
zipcode                  966
host_since               188
accommodates               0
bathrooms                  0
cancellation_policy        0
id                         0
dtype: int64

Data typesยถ

Converting cleaning_fee:
Before: [ True False]
After: [1 0]
Data type: int64

Converting instant_bookable:
Before: ['f' 't']
After: [0 1]
Data type: int64

Converting host_has_profile_pic:
Before: ['t' 'f']
After: [1 0]
Data type: int64

Converting host_identity_verified:
Before: ['t' 'f']
After: [1 0]
Data type: int64

Review scores range before: 0.0 - 100.0
Review scores range after: 0.0 - 1.0
Max reviews: 605
Normalized reviews range: 0.0 - 1.0

Feature engineeringยถ

Amenitiesยถ

Deze variabele bestaat uit een reeks van voorzieningen en is in die vorm niet bruikbaar. We zetten de meest frequente om naar binaire features (aan-/afwezig)

array(['{"Wireless Internet","Air conditioning",Kitchen,Heating,"Family/kid friendly",Essentials,"Hair dryer",Iron,"translation missing: en.hosting_amenity_50"}', '{"Wireless Internet","Air conditioning",Kitchen,Heating,"Family/kid friendly",Washer,Dryer,"Smoke detector","Fire extinguisher",Essentials,Shampoo,Hangers,"Hair dryer",Iron,"translation missing: en.hosting_amenity_50"}', '{TV,"Cable TV","Wireless Internet","Air conditioning",Kitchen,Breakfast,"Buzzer/wireless intercom",Heating,"Family/kid friendly","Smoke detector","Carbon monoxide detector","Fire extinguisher",Essentials,Shampoo,Hangers,"Hair dryer",Iron,"Laptop friendly workspace","translation missing: en.hosting_amenity_50"}'], dtype=object)
Total unique amenities found: 128

Top 20 most common amenities:
- Wireless Internet: 71265 listings (96.2%)
- Kitchen: 67526 listings (91.1%)
- Heating: 67073 listings (90.5%)
- Essentials: 64005 listings (86.4%)
- Smoke detector: 61727 listings (83.3%)
- Air conditioning: 55210 listings (74.5%)
- TV: 52458 listings (70.8%)
- Shampoo: 49465 listings (66.7%)
- Hangers: 49173 listings (66.4%)
- Carbon monoxide detector: 47190 listings (63.7%)
- Internet: 44648 listings (60.2%)
- Laptop friendly workspace: 43703 listings (59.0%)
- Hair dryer: 43330 listings (58.5%)
- Washer: 43169 listings (58.2%)
- Dryer: 42711 listings (57.6%)
- Iron: 41687 listings (56.2%)
- Family/kid friendly: 37026 listings (50.0%)
- Fire extinguisher: 30724 listings (41.5%)
- First aid kit: 27532 listings (37.1%)
- Cable TV: 24253 listings (32.7%)
Added 15 amenity features
New columns: ['amenity_wireless_internet', 'amenity_kitchen', 'amenity_heating', 'amenity_essentials', 'amenity_smoke_detector', 'amenity_air_conditioning', 'amenity_tv', 'amenity_shampoo', 'amenity_hangers', 'amenity_carbon_monoxide_detector']

Categorische encoderingยถ

Property type distribution:
property_type
Apartment             49003
House                 16511
Condominium            2658
Townhouse              1692
Loft                   1244
Other                   607
Guesthouse              498
Bed & Breakfast         462
Bungalow                366
Villa                   179
Dorm                    142
Guest suite             123
Camper/RV                94
Timeshare                77
Cabin                    72
In-law                   71
Hostel                   70
Boutique hotel           69
Boat                     65
Serviced apartment       21
Tent                     18
Castle                   13
Vacation home            11
Yurt                      9
Hut                       8
Treehouse                 7
Chalet                    6
Earth House               4
Tipi                      3
Cave                      2
Train                     2
Casa particular           1
Parking Space             1
Lighthouse                1
Island                    1
Name: count, dtype: int64
Bed type distribution:
bed_type
Real Bed         72028
Futon              753
Pull-out Sofa      585
Airbed             477
Couch              268
Name: count, dtype: int64

Converted bed_type to ordinal scale (1-2):
bed_type
2    72028
1     2083
Name: count, dtype: int64
Loading...

Neighborbood pricingยถ

Nieuwe feature met ordinale buurt-specifieke prijs-niveaus

Top 10 most expensive neighborhoods:
neighbourhood
Wilmington           7.170120
Harvard Square       5.480639
Coolidge Corner      5.416100
Government Center    5.357209
Lighthouse HIll      5.298317
Watertown            5.293305
Fort Wadsworth       5.101796
Printers Row         5.075174
Bethesda, MD         5.043425
Mission Bay          5.008402
Name: price_per_bedroom, dtype: float64

Top 10 least expensive neighborhoods:
neighbourhood
Mt Rainier/Brentwood, MD    0.945725
Mill Basin                  1.035768
Rossville                   1.100252
West Athens                 1.288258
Castleton Corners           1.398678
Chevy Chase, MD             1.426180
Emerson Hill                1.534508
Observatory Circle          1.678846
Gateway                     1.684834
Galewood                    1.705165
Name: price_per_bedroom, dtype: float64
Neighborhood price level distribution:
neighborhood_price_level
3    39928
1    18978
2    15205
Name: count, dtype: int64

Finale dataยถ

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 74111 entries, 0 to 74110
Data columns (total 53 columns):
 #   Column                               Non-Null Count  Dtype  
---  ------                               --------------  -----  
 0   log_price                            74111 non-null  float64
 1   accommodates                         74111 non-null  int64  
 2   bathrooms                            74111 non-null  float64
 3   bed_type                             74111 non-null  int64  
 4   city                                 74111 non-null  object 
 5   cleaning_fee                         74111 non-null  int64  
 6   host_has_profile_pic                 74111 non-null  int64  
 7   host_identity_verified               74111 non-null  int64  
 8   host_response_rate                   74111 non-null  float64
 9   instant_bookable                     74111 non-null  int64  
 10  number_of_reviews                    74111 non-null  float64
 11  review_scores_rating                 74111 non-null  float64
 12  bedrooms                             74111 non-null  float64
 13  beds                                 74111 non-null  float64
 14  amenity_wireless_internet            74111 non-null  int64  
 15  amenity_kitchen                      74111 non-null  int64  
 16  amenity_heating                      74111 non-null  int64  
 17  amenity_essentials                   74111 non-null  int64  
 18  amenity_smoke_detector               74111 non-null  int64  
 19  amenity_air_conditioning             74111 non-null  int64  
 20  amenity_tv                           74111 non-null  int64  
 21  amenity_shampoo                      74111 non-null  int64  
 22  amenity_hangers                      74111 non-null  int64  
 23  amenity_carbon_monoxide_detector     74111 non-null  int64  
 24  amenity_internet                     74111 non-null  int64  
 25  amenity_laptop_friendly_workspace    74111 non-null  int64  
 26  amenity_washer                       74111 non-null  int64  
 27  amenity_hair_dryer                   74111 non-null  int64  
 28  amenity_dryer                        74111 non-null  int64  
 29  neighborhood_price_level             74111 non-null  int64  
 30  cancellation_policy_flexible         74111 non-null  float64
 31  cancellation_policy_moderate         74111 non-null  float64
 32  cancellation_policy_strict           74111 non-null  float64
 33  cancellation_policy_super_strict_30  74111 non-null  float64
 34  cancellation_policy_super_strict_60  74111 non-null  float64
 35  city_Boston                          74111 non-null  float64
 36  city_Chicago                         74111 non-null  float64
 37  city_DC                              74111 non-null  float64
 38  city_LA                              74111 non-null  float64
 39  city_NYC                             74111 non-null  float64
 40  city_SF                              74111 non-null  float64
 41  property_type_Apartment              74111 non-null  float64
 42  property_type_Bed & Breakfast        74111 non-null  float64
 43  property_type_Bungalow               74111 non-null  float64
 44  property_type_Condominium            74111 non-null  float64
 45  property_type_Guesthouse             74111 non-null  float64
 46  property_type_House                  74111 non-null  float64
 47  property_type_Loft                   74111 non-null  float64
 48  property_type_Other                  74111 non-null  float64
 49  property_type_Townhouse              74111 non-null  float64
 50  room_type_Entire home/apt            74111 non-null  float64
 51  room_type_Private room               74111 non-null  float64
 52  room_type_Shared room                74111 non-null  float64
dtypes: float64(30), int64(22), object(1)
memory usage: 30.0+ MB
Training set shape: (59288, 53)
Test set shape: (14823, 53)

Split verification: 74111 total samples
Training features shape: (59288, 51)
Training target shape: (59288,)
Test features shape: (14823, 51)
Test target shape: (14823,)

Model definitionยถ

Lineaire regressieยถ

Linear Regression Cross-Validation Rยฒ Scores: [0.59495647 0.60093226 0.59434806 0.5850233  0.60406696]
Mean Rยฒ: 0.5959 (+/- 0.0131)

XGBoost Regressionยถ

XGBoost Cross-Validation Rยฒ Scores: [0.71489725 0.7151093  0.71387543 0.70976343 0.71964383]
Mean Rยฒ: 0.7147 (+/- 0.0063)

Final Model Comparison:
Linear Regression: 0.5959
XGBoost:          0.7147
Loading...

Scoringยถ

Validation Set Performance:
MAE:  0.2808
MSE:  0.1494
RMSE: 0.3866
Rยฒ:   0.7144

In actual dollars:
Mean actual price: $160.56
MAE in dollars: $53.48
This means our model is off by about $53.48 on average
Loading...

Business Insights from Feature Importance:
49. room_type_Entire home/apt: 0.5896
28. neighborhood_price_level: 0.1311
2. bathrooms: 0.0718
11. bedrooms: 0.0272
37. city_LA: 0.0188
39. city_SF: 0.0147
1. accommodates: 0.0107
35. city_Chicago: 0.0097
36. city_DC: 0.0092
19. amenity_tv: 0.0079
Loading...
Loading...