import pandas as pd from sklearn.preprocessing import LabelEncoder from sklearn.model_selection import train_test_split from sklearn.ensemble import GradientBoostingRegressor from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score, median_absolute_error from joblib import dump # Load the dataset df = pd.read_csv('cleaned_housesTRAIN.csv') # Apply label encoding to 'Area' and 'Suburb' le_area = LabelEncoder() df['Area'] = le_area.fit_transform(df['Area']) le_suburb = LabelEncoder() df['Suburb'] = le_suburb.fit_transform(df['Suburb']) # Save the label encoders dump(le_area, 'le_area.joblib') dump(le_suburb, 'le_suburb.joblib') # Shuffle the dataframe df = df.sample(frac=1) # Split the data into features (X) and target (y) X = df.drop('Rent', axis=1) y = df['Rent'] # Split the data into training and test sets (90/10 split) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=42) # Create a Gradient Boosting regressor with specified hyperparameters gb = GradientBoostingRegressor(n_estimators=850, learning_rate=0.195, max_depth=7, random_state=42) # Train the model gb.fit(X_train, y_train) # Make predictions on the test set y_pred = gb.predict(X_test) # Calculate MAE, MSE, and R2 mae = mean_absolute_error(y_test, y_pred) mse = mean_squared_error(y_test, y_pred) r2 = r2_score(y_test, y_pred) medae = median_absolute_error(y_test, y_pred) print(f"MAE: {mae}, MSE: {mse}, R2: {r2}, MedAE: {medae}") # Save the model dump(gb, 'bestmodelyet.joblib')