ID Ever_Married    Graduated   Gender  Profession  Spending_Score  Segmentation    Family_Size Age Work_Experience
0   462809  0   0   1   5   2   3   3   4   1
1   462643  1   1   0   2   0   0   2   18  15
2   466315  1   1   0   2   2   1   0   44  1
3   461735  1   1   1   7   1   1   1   44  0
4   462669  1   1   0   3   1   0   5   20  15
... ... ... ... ... ... ... ... ... ... ...
8063    464018  0   0   1   9   2   3   6   4   0
8064    464685  0   0   1   4   2   3   3   15  3
8065    465406  0   1   0   5   2   3   0   14  1
8066    467299  0   1   0   5   2   1   3   8   1
8067    461879  1   1   1   4   0   1   2   17  0
8068 rows × 10 columns
data1=data.drop(["ID","Segmentation"],axis=1)

from sklearn.model_selection import train_test_split
     x_train,x_test,y_train,y_test=train_test_split(data1,data.Segmentation,test_size=0.20,random_state=50)

 from sklearn.neighbors import KNeighborsClassifier
 knn=KNeighborsClassifier(n_neighbors=17)
 knn.fit(x_train,y_train)
 tahmin=knn.predict(x_test)

 knn.score(x_test,y_test)
 #0.4838909541511772
 knn.predict([[1,1,0,2,0,2,18,15]])
 UserWarning: X does not have valid feature names, but KNeighborsClassifier was fitted with feature names
  #warnings.warn(
array([1])

When I make a prediction, I was not expecting this warning.

1

There are 1 best solutions below

0
Rutvi Rajesh On

This warning came because there's a mismatch between the presence of feature names in your training and testing data. When you trained the KNN model (knn.fit(x_train, y_train)), the x_train DataFrame probably had column names (e.g., 'Ever_Married', 'Graduated', etc.) but when you made the prediction (knn.predict([[1,1,0,2,0,2,18,15]])), the input data to predict on doesn't have those names.

prediction_data = pd.DataFrame([[1, 1, 0, 2, 0, 2, 18, 15]], columns=['provide list of columns here'])
prediction = knn.predict(prediction_data)
print(prediction)