When I try to run this code, always I have different error

def predicted_price_multivariante(new_listing_value,feature_columns):
temp_df = norm_train_df
temp_df[‘distance’] = distance.cdist(temp_df[feature_columns],[new_listing_value[feature_columns]])
temp_df = temp_df.sort_values(‘distance’)
knn_5 = temp_df.price.iloc[:5]
predicted_price = knn_5.mean()
return predicted_price_multivariante
cols = [‘guests’, ‘reviews’]
norm_test_df[‘predicted_price’] = norm_test_df[cols].apply(predicted_price_multivariante,feature_columns=cols,axis=1)
norm_test_df[‘squared_error’] = (norm_test_df[‘predicted_price’] - norm_test_df[‘price’])**(2)
mse = norm_test_df[‘squared_error’].mean()
rmse = mse ** (1/2)

Error TypeError: unsupported operand type(s) for -: ‘function’ and ‘float’

->10 norm_test_df[‘squared_error’] = (norm_test_df[‘predicted_price’] - norm_test_df[‘price’])**(2)

OR Error: predicted_price is not defined, but it is defined

Hi Ariana,

you seem to subtract a float from a function - which leads to an error message…

You might want to insert a line printing out the types, alike:

print( type(norm_test_df[‘predicted_price’]), type(norm_test_df[‘price’]) )

before that and check, whether both terms fit together. The error message seems to say, that one of both if not a number…

Cheers, Dominik