Evaluating Model Performance: SettingwithCopyWarning

Hello,

I’ve been working through this mission in my own Jupyter notebook and I keep getting a SettingwithCopyWarning whenever I run the predict_price function (either on the accommodates column or bathrooms column). I am trying to figure out why and I’ve tried alternative ways to do the chain indexing, but I keep getting the error:

def predict_price(new_listing):
    temp_df = train_df.copy()
    temp_df['distance'] = temp_df['accommodates'].apply(lambda x: abs(x-new_listing))
    temp_df = temp_df.sort_values(by='distance')
    nearest_neighbor_prices = temp_df.iloc[0:5]['price']
    predicted_price = nearest_neighbor_prices.mean()
    return(predicted_price)
test_df['predicted_price'] = test_df['accommodates'].apply(predict_price)

Thanks for your help!
Scott

You can use this blog https://www.dataquest.io/blog/settingwithcopywarning/ post to help you resolve the setting with copy warning.

Hi Alvin,

I read the blog post, but I’m still not sure why the warning is being triggered. We created a copy of the train_df to prevent modifying the original dataframe. I’m looking for chained indexing and the only place I can see that being an issue is nearest_neighbor_prices = temp_df.iloc[0:5]['price']. I tried changing this to temp_df.loc[0:5, 'price'] or temp_df.loc[[0:5], 'price'] but I still get the error. First of all, am I rewriting with .loc[] correctly and second, is there some hidden chaining that I’m missing? Or is there some method that is returning a copy or view and that is creating the warning?

Thank,
Scott

I have to test it out to figure what’s wrong. Can you list the Mission URL and/or share the notebook?

Thank you Alvin!

Evaluating Model Performance.ipynb (20.5 KB)

Can you provide the csv?