text_plain_output_1.png
from sklearn.metrics import accuracy_score

from sklearn.model_selection import GridSearchCV

from sklearn.model_selection import KFold

from sklearn.model_selection import cross_val_score

from sklearn.model_selection import train_test_split

from sklearn.neighbors import KNeighborsClassifier

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

vodafone_subset_6 = pd.read_csv('../input/vodafone6nm/vodafone-subset-6.csv')

df = vodafone_subset_6[['target', 'ROUM', 'phone_value', 'DATA_VOLUME_WEEKDAYS', 'DATA_VOLUME_WEEKENDS', 'device_brand', 'software_os_vendor', 'software_os_name', 'software_os_version', 'device_type_rus', 'AVG_ARPU', 'lifetime', 'how_long_same_model', 'ecommerce_score', 'banks_sms_count', 'instagram_volume', 'viber_volume', 'linkedin_volume', 'tinder_volume', 'telegram_volume', 'google_volume', 'whatsapp_volume', 'youtube_volume']]

df_1 = pd.get_dummies(df, columns=['phone_value', 'device_brand', 'software_os_vendor', 'software_os_name', 'software_os_version', 'device_type_rus'])

df_1.dtypes.value_counts()

df_2 = df_1.dropna()
df_2.shape

X = df_1.drop('target', axis=1)
y = df_1['target']
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.25, random_state=1)

knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train, y_train)

y_pred = knn.predict(X_valid)

kf = KFold(n_splits=5, shuffle=True, random_state=22)
model_knn = KNeighborsClassifier(n_neighbors=5)
model_knn.fit(X_train, y_train)
y_pred = model_knn.predict(X_valid)
accuracy_score(y_pred, y_valid)

kf = KFold(n_splits=5, shuffle=True, random_state=22)
model_knn = KNeighborsClassifier(n_neighbors=5)
scores = cross_val_score(model_knn, X, y, cv=kf, scoring='accuracy')
mean_score = scores.mean()

knn_params = {'n_neighbors': np.arange(1, 51)}
knn_grid = GridSearchCV(model_knn, knn_params, scoring='accuracy', cv=kf)
knn_grid.fit(X_train, y_train)

knn_grid.best_estimator_

knn_grid.best_score_

knn_grid.best_params_