# Module: jess_obesity_dataset_RandomForestRegressor.py
# Subject: Perform RandomForestRegressor modeling on ObesityDataSet_cleaned_and_data_sinthetic.csv
# Date: 13 Jul, 2024
# Imports
import os
os.system(“cls”)
import sys
import time
import datetime
import numpy as np
import pandas as pd
import array
pd.set_option(‘display.precision’, 4)
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
import seaborn as sns
import traceback as tb
import warnings
warnings.filterwarnings(‘ignore’)
#From / Imports
from pandas import DataFrame
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import train_test_split
# Random Forest
from sklearn.ensemble import RandomForestRegressor
from sklearn.inspection import permutation_importance
# printing functions
def print_program_runtime(start_time):
“””print_program_runtime: prints out the running time of the program
Args:
start_time (time[float]): recorded start time to use to determine run time
“””
end_time = time.time()
diff_time = end_time – start_time
result = time.strftime(“%H:%M:%S”, time.gmtime(diff_time))
print(“program runtime: {}”.format(result))
def print_cv_scores (hyperparameter_set_label, cv_scores_array):
“””print_cv_scores: prints sorted cross validation scores for hyperparameter_set_label
Args:
hyperparameter_set_label (string): Label to use to indicate hyperparameter set
cv_scores_array (array): Array of cross validation scores
“””
sort_list = cv_scores_array.tolist()
sort_list.sort()
sort_array = np.sort(sort_list)
print (f” Cross Validation For {hyperparameter_set_label}:”)
print (f” Mean CV Score : {cv_scores_array.mean()}”)
print (f” Stan Dev CV Score : {cv_scores_array.std()}”)
print (f” Sorted CV Scores Array : {sort_array}”)
def print_regression_scores (r2_score_value, ar2_score_value, mean_squared_error_array, root_mse):
“””print_regression_scores: prints regression scores based on parameters
Args:
r2_score_value (float64): R2 score value
ar2_score_value (float64): Adjusted R2 score value
mean_squared_error_array (float64): mean squared error array
root_mse (float64): Root mean squred error
“””
print (” Regression R2 and MSE”)
print (” R2 : “, float(“{0:0.4f}”.format(r2_score_value)))
print (” Adjusted R2 : “, float(“{0:0.4f}”.format(ar2_score_value)))
print (” Mean Squared Error : “, float(“{0:0.8f}”.format(mean_squared_error_array)))
print (” Root Mean Squared Error : “, float(“{0:0.8f}”.format(root_mse)))
print ()
# miscellanious functions
def get_directory_strings ():
“””get_directory_strings: returns directory data used by program
Returns:
string, string, string, string, string, string:
images_path_folder, csv_path_folder, csv_full_file_name, csv_file_name, csv_preprocessed_file_name
“””
base_path = “C:\Projects\Python\DATA260\data_260_python\src\CategoricalExperiment”
csv_path_folder = f”{base_path}/csv”
csv_file_name = “ObesityDataSet_cleaned_and_data_sinthetic”
csv_file_extension = “.csv”
csv_full_file_name = csv_file_name + csv_file_extension
images_path_folder = f”{base_path}/images/{g_date_suffix}”
csv_preprocessed_file_name = f”{csv_path_folder}/{csv_file_name}_preprocessed{csv_file_extension}”
return images_path_folder, csv_path_folder, csv_full_file_name, csv_preprocessed_file_name
# Dataframe functions
def get_dataframe_from_csv (csv_path_folder, csv_file_name):
“””get_dataframe_from_csv: Reads the specified csv file and returns DataFrame with csv file data
Args:
csv_path_folder (string): fully qualified folder name to find the csv file
csv_file_name (string): file name of csv file
Returns:
pandas.DataFrame: DataFrame loaded with contents of csv file
“””
# data frame data loading
try:
csv_path_file = os.path.join(csv_path_folder, csv_file_name)
df_return = pd.read_csv(filepath_or_buffer = csv_path_file)
return df_return
except Exception as e:
print (tb.print_exc)
raise (e) #We want the program to fail here
def get_manual_factorization_dictionary():
“””get_manual_factorization_dictionary: Returns a dictionary of factorized values for each column of the Obesity data.
Data indexed to start at 1.
Returns:
list(str,list[(str,int),(str,int),…]): returns a list of [“Col Name”, [(“string value”,factorized int),(“string value”,factorized int),…]]
“””
# general factorization settings list
YN_factorization = [(“no”, 1), (“yes”, 2)]
NoSoFrAl_factorization = [(“no”, 1), (“sometimes”, 2), (“frequently”, 3), ( “always”, 4)]
NeSoAl_factorization = [(“never”, 1), (“sometimes”, 2), (“always”, 3)]
# factorization settings for categorical data
Gender_factorization = [“Gender”, [(“male”, 1), (“female”, 2)]]
FHWO_factorization = [“FHWO”, YN_factorization]
FAVC_factorization = [“FAVC”, YN_factorization]
FCVC_factorization = [“FCVC”, NeSoAl_factorization]
CAEC_factorization = [“CAEC”, NoSoFrAl_factorization]
SMOKE_factorization = [“SMOKE”, YN_factorization]
CH2O_factorization = [“CH2O”, [(“less than a liter”, 1), (“between 1 and 2 l”, 2 ), (“more than 2 l”, 3)]]
SCC_factorization = [“SCC”, YN_factorization]
FAF_factorization = [“FAF”, [(“0”, 1), (“1 to 2”, 2), (“2 to 4”, 3), (“4 to 5”, 4)]]
TUE_factorization = [“TUE”, [(“0 to 2”, 1), (“3 to 5”, 2), (“>5”,2)]]
CALC_factorization = [“CALC”, NoSoFrAl_factorization]
MTRANS_factorization = [“MTRANS”
, [(“automobile”, 1), (“motorbike”, 2), (“public_transportation”, 3), (“bike”, 4), (“walking”,5)]
]
NObeyesdad_factorization = [“NObeyesdad”, [ (“insufficient_weight”, 1) , (“normal_weight”, 2) , (“overweight_level_i”, 3)
, (“overweight_level_ii”, 4) , (“obesity_type_i”, 5) , (“obesity_type_ii”, 6)
, (“obesity_type_iii”, 7)
]
]
return_dictionary = [Gender_factorization, FHWO_factorization, FAVC_factorization
, FCVC_factorization, CAEC_factorization, SMOKE_factorization
, CH2O_factorization, SCC_factorization, FAF_factorization
, TUE_factorization, CALC_factorization, MTRANS_factorization
, NObeyesdad_factorization]
return return_dictionary
def manual_factorization (df, manual_factorization_dictionary, verbose=0):
“””manual_factorization: Performs manual factorization using the dictionary from get_manual_factorization_dictionary
Args:
df (str and numeric): DataFrame holding obesity data
Returns:
DataFrame: returns factorized obesity data in DataFrame
“””
return_df = pd.DataFrame.copy(df)
if (verbose > 0):
print (f”Performaing Manual Factorization”)
for factor_column in manual_factorization_dictionary:
factor_title = factor_column[0]
factor_dict = factor_column[1]
if (verbose > 0):
print (f”Column {factor_title}:”)
print (f” Value Dictionary: {factor_dict}”)
for factor_values in factor_dict:
return_df[factor_title].replace(factor_values[0], factor_values[1], inplace=True)
if (verbose > 0):
print (f” Replaced : {str(factor_values[0]).rjust(21)}, {str(factor_values[1]).rjust(3)}”)
if (verbose > 0):
print()
return return_df
def drop_features (df, drop_features_list):
“””drop_features: returns copy of df with features in drop_features_list dropped
Args:
df (pandas.DataFrame): DataFrame to copy and drop features from
drop_features_list (list<string>): List of feature names to drop
Returns:
pandas.DataFrame: copy of df with drop_features_list columns removed
“””
return_df = DataFrame.copy(df)
for feature in drop_features_list:
return_df = return_df.drop(feature, axis=1)
return return_df
def preprocess_and_save_obesity_dataset (df, csv_preprocessed_file_name, verbose = 0, factorize=True):
“””preprocess_and_save_obesity_dataset: Performs processing of Dataframe to prepare for ML
Args:
df (pandas.Dataframe): Dataframe holding obesity data to be processed
verbose (int32): [default 0] debug value indicating the amount of output to produce
factorize (bool): [default True] flag indicating whether returned dataframe will be factorized
Returns:
pandas.Dataframe: Processed Dataframe
“””
try:
return_df = df.copy(deep=True)
return_df.drop(‘id’, axis=1, errors=’ignore’)
if (verbose > 0):
print(“Original Dataset Info”)
print(return_df.info())
# Dropping ‘id’
if (‘id’) in return_df.columns:
if (verbose > 0):
print(“Removing id column”)
return_df.drop(‘id’, axis=1, inplace=True)
# Changing family_history_with_overweight to FWHO
if (‘family_history_with_overweight’ in return_df.columns):
if (verbose > 0):
print(“Renaming family_history_with_overweight to FHWO”)
return_df.rename(columns={‘family_history_with_overweight’: ‘FHWO’}, inplace=True)
# Adding BMI data if needed
if ‘BMI’ not in return_df.columns:
if (verbose > 0):
print (“Calculating BMI data and adding BMI column. BMI = Weight / squared(Height)”)
return_df[‘BMI’]=return_df[‘Weight’]/((return_df[‘Height’]) ** 2)
# convert all categorical columns to numeric
if (factorize == True):
manual_factorization_dictionary = get_manual_factorization_dictionary()
return_df = manual_factorization(return_df, manual_factorization_dictionary)
# converting age data to integer, to match original data
if (verbose > 0):
print(“Correcting Age Values Back to Integer Values (Sythetic Data Added Non-Integer Values)”)
return_df[‘Age’] = return_df[‘Age’].round(0).astype(int)
count_nan = return_df.isna().sum().sum()
# check for NaNs and remove
if (verbose > 0):
print(return_df.info())
print (f”Number of NaN : {count_nan}”)
if count_nan > 0:
if (verbose > 0):
print (“Dropping NaN”)
return_df.dropna(inplace=True)
return_df.reset_index(drop=True, inplace=True)
else:
if (verbose > 0):
print (“No NaN found.”)
# removing duplicates
count_dup = return_df.duplicated().sum()
if (verbose > 0):
print (f”Number of Duplicates : {count_dup}”)
if count_dup > 0:
if (verbose > 0):
print (“Dropping Duplicates”)
return_df.drop_duplicates(inplace=True)
return_df.reset_index(drop=True, inplace=True)
else:
if (verbose > 0):
print (“No duplicates found.”)
# save file if it does not exist
if (os.path.isfile(csv_preprocessed_file_name) == False):
DataFrame.to_csv(df, path_or_buf=csv_preprocessed_file_name, index=False)
else:
print (f”File {csv_preprocessed_file_name} found. Not saving csv.”)
return return_df
except Exception as e:
print (tb.print_exc())
raise (e) #We want the program to fail here
# Factories
def hyperparameter_factory(criterion_type):
“”” hyperparameter_factory: Returns hyperparameter settings used to run model
Args:
criterion_type (string): String describing the hyperparameter set to return
Returns:
hyperparameter_title (string): title to use for hyperparameter set
hyperparameter_desc (string): description to use for hyperparameter set
hyperparameter_values (set): hyperparameter settings to use for regressor
“””
check_this = criterion_type.lower()
if (check_this == ‘poisson’):
# previous hyperparameter settings
# ‘bootstrap’ : False, ‘criterion’ :’poisson’, ‘max_depth’ : 90, ‘max_features’ : 3, ‘min_samples_leaf’ : 1, ‘min_samples_split’ : 6, ‘n_estimators’ : 160
# ‘bootstrap’ : False, ‘criterion’ :’poisson’, ‘max_depth’ : 20, ‘max_features’ : 2, ‘min_samples_leaf’ : 1, ‘min_samples_split’ : 4, ‘n_estimators’ : 410
# ‘bootstrap’ : True, ‘criterion’ : ‘poisson’, ‘max_depth’ : 30, ‘max_features’ : 2, ‘min_samples_leaf’ : 1, ‘min_samples_split’ : 4, ‘n_estimators’ : 260
hyperparameter_title = f”Poisson”
hyperparameter_desc = “bootstrap=True, criterion=’poisson’, max_depth=30, max_features=2, min_samples_leaf=1, min_samples_split=4, n_estimators=260”
hyperparameter_values = {
‘bootstrap’ : True,
‘criterion’ : ‘poisson’,
‘max_depth’ : 30,
‘max_features’ : 2,
‘min_samples_leaf’ : 1,
‘min_samples_split’ : 4,
‘n_estimators’ : 260
}
elif (check_this == ‘squared_error’):
# previous hyperparameter settings
# ‘bootstrap’: True, ‘criterion’: ‘squared_error’, ‘max_features’: r, ‘n_estimators’: 150
# ‘bootstrap’: True, ‘criterion’: ‘squared_error’, ‘max_features’: 4, ‘n_estimators’: 50
# ‘bootstrap’: True, ‘criterion’: ‘squared_error’, ‘max_features’: ‘sqrt’, ‘n_estimators’: 250
hyperparameter_title = f”Squared_Error”
hyperparameter_desc = “bootstrap=True, criterion=’squared_error’, max_features=’sqrt’, n_estimators=250”
hyperparameter_values = {
‘bootstrap’ : True,
‘criterion’ : ‘squared_error’,
‘max_features’ : ‘sqrt’,
‘n_estimators’ : 250
}
else:
hyperparameter_title = None
hyperparameter_desc = None
hyperparameter_values = None
return hyperparameter_title, hyperparameter_desc, hyperparameter_values
def DataFrame_factory(df, dataframe_type):
“””DataFrame_factory: Returns dataframe holding data selected from df based on dataframe_type
Args:
df (pandas.DataFrame): DataFrame to get data from
dataframe_type (string): string describing the column set sought
Returns:
pandas.DataFrame: DataFrame holding data from df as specified by dataframe_type
“””
# Create dataframe to return
return_df = pd.DataFrame.copy(df)
check_this = dataframe_type.lower()
related_features = None
all_features = [“Gender”, “Age”, “Height”, “Weight”, “FHWO”, “FAVC”
, “FCVC”, “NCP”, “CAEC”, “SMOKE”, “CH2O”, “SCC”, “FAF”
, “TUE”, “CALC”, “MTRANS”, “NObeyesdad”, “BMI”
]
# set related_factors to include features for returned dataframe
if (check_this == “all”):
related_features = all_features
if (check_this == “categorical”):
related_features = [“Gender”, “FHWO”, “FAVC”, “FCVC”, “NCP”, “CAEC”
, “SMOKE”, “CH2O”, “SCC”, “FAF”
, “TUE”, “CALC”, “MTRANS”, “BMI”
]
if (check_this == “diet”):
related_features = [“FAVC”, “FCVC”, “NCP”, “CAEC”, “CALC”, “CH2O”, “BMI”]
if (check_this == “exercise”):
related_features = [“FAF”, “TUE”, “MTRANS”, “BMI”]
if (check_this == “habit”):
related_features = [“SCC”, “SMOKE”, “BMI”]
if (check_this == “genetic”):
related_features = [“Gender”, “FHWO”, “BMI”]
# remove unwanted features
if (related_features != None):
for factor in all_features:
if factor not in related_features:
if factor in return_df.columns:
return_df = return_df.drop(factor, axis=1)
else:
return_df = None
return return_df
def run_regressor (use_this_regressor, X, y):
“””run_regressor: Runs the supplied regressor, and returns scores and prediction and training data
Args:
use_this_regressor (sklearn.ensemble.regressor): Regressor to run against X, y data
X (pandas.DataFrame): X feature matrix
y (pansas.Series): y feature series
Returns:
sklearn.ensemble.regressor: use_this_regressor,
float64: r2_score_value,
float64: ar2_score_value,
float64: mean_squared_error_array,
float64: root_mse,
DataFrame: X_train,
FataFrame: X_test,
Series: y_train,
Series: y_test,
Series: y_predicted
“””
try:
# data split: 80% for training, 20% for testing
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=50)
# model fitting
use_this_regressor.fit(X_train, y_train)
# y prediction
y_predicted = use_this_regressor.predict(X_test)
# calculate regression r2 and mean squared error scores
r2_score_value = r2_score(y_test, y_predicted)
ar2_score_value = 1-(1-r2_score_value)*((len(X_test)-1)/(len(X_test)-len(X_test.columns)-1))
mean_squared_error_array = mean_squared_error(y_test, y_predicted, squared=True)
root_mse = mean_squared_error(y_test, y_predicted, squared=False)
return use_this_regressor, r2_score_value, ar2_score_value, mean_squared_error_array \
, root_mse, X_train, X_test, y_train, y_test, y_predicted
except Exception as e:
print (tb.print_exc())
raise (e) #We want the program to fail here
def k_folds_cross_validation(model_estimator, X_feature, y_label, k_number_splits, k_random_state=None):
“””k_folds_cross_validation: performs k folds cross validation on model_estimator
args:
model_estimator (object): object model
X_feature (dataframe): pandas data frame
y_label (series): pandas data series
k_number_splits (int): k-fold number of splits
k_random_state (int, optional): defaults to none
returns:
cv_scores_array (array): array holding scores of cross validation
“””
cv_scores_array = None
try:
k_folds = KFold(n_splits=k_number_splits, random_state=k_random_state, shuffle=True)
cv_scores_array = cross_val_score(estimator=model_estimator, X=X_feature, y=y_label, cv=k_folds, n_jobs=-1)
except Exception as e:
print (tb.print_exc())
raise (e) #We want the program to fail here
return cv_scores_array
# Plot and save images functions
def plot_and_save_correlation_matrix (df, title, file_title, images_path_folder, program_name, use_cmap=”RdBu”):
“””plot_and_save_correlation_matrix: Create plot of correlation matrix and save image of it to file
Args:
df (pandas.Dataframe): Dataframe to make the correlation matrix of
title (string): Title of correlation image (appears on image)
file_title (string): Title of correlation file (spaces changed to underscores)
images_path_folder (string): Folder path to save correlation image file
program_name (string): Name of program (added to image file name)
use_cmap (string): Default:”RdBu” Color map string to use for plot
“””
try:
if (g_plot_images==True):
# calculate and plot correlation matrix
correlation_matrix = df.corr()
# size output image large enough to hold data
if (len(df.columns) < 8 ):
if (len(df.columns) <= 3):
vert_param = 4
horiz_param = 4
else:
horiz_param = vert_param = len(df.columns)
else:
horiz_param = vert_param = len(df.columns)
figsize = (horiz_param, vert_param)
# Set figure size
plt.figure(figsize=figsize)
# plot heatmap and titles
sns.heatmap(data=correlation_matrix, annot=True, cmap=use_cmap, vmin=-1.0, vmax=1.0)
plt.title(title)
# create file name
filename = f”{images_path_folder}/{program_name.replace(‘ ‘,’_’)}_{file_title.replace(‘ ‘,’_’)}{g_date_suffix}.png”
# save file
plt.savefig(filename)
# clear the plt
plt.clf()
plt.cla()
except Exception as e:
print (tb.print_exc())
raise (e) #We want the program to fail here
def plot_and_save_gini_feature_importance_plot (model, data_frame, title, file_title, images_path_folder, program_name):
“””plot_and_save_gini_feature_importance_plot: Create plot of feature importance and save file of image
Args:
df (pandas.Dataframe): Dataframe to make the correlation matrix of
title (string): Title of correlation image (appears on image)
file_title (string): Title of correlation file (spaces changed to underscores)
images_path_folder (string): Folder path to save correlation image file
program_name (string): Name of program (added to image file name)
“””
# calculate and plot correlation matrix
try:
if (g_plot_images==True):
# calculate and plot feature importance
feature_importance = np.array(model.feature_importances_)
feature_name = np.array(data_frame.columns)
feature_names={‘feature_name’:feature_name, ‘feature_importance’:feature_importance}
df_feature_importances = pd.DataFrame(feature_names)
df_feature_importances.sort_values(by=[“feature_importance”], ascending=False, inplace=True)
# Set figure size
plt.figure(figsize=(15,8))
sns.barplot(x=df_feature_importances[‘feature_importance’], y=df_feature_importances[‘feature_name’])
plt.title(title)
plt.xlabel(‘Feature Importance’)
plt.ylabel(‘Feature Name’)
plt.tight_layout()
# create file name
filename= f”{images_path_folder}/{program_name.replace(‘ ‘,’_’)}_{file_title.replace(‘ ‘,’_’)}{g_date_suffix}.png”
# save file
plt.savefig(filename)
# clear the plt
plt.clf()
plt.cla()
except Exception as e:
print (tb.print_exc())
raise (e) #We want the program to fail here
def plot_and_save_all_correlation_matrices(df, images_path_folder, program_name):
“””plot_and_save_all_correlation_matrices: Plots and saves all correlation matrices
Args:
df (pandas.DataFrame): DataFrame holding values for the correlation matrices
images_path_folder (string): File path to store the image files
program_name (string): Name of program for file names
“””
df_all = DataFrame_factory(df, “all”)
df_categorical = DataFrame_factory(df, “categorical”)
df_diet_related = DataFrame_factory(df, “diet”)
df_exercise_related = DataFrame_factory(df, “exercise”)
df_habit_related = DataFrame_factory(df, “habit”)
df_genetic_related = DataFrame_factory(df, “genetic”)
sub_dataframes = [(“all”, df_all), (“categorical”, df_categorical), (“diet”, df_diet_related), (“exercise”, df_exercise_related)
, (“habit”, df_habit_related), (“genetic”, df_genetic_related)]
for this_name, this_df in sub_dataframes:
title = f”Correlation Matrix Plot\n{this_name.capitalize()} Features + BMI Dataset”
file_title =f”Corr Mat Plot {this_name.capitalize()} Features”
print(f”{this_name.upper()} RELATED DATAFRAME”)
print(this_df)
plot_and_save_correlation_matrix (this_df, title, file_title, images_path_folder, program_name)
def plot_and_save_test_vs_pred_scatter_plot (y_test, y_pred, hyperparameter_set_label
, images_path_folder, program_name
):
“””plot_and_save_test_vs_pred_scatter_plot: plots y true vs y predicted values
Args:
y_test (Series): Test values
y_pred (Series): Y predicted values
hyperparameter_set_label (string): label of hyperparameters
images_path_folder (string): path to store images at.
program_name (string): used to generate part of the file name for images.
“””
try:
if (g_plot_images==True):
# Scatter plot of the true values vs. predicted values
plt.scatter(y_test, y_pred)
# set plot labels, title
plt.xlabel(“True Values (BMI)”, fontsize=15)
plt.ylabel(“Predicted Values (BMI)”, fontsize=15)
file_title = f”True vs Predicted BMI Values – Parameters {hyperparameter_set_label}”
plt.title(file_title)
# True vs Predicted line
plt.plot(np.linspace(min(y_test), max(y_test))
, np.linspace(min(y_pred), max(y_pred))
, c=”red”, linestyle=’solid’, label=”True vs Predicted Line”)
# fit line for True vs Predicted
plt.plot(np.unique(y_test), np.poly1d(np.polyfit(y_test, y_pred, 1))(np.unique(y_test))
, c=”blue”, linestyle=”solid”, label=”Fit Line”)
# comparison line for equality
plt.plot(np.linspace(min(np.unique(y_test)), max(np.unique(y_test)))
, np.linspace(min(np.unique(y_test)), max(np.unique(y_test)))
, c=”black”, linestyle=”dashed”, label=”if y_test=y_pred”)
plt.legend()
# create file name
filename = images_path_folder + “/” + program_name.replace(‘ ‘,’_’) \
+ “_” + hyperparameter_set_label + “_” \
+ file_title.replace(‘ ‘,’_’).replace(‘\n’,’_’) \
+ g_date_suffix + “.png”
# save file
plt.savefig(filename)
# clear the plt
plt.clf()
plt.cla()
except Exception as e:
print (tb.print_exc)
raise (e) #We want the program to fail here
def plot_and_save_categorical_scatter_plots (y_test, y_pred, X_test, hyperparameter_set_label, file_title
, images_path_folder, program_name
):
“””plot_and_save_categorical_scatter_plots: Plots scatter plots and saves image files in specified folder
Args:
y_test (Series): Test y values
y_pred (Series): Predicted y values
X_test (DataFrame): Test data DataFrame
hyperparameter_set_label (string): label for hyperparameter settings
file_title (string): title to use to generate file name.
images_path_folder (string): path to store images at.
program_name (string): used to generate part of the file name for images.
“””
try:
if (g_plot_images==True):
for col_name in X_test:
this_X_test = X_test[col_name]
# Plotting fit line
X_ticks = np.unique(this_X_test)
this_min = np.min(X_ticks)-0.5
this_max = np.max(X_ticks)+0.5
plt.xticks = X_ticks
ax = plt.figure().gca()
ax.xaxis.set_major_locator(MaxNLocator(integer=True))
ax.set_xlim([this_min, this_max])
# Plot True values larger than predicted values in less vibrant color
plt.scatter(this_X_test, y_test, color=’blue’, label=’True Values’, s=24)
# Plot Predicted values plotted smaller than True values in more vibrant color
plt.scatter(this_X_test, y_pred, color=’red’, label=’Predicted Values’, s=8)
# Axes labels
plt.xlabel(f”{col_name} Values”)
plt.ylabel(“BMI values”)
plt.title(“Feature Values vs Target Variable”)
plt.plot(np.unique(this_X_test), np.poly1d(np.polyfit(this_X_test, y_pred, 1))(np.unique(this_X_test))
, c=”black”, linestyle=”dotted”, label =”Fit Line”)
# Create file name
file_title = f”{hyperparameter_set_label} X_test[{col_name}] vs Test & Predicted Values”
filename = images_path_folder + “/” + program_name.replace(‘ ‘,’_’) + “_” \
+ file_title.replace(‘ ‘,’_’).replace(‘\n’,’_’) + g_date_suffix + “.png”
plt.legend()
# Save plot in filename
plt.savefig(filename)
# Clear plot object
plt.clf()
plt.cla()
except Exception as e:
print (tb.print_exc())
raise (e) #We want the program to fail here
def main():
“””jess_obesity_dataset_FullTreatment_RFR_ResearchPaper.py
“””
program_name = “Obesity RandomForestRegressor”
print(program_name)
print()
images_path_folder, csv_path_folder, csv_full_file_name, csv_preprocessed_file_name = \
get_directory_strings()
if (False==os.path.exists(images_path_folder)):
os.mkdir(images_path_folder)
np.set_printoptions(formatter={‘float’: ‘{: 0.4f}’.format})
# data frame data loading
df = get_dataframe_from_csv (csv_path_folder, csv_full_file_name)
# process data frame
df = preprocess_and_save_obesity_dataset(df, csv_preprocessed_file_name)
print (“post preprocess df”)
print (df)
print ()
plot_and_save_all_correlation_matrices (df, images_path_folder, program_name)
print()
print()
# print preprocessed dataset metadata
print(df)
print()
# y target
y = df[“BMI”]
# X features
drop_features_list = [“NObeyesdad”, “Weight”, “Height”, “Age” ]
X = drop_features (df, drop_features_list)
print(X)
# Save Correlation matrix of just categorical data + BMI
title = “Correlation Matrix Plot + BMI\nRemoved Age, Weight, Height, NObeyesdad”
file_title = “Correlation Matrix Plot with BMI included”
plot_and_save_correlation_matrix (X, title, file_title
, images_path_folder, program_name)
# Drop y column
X = X.drop(“BMI”, axis=1)
title = “Correlation Matrix Plot – BMI\nRemoved Age, Weight, Height, NObeyesdad”
file_title = “Correlation Matrix Plot with BMI removed”
plot_and_save_correlation_matrix (X, title, file_title
, images_path_folder, program_name)
print ()
print (“X Columns:”)
print ()
print (X)
# Making list of hyperparameters settings
hparms_list = list()
hparms_list.append( hyperparameter_factory (“Squared_Error”) )
hparms_list.append( hyperparameter_factory (“Poisson”) )
# Cycle through the list of hyperparameter settings
item_index = 1
for item in hparms_list:
use_this_regressor = RandomForestRegressor (**item[2])
print ()
print (f” Testing hyperparameters set {item_index}: {item[0]}”)
print (f” Hyperparameter set {item_index}: {item[2]}”)
cv_scores_array = k_folds_cross_validation(use_this_regressor, X, y, 13)
print_cv_scores (item[0], cv_scores_array)
print ()
# Run the regression
plot_this_regressor, this_r2_score_value, this_ar2_score_value \
, this_mean_squared_error_array, this_root_mse, X_train, X_test, y_train, y_test, y_pred = \
run_regressor (use_this_regressor, X, y)
print_regression_scores (this_r2_score_value, this_ar2_score_value
, this_mean_squared_error_array, this_root_mse)
# Plot test vs predicted scatter plot
plot_and_save_test_vs_pred_scatter_plot (y_test=y_test, y_pred=y_pred, hyperparameter_set_label=item[0]
, images_path_folder=images_path_folder, program_name=program_name
)
# Plot categorical test vs predicted scatter plots
plot_and_save_categorical_scatter_plots (y_test=y_test, y_pred=y_pred, X_test=X_test, hyperparameter_set_label=item[0]
, file_title=f”{item[0]} True vs Predicted BMI Values”
, images_path_folder=images_path_folder, program_name=program_name
)
# Plot and save gini feature importance plot
use_this_title = f”RandomForestRegressor Feature Importance Plot: {item[0]}”
use_this_file_title = f”{item[0]} RandomForestRegressor Feature Importance Plot”
plot_and_save_gini_feature_importance_plot (plot_this_regressor
, X, use_this_title, use_this_file_title
, images_path_folder, program_name)
item_index += 1
print()
print()
if __name__ == ‘__main__’:
g_plot_images = True
start_time = time.time()
st = datetime.datetime.now()
ds_month=f”{st.month}”.zfill(2)
ds_day=f”{st.day}”.zfill(2)
ds_hour=f”{st.hour}”.zfill(2)
ds_minute=f”{st.minute}”.zfill(2)
ds_second=f”{st.second}”.zfill(2)
g_date_suffix = f”_{st.year}_{ds_month}_{ds_day}_{ds_hour}_{ds_minute}_{ds_second}”
main()
print_program_runtime(start_time)