Step 6 — Fine Tuning the Model

** Introduction to Machine Learning & AI with Python
Lesson Content
0% Complete

Step 6 — Fine Tuning the Model

Phase: Machine Learning

“The default model is good. Fine tuning makes it great — and more importantly, trustworthy enough to deploy in a real pricing system.”

 

6a. GridSearchCV

from sklearn.model_selection import GridSearchCV

 

param_grid = {

    ‘n_estimators’:      [100, 200, 300],

    ‘max_depth’:         [None, 10, 20],

    ‘min_samples_split’: [2, 5, 10],

    ‘max_features’:      [‘sqrt’, ‘log2’]

}

 

grid_search = GridSearchCV(

    RandomForestRegressor(random_state=42),

    param_grid,

    cv=5,

    scoring=’neg_mean_absolute_error’,

    n_jobs=-1

)

 

grid_search.fit(X_train, y_train)

print(‘Best params:’, grid_search.best_params_)

 

6b. Making Predictions — The Pricing Engine

# A customer brings in: 1.2 carat, Ideal cut, E color, VS1 clarity

new_diamond = pd.DataFrame([{

    ‘carat’:       1.20,

    ‘cut_enc’:     4,     # Ideal

    ‘color_enc’:   5,     # E

    ‘clarity_enc’: 6,     # VS1

    ‘depth’:       61.5,

    ‘table’:       55.0

}])

 

predicted_price = best_model.predict(new_diamond)[0]

print(f’Estimated market price: ${predicted_price:,.0f}’)

 

💡 This is the moment machine learning delivers real business value. Your pricing team can now enter a diamond’s specifications and get an estimated market price — instantly, consistently, and based on 54,000 real transactions rather than one perso

Course Outline