text_plain_output_1.png
from sklearn.metrics import mean_squared_error, r2_score

from sklearn.model_selection import cross_val_score

from sklearn.neighbors import KNeighborsRegressor

import numpy as np

import pandas as pd

income_data = pd.read_csv('../input/adult-income-dataset/adult.csv')

income_data.isnull().sum()

income_data.isin(['?']).sum()

income_data['workclass'] = income_data['workclass'].replace('?', np.nan)
income_data['occupation'] = income_data['occupation'].replace('?', np.nan)
income_data['native-country'] = income_data['native-country'].replace('?', np.nan)

x_train.shape

y_train.shape

knn_model = KNeighborsRegressor(n_neighbors=5, weights='uniform', algorithm='auto', leaf_size=30, p=2, metric='minkowski', metric_params=None, n_jobs=None)

model = knn_model.fit(x_train, y_train)

y_pred = model.predict(x_test)

np.sqrt(mean_squared_error(y_test, y_pred))

np.sqrt(mean_squared_error(y_train, model.predict(x_train)))

for k in range(7):
    k = k + 1
    k_model = KNeighborsRegressor(n_neighbors=k).fit(x_train, y_train)
    y_pred = k_model.predict(x_train)
    fehler = np.sqrt(mean_squared_error(y_train, y_pred))

for k in range(7):
    k = k + 1
    k_model = KNeighborsRegressor(n_neighbors=k).fit(x_train, y_train)
    y_pred = k_model.predict(x_test)
    fehler = np.sqrt(mean_squared_error(y_test, y_pred))

knn_werte = {'n_neighbors': np.arange(1, 7, 1)}

model = KNeighborsRegressor(n_neighbors=6)

model_knn = model.fit(x_train, y_train)

np.sqrt(-1 * cross_val_score(model_knn, x_train, y_train, cv=10, scoring='neg_mean_squared_error')).mean()