# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
# Random Forest Regression

# Importing the libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# -

# Loading the dataset 
data = pd.read_csv("boston.csv")

data.info()  # Getting information about our dataset

data.head()  # Displays the first 5 elements of our dataset

# Replacing 0 values in columns 'ZN' and 'CHAS' with NaN
data.ZN.replace(0, np.nan, inplace = True)
data.CHAS.replace(0, np.nan, inplace = True)

data.info()  # Checking for null values

data.isnull().sum()/ len(data) * 100  # Checking the % of missing values in our dataset

# As we can see below both “ZN” and “CHAS” are missing more than
# 70% data so we will remove both these features.
data.drop(['Unnamed: 0', 'ZN', 'CHAS'], axis = 1, inplace = True)

data.info()

data.isnull().sum()/ len(data) * 100  # Again calculating percentage of missing values

# To get basic stats about our data like mean, median, count etc. 
# We use .describe() method as shown below:
data.describe()

# Splitting our data into dependent and independent variables
X = data.iloc[:, :-1].values
y = data.iloc[:, -1].values

# +
# Splitting our data into Test set and Training set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 0)

y_train = y_train.reshape(-1,1)
y_test = y_test.reshape(-1, 1)

# +
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
sc_y = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.fit_transform(X_test)

y_test = sc_y.fit_transform(y_test)
y_train = sc_y.fit_transform(y_train)
# -

# Importing the RandomForestRegressor class from sklearn.ensemble library
from sklearn.ensemble import RandomForestRegressor
model = RandomForestRegressor(n_estimators = 500, criterion = 'mse', random_state = 0)
model.fit(X_train, y_train)

# Making predictions with our model
y_pred = model.predict(X_test)
y_pred = y_pred.reshape(-1, 1)
y_pred = sc_y.transform(y_pred)

# Calculating the performance metrics
from sklearn import metrics  
print("MAE", metrics.mean_absolute_error(y_test, y_pred))
print("MSE", metrics.mean_squared_error(y_test, y_pred))
print("RMSE", np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
print("Score:", model.score(X_test, y_test))

StarcoderdataJupyterScriptsDedupFiltered