Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
792 B
2
Indexable
Never
# KNN REGRESSION
from sklearn.neighbors import KNeighborsRegressor
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error

# GENERATE SOME EXAMPLES DATA
X, y = make_regression(n_samples=100, n_features=10, noise=0.5 ,random_state=42)

# SPLIT THE DATA INTO TRAINING AND TESTING SETS
X_train, X_test, y_train ,y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# CREATE AND FIT THE KNN REGRESSION MODEL
k = 5 # NUMBER OF NEIGHBORS
knn =  KNeighborsRegressor (n_neighbors=k)
knn.fit (X_train, y_train)

# PREDICT ON THE TESTING SET
y_pred = knn.predict (X_test)

# CALCULATE MEAN SQUARED ERROR
mse = mean_squared_error(y_test, y_pred)
print("Mean Squared Error:", mse)
Leave a Comment