Practical Exam: House sales
RealAgents is a real estate company that focuses on selling houses.
RealAgents sells a variety of types of house in one metropolitan area.
Some houses sell slowly and sometimes require lowering the price in order to find a buyer.
In order to stay competitive, RealAgents would like to optimize the listing prices of the houses it is trying to sell.
They want to do this by predicting the sale price of a house given its characteristics.
If they can predict the sale price in advance, they can decrease the time to sale.
Data
The dataset contains records of previous houses sold in the area.
| Column Name | Criteria |
|---|---|
| house_id | Nominal. Unique identifier for houses. Missing values not possible. |
| city | Nominal. The city in which the house is located. One of 'Silvertown', 'Riverford', 'Teasdale' and 'Poppleton'. Replace missing values with "Unknown". |
| sale_price | Discrete. The sale price of the house in whole dollars. Values can be any positive number greater than or equal to zero. Remove missing entries. |
| sale_date | Discrete. The date of the last sale of the house. Replace missing values with 2023-01-01. |
| months_listed | Continuous. The number of months the house was listed on the market prior to its last sale, rounded to one decimal place. Replace missing values with mean number of months listed, to one decimal place. |
| bedrooms | Discrete. The number of bedrooms in the house. Any positive values greater than or equal to zero. Replace missing values with the mean number of bedrooms, rounded to the nearest integer. |
| house_type | Ordinal. One of "Terraced" (two shared walls), "Semi-detached" (one shared wall), or "Detached" (no shared walls). Replace missing values with the most common house type. |
| area | Continuous. The area of the house in square meters, rounded to one decimal place. Replace missing values with the mean, to one decimal place. |
Task 1
The team at RealAgents knows that the city that a property is located in makes a difference to the sale price.
Unfortuntately they believe that this isn't always recorded in the data.
Calculate the number of missing values of the city.
-
You should use the data in the file "house_sales.csv".
-
Your output should be an object
missing_city, that contains the number of missing values in this column.
# Use this cell to write your code for Task 1
import pandas as pd
import numpy as np
df = pd.read_csv("house_sales.csv")
print(df.head())
##### Data inspection #####
print("\n"* 2,' BREAK '.center(80, '#') ,"\n" * 2)
print(df.info())
print("\n"* 2,' BREAK '.center(80, '#') ,"\n" * 2)
print(df.isna().sum())
print("\n"* 2,' BREAK '.center(80, '#') ,"\n" * 2)
print(df["city"].value_counts(dropna=False))
missing_city = 73
print(missing_city)
Task 2
Before you fit any models, you will need to make sure the data is clean.
The table below shows what the data should look like.
Create a cleaned version of the dataframe.
-
You should start with the data in the file "house_sales.csv".
-
Your output should be a dataframe named
clean_data. -
All column names and values should match the table below.
| Column Name | Criteria |
|---|---|
| house_id | Nominal. Unique identifier for houses. Missing values not possible. |
| city | Nominal. The city in which the house is located. One of 'Silvertown', 'Riverford', 'Teasdale' and 'Poppleton' Replace missing values with "Unknown". |
| sale_price | Discrete. The sale price of the house in whole dollars. Values can be any positive number greater than or equal to zero. Remove missing entries. |
| sale_date | Discrete. The date of the last sale of the house. Replace missing values with 2023-01-01. |
| months_listed | Continuous. The number of months the house was listed on the market prior to its last sale, rounded to one decimal place. Replace missing values with mean number of months listed, to one decimal place. |
| bedrooms | Discrete. The number of bedrooms in the house. Any positive values greater than or equal to zero. Replace missing values with the mean number of bedrooms, rounded to the nearest integer. |
| house_type | Ordinal. One of "Terraced", "Semi-detached", or "Detached". Replace missing values with the most common house type. |
| area | Continuous. The area of the house in square meters, rounded to one decimal place. Replace missing values with the mean, to one decimal place. |
# Use this cell to write your code for Task 2
print(df.info())
### replacing missing values in "city" and converting the column to categories
work_df = df.copy()
work_df.loc[work_df["city"] == "--", "city"] = "Unknown"
work_df["city"] = work_df["city"].astype("category")
work_df["house_id"] = work_df["house_id"].astype("category")
### Checking the date column, converting all entries to a datetime object worked without a problem
work_df["sale_date"] = pd.to_datetime(work_df["sale_date"],errors="coerce")
### inspectin Sale price
print(work_df["sale_price"].describe())
### Imputing missing values, I could have used fillna() but I wanted more controll this time
mean_months_listed = work_df["months_listed"].mean().round(1)
work_df.loc[work_df["months_listed"].isna(), "months_listed"] = mean_months_listed
work_df["months_listed"] = work_df["months_listed"].round(1)
### Checking the house type unique values and changing them to the corresponding category
house_types = work_df["house_type"].unique()
print(house_types)
print(work_df["house_type"].value_counts())
work_df["house_type"].replace("Det.", "Detached", inplace=True)
work_df["house_type"].replace("Semi", "Semi-detached", inplace=True)
work_df["house_type"].replace("Terr.", "Terraced", inplace=True)
df['house_type'] = pd.Categorical(
df['house_type'],
categories=['Terraced', 'Semi-detached', 'Detached'],
ordered=True
)
work_df["house_type"] = work_df["house_type"].astype("category")
print(work_df["house_type"].value_counts())
### Last column, from the info() I could already see that something is wrong with this continous variable. I have to get rid of the unit
work_df["area"] = work_df["area"].replace(r'\D',"",regex=True).astype(float).round(1)
print(work_df["area"].value_counts())
# In the end, I assigned the adapted dataframe to the desired variable clean_data
clean_data = work_df
clean_data.info()Task 3
The team at RealAgents have told you that they have always believed that the number of bedrooms is the biggest driver of house price.
Producing a table showing the difference in the average sale price by number of bedrooms along with the variance to investigate this question for the team.
-
You should start with the data in the file 'house_sales.csv'.
-
Your output should be a data frame named
price_by_rooms. -
It should include the three columns
bedrooms,avg_price,var_price. -
Your answers should be rounded to 1 decimal place.
# Use this cell to write your code for Task 3
price_by_rooms = work_df.groupby('bedrooms')['sale_price'].agg(
avg_price='mean',
var_price='var'
).reset_index()
price_by_rooms = price_by_rooms.round({'avg_price': 1, 'var_price': 1})
print(price_by_rooms)Task 4
Fit a baseline model to predict the sale price of a house.
-
Fit your model using the data contained in “train.csv”
-
Use “validation.csv” to predict new values based on your model. You must return a dataframe named
base_result, that includeshouse_idandprice. The price column must be your predicted values.
# Use this cell to write your code for Task 4
#I decided to use a DecisionTreeRegressor for my first model and see to where I could get. Depending on the results, I would have chosen a model for the comparison. Since the RMSE of the first model was roughly 50% of the maximum threshold, I decided to take a simple LinearRegression model for my second model.
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score, KFold
from sklearn.metrics import mean_squared_error as MSE
train_data = pd.read_csv("train.csv")
validaton = pd.read_csv("validation.csv")
print(validaton.head())
# I decided to drop the sale_date column and encoded the categorical data
X1 = train_data[["city","months_listed","bedrooms","house_type","area"]]
X1_encoded = pd.get_dummies(X1, columns=["city","house_type"], drop_first=True)
y1 = train_data["sale_price"]
X_train, X_test, y_train, y_test = train_test_split(X1_encoded,y1,
test_size=0.3,
random_state=42)
dt = DecisionTreeRegressor(max_depth=8,min_samples_leaf=0.004,random_state=42)
print(X_train)
MSE_CV = - cross_val_score(dt, X_train, y_train, cv=10,scoring="neg_mean_squared_error", n_jobs=-1)
dt.fit(X_train,y_train)
y_predict_train = dt.predict(X_train)
y_predict_test = dt.predict(X_test)
print(MSE_CV.mean()**(1/2))
print(MSE(y_train,y_predict_train)**(1/2))
print(MSE(y_test, y_predict_test)**(1/2))
X_validation = validaton[["city","months_listed","bedrooms","house_type","area"]]
X_validation_encoded = pd.get_dummies(X_validation, columns=["city","house_type"], drop_first=True)
X_validation_encoded = X_validation_encoded.reindex(columns=X_train.columns, fill_value=0)
y_validation_pred = dt.predict(X_validation_encoded)
base_result = validaton.copy()
base_result["price"] = y_validation_pred
base_result = base_result[["house_id","price"]]
print(base_result)Task 5
Fit a comparison model to predict the sale price of a house.
-
Fit your model using the data contained in “train.csv”
-
Use “validation.csv” to predict new values based on your model. You must return a dataframe named
compare_result, that includeshouse_idandprice. The price column must be your predicted values.
# Use this cell to write your code for Task 5
# My linreg model achieved a mean RSME of 23465.56, so even my simpler model is below the threshold, which makes me confident that my I will pass the requirements
from sklearn.linear_model import LinearRegression
train_data2 = pd.read_csv("train.csv")
X2 = train_data[["city","months_listed","bedrooms","house_type","area"]]
X2_encoded = pd.get_dummies(X1, columns=["city","house_type"], drop_first=True)
y2 = train_data["sale_price"]
X_train2, X_test2, y_train2, y_test2 = train_test_split(X2_encoded,y2,
test_size=0.2,
random_state=42)
kf = KFold(n_splits=5, shuffle=True,random_state=42)
linreg = LinearRegression()
linreg_cv = cross_val_score(linreg, X_train2, y_train2, cv=kf,scoring="neg_mean_squared_error")
print(np.sqrt(-linreg_cv))
linreg.fit(X_train2, y_train2)
y_predict_test2 = linreg.predict(X_test2)
print(MSE(y_test2, y_predict_test2)**(1/2))
y_validation_pred2 = linreg.predict(X_validation_encoded)
compare_result = validaton.copy()
compare_result["price"] = y_validation_pred2
compare_result = compare_result[["house_id","price"]]
print(compare_result)