Multiple Random regressor model in python

Build multiple Random forest regressor on X_train set and Y_train labels with max_depth parameter value changing from 3 to 5 and also setting n_estimators to one of 50, 100, 200 values.

Evaluate each model accuracy on testing data set.

Hint: Make use of for loop Print the max_depth and n_estimators values of the model with highest accuracy.

Note: Print the parameter values in the form of tuple (a, b). a refers to max_depth value and b refers to n_estimators

This is what I've tried so far:

boston= datasets.load_boston()
X_train, X_test, Y_train, Y_test = train_test_split(boston.data, boston.target, random_state=30)
for m in range(3,6) :
    rf_reg = RandomForestRegressor(n_estimators =100, max_depth=m)
    rf_reg = rf_reg.fit(X_train, Y_train) 
    print(rf_reg.score(X_test,Y_test))

This gives me the accuracy score for the 3 models but I am not able to fetch the highest accuracy's parameters individually. I can use rf_reg.get_params(), but it gives me all the parameters. I only want max_depth and n_estimators of the highest score one's


Solution 1:

max_reg = None  #<--- add this to represent the regressor with maximum score
max_score = 0   #<--- add this to represent maximum score
t=() # <--- add this to tuple declaration
c_estimators = 100 
for m in range(3,6) :
    rf_reg = RandomForestRegressor(n_estimators =c_estimators , max_depth=m)
    rf_reg = rf_reg.fit(X_train, Y_train) 
    rf_reg_score = rf_reg.score(X_test,Y_test)
    t = (m,c_estimators,rf_reg.score) # tuple assignment
    rf_reg_score = t[2]
    print (t)
    if rf_reg_score > max_score :
        max_score = rf_reg_score
        max_reg = rf_reg
        t = (m,c_estimators) # tuple assignment
print (t)

Solution 2:

You will get max_score with (5, 100) combination. And as per question they are asking to perform total 9 combinations. 3 x 3.