add this and give visual plots of high price and low price

here:
data_combined[‘High_Price’] = (data_combined[‘Price’] > median_price).astype(int)

# Additional segment - Focusing on 2-bedroom units prediction within the ‘data_full’ specifically
data_full.dropna(subset=[‘Price’], inplace=True) # Dropping NA values specifically in ‘data_full’ for clear separation of concerns
data_2_bed = data_full[data_full[‘Bedroom2’] == 2]

# Note: Encoding and imputation steps would be here if new data were filtered

X = data_2_bed[features] # Assuming ‘features’ list matches your available columns in ‘data_full’
y = data_2_bed[‘Price’]

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
from sklearn.metrics import mean_absolute_error, r2_score, classification_report, f1_score, accuracy_score, roc_curve, auc, confusion_matrix
from sklearn.preprocessing import LabelEncoder
from sklearn.impute import SimpleImputer
import matplotlib.pyplot as plt
import seaborn as sns

# Load datasets
data_less = pd.read_csv(‘MELBOURNE_HOUSE_PRICES_LESS.csv’)
data_full = pd.read_csv(‘Melbourne_housing_FULL.csv’)

# Merge data based on common columns
data_combined = pd.merge(data_less[[‘Suburb’, ‘Rooms’, ‘Type’, ‘Price’]],
data_full[[‘Suburb’, ‘Rooms’, ‘Type’, ‘Price’, ‘Distance’, ‘Postcode’, ‘Bathroom’, ‘Car’, ‘Landsize’, ‘BuildingArea’, ‘YearBuilt’]],
on=[‘Suburb’, ‘Rooms’, ‘Type’, ‘Price’],
how=‘outer’)

# Drop rows with missing ‘Price’ values
data_combined.dropna(subset=[‘Price’], inplace=True)

# Encode categorical variables
label_encoder = LabelEncoder()
data_combined[‘Suburb’] = label_encoder.fit_transform(data_combined[‘Suburb’])
data_combined[‘Type’] = label_encoder.fit_transform(data_combined[‘Type’])

# Choose features
features = [‘Suburb’, ‘Rooms’, ‘Type’, ‘Distance’, ‘Postcode’, ‘Bathroom’, ‘Car’, ‘Landsize’, ‘BuildingArea’, ‘YearBuilt’]
X = data_combined[features]
y = data_combined[‘Price’]

# Split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Handle missing values (here using median for simplicity)
imputer = SimpleImputer(strategy=‘median’)
X_train = imputer.fit_transform(X_train)
X_test = imputer.transform(X_test)

# Train a RandomForestRegressor
model = RandomForestRegressor(n_estimators=100, random_state=42)
model.fit(X_train, y_train)

# Make predictions
predictions = model.predict(X_test)

# Evaluate predictions
mae = mean_absolute_error(y_test, predictions)
r2 = r2_score(y_test, predictions)
print(“Mean Absolute Error (MAE):”, mae)
print(“R^2 Score:”, r2)

# Additional segment - Hypothetical Classification Task based on Median Price
median_price = data_combined[‘Price’].median()
data_combined[‘High_Price’] = (data_combined[‘Price’] > median_price).astype(int)

y_class = data_combined[‘High_Price’]
X_train_class, X_test_class, y_train_class, y_test_class = train_test_split(X, y_class, test_size=0.2, random_state=42)

X_train_class = imputer.fit_transform(X_train_class)
X_test_class = imputer.transform(X_test_class)

# Train a RandomForestClassifier
classifier = RandomForestClassifier(n_estimators=100, random_state=42)
classifier.fit(X_train_class, y_train_class)

# Make predictions
predictions_class = classifier.predict(X_test_class)
predictions_proba = classifier.predict_proba(X_test_class)[:,1]

# Classification metrics
print(classification_report(y_test_class, predictions_class))
print(“F1 Score:”, f1_score(y_test_class, predictions_class))
print(“Accuracy Score:”, accuracy_score(y_test_class, predictions_class))

# ROC Curve
fpr, tpr, thresholds = roc_curve(y_test_class, predictions_proba)
roc_auc = auc(fpr, tpr)

plt.figure()
plt.plot(fpr, tpr, color=‘darkorange’, lw=2, label=‘ROC curve (area = %0.2f)’ % roc_auc)
plt.plot([0, 1], [0, 1], color=‘navy’, lw=2, linestyle=‘–’)
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel(‘False Positive Rate’)
plt.ylabel(‘True Positive Rate’)
plt.title(‘Receiver Operating Characteristic’)
plt.legend(loc=“lower right”)
plt.show()

# Confusion Matrix Plot
conf_mat = confusion_matrix(y_test_class, predictions_class)
sns.heatmap(conf_mat, annot=True, fmt=“d”, cmap=“Blues”)
plt.title(‘Confusion Matrix’)
plt.xlabel(‘Predicted’)
plt.ylabel(‘Actual’)
plt.show()

# Density Plots for Predicted Probabilities
sns.kdeplot(predictions_proba[y_test_class == 0], label=‘Not High Price’, shade=True)
sns.kdeplot(predictions_proba[y_test_class == 1], label=‘High Price’, shade=True)
plt.title(‘Density plot of Predicted Probabilities’)
plt.xlabel(‘Probability’)
plt.ylabel(‘Density’)
plt.legend()
plt.show()