test set using the train_test_split() function. The shuffle=True parameter indicates that the dataset is shuffled before the split. And the random_state=1 parameter controls the random number generator that is used for shuffling.
X_train, X_test, y_train, y_test = train_test_split(df[["total_bill"]], df["tip"], shuffle=True, random_state=1)
After that, we initialize the KNN regressor using the KNeighborsRegressor class. The n_neighbors parameter in the KneighborsRegressor() constructor indicates k or the number of nearest neighbors to select in the algorithm. Please note that a small value of k may introduce noise. And a very large value of k may make the boundaries between classes less distinct.
knn_regressor = KneighborsRegressor(n_neighbors=5)
After that, we use the fit() and predict() methods to learn from the data and predict the target values in the dataset.
knn_regressor.fit(X_train, y_train) y_test_pred = knn_regressor.predict(X_test)
Now, we can compare y_test_pred and y_test to measure the performance of the model. The output of the above program will be:
Mean Absolute Error: 0.8589836065573772 Root Mean Square Error: 1.2484401020972493








































0 Comments