import pandas as pd
# Specify the file path
file_path = 'Self_Master.csv'
# Read the CSV file into a DataFrame
df = pd.read_csv(file_path)
df
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsRegressor
from sklearn.metrics import mean_squared_error
import numpy as np
# Load the data into a DataFrame
file_path = 'Self_Master.csv'
df = pd.read_csv(file_path)
# Assuming 'target_variable' is the column you want to predict
target_variable = 'Total Single Unit Housing' # Replace with the actual column name
# Extract features (X) and target variable (y)
X = df.drop(columns=[target_variable])
y = df[target_variable]
# Split the data into training and testing sets (90% training, 10% testing, random state = 51)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=51)
# Create and train the KNN model
k_value = 3 # Choose an appropriate value for k
knn_model = KNeighborsRegressor(n_neighbors=k_value)
knn_model.fit(X_train, y_train)
# Make predictions on the test set
y_pred = knn_model.predict(X_test)
# Evaluate the model's performance using mean squared error
mse = mean_squared_error(y_test, y_pred)
rmse = np.sqrt(mse)
# Display the root mean squared error
print(f'Root Mean Squared Error: {rmse}')
knn_model