text_plain_output_1.png
from sklearn.metrics import mean_squared_error, r2_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)

income_data.income = [1 if each == '>50K' else 0 for each in income_data.income]
y = income_data.income
y

dup = income_data.duplicated().any()

income_data = income_data.drop_duplicates()

income_data.drop(['fnlwgt', 'native-country'], axis=1, inplace=True)

income_data = pd.get_dummies(income_data)
income_data

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))

model = KNeighborsRegressor(n_neighbors=6)

model_knn = model.fit(x_train, y_train)

y_pred = model.predict(x_test)

v_W = pd.DataFrame({'y_test': y_test, 'vorhergesagten Werte': y_pred})
v_W