Reason:
The "KeyError" in machine learning typically occurs when a key (such as a column name or index) is not found within the specified dictionary, list, or DataFrame. It can happen in various situations during data preprocessing, training, or prediction phases.
The error message you encountered, "None of [Index(['name', 'company', 'fuel_type'], dtype='object')] are in the [columns]", suggests that the columns 'name', 'company', and 'fuel_type' are not present in the DataFrame you are trying to predict on.
To resolve this issue, you need to ensure that the columns in your prediction DataFrame match the expected columns of the model. Here are a few steps you can take to troubleshoot the problem:
Check the column names: Verify the column names expected by the model. This can be done by inspecting the training code or documentation related to the model. Confirm that the column names 'name', 'company', and 'fuel_type' are correct.
Validate the DataFrame structure: Verify the structure of the data DataFrame that you are using for prediction. Ensure that the DataFrame has the necessary columns and that the column names match the expected names. You can print the columns of the DataFrame using data.columns to double-check their names.
Verify the input data: Make sure that the values you pass to the DataFrame constructor are correct and correspond to the expected columns. Ensure that the keys in the dictionary match the column names, and the corresponding values are in the correct format.
Check for any preprocessing steps: If you are applying any preprocessing steps to the input data before prediction, such as encoding categorical variables, ensure that the preprocessing steps are performed correctly and consistently with how the model was trained.
my dataset is
pred=pipe.predict(pd.DataFrame(columns=['name','company','year','kms_driven','fuel_type'],data=np.array(['Maruti Suzuki Swift','Maruti',2019,100,'Petrol']).reshape(1,5)))
print('pred data x target')
print(pred)
company=form.cleaned_data['make']
car_model=form.cleaned_data['model']
year=int(form.cleaned_data['year'])
fuel_type=form.cleaned_data['fuel']
driven=float(form.cleaned_data['mileage'])
prediction=model.predict(pd.DataFrame(columns=['name','company','year','kms_driven','fuel_type'],data=np.array([company,car_model,year,driven,fuel_type]).reshape(1,5)))
company=form.cleaned_data['make'] output is Maruti is equal to name
so we change order
Solution
prediction=model.predict(pd.DataFrame(columns=['name','company','year','kms_driven','fuel_type'],data=np.array([car_model,company,year,driven,fuel_type]).reshape(1,5)))
Top comments (0)