{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"import sys\nprint(sys.executable)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Installing RAPIDS by Nvidia"},{"metadata":{"trusted":true},"cell_type":"code","source":"import sys\n!cp ../input/rapids/rapids.0.17.0 /opt/conda/envs/rapids.tar.gz\n!cd /opt/conda/envs/ && tar -xzvf rapids.tar.gz > /dev/null\nsys.path = [\"/opt/conda/envs/rapids/lib/python3.7/site-packages\"] + sys.path\nsys.path = [\"/opt/conda/envs/rapids/lib/python3.7\"] + sys.path\nsys.path = [\"/opt/conda/envs/rapids/lib\"] + sys.path \n!cp /opt/conda/envs/rapids/lib/libxgboost.so /opt/conda/lib/","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Import necessary modules"},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\n\n## cuDF Dataframe \nimport cudf\n\n## cuML machine learning\nfrom cuml.preprocessing.model_selection import train_test_split\nfrom cuml.neighbors import KNeighborsClassifier, NearestNeighbors\nfrom cuml.metrics.accuracy import accuracy_score\n\n## Plotting charts\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Loading the dataset\ndigits dataset: digits"},{"metadata":{"trusted":true},"cell_type":"code","source":"training = cudf.read_csv(\"../input/digit-recognizer/train.csv\")\ntraining.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"training.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Denotes a 28x28 image contained from columns pixel0 to pixel783 "},{"metadata":{},"cell_type":"markdown","source":"Display img data "},{"metadata":{"trusted":true},"cell_type":"code","source":"# VISUALIZE DATA\nsamples = training.iloc[5000:5030,1:].to_pandas().values\nplt.figure(figsize=(15,4.5))\nfor i in range(30):  \n    plt.subplot(3, 10, i+1)\n    plt.imshow(samples[i].reshape((28,28)),cmap=plt.cm.binary)\n    plt.axis('off')\nplt.subplots_adjust(wspace=-0.1, hspace=-0.1)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def split_df(df):\n    X = df.drop('label', axis = 1).values\n    y = df['label'].values\n    return(X,y)\nX, y = split_df(training)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Modelling "},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(training, 'label',train_size=0.8, random_state=193, stratify=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"scores = {}\nk_range = range(1,10)\nfor i,k in enumerate(k_range):\n    knn = KNeighborsClassifier(n_neighbors = k)\n    knn.fit(X_train, y_train)\n    y_pred = knn.predict(X_test)\n    scores[i] = accuracy_score(y_test,y_pred)\nscores_values = [x for x in scores.values()]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.clf()\nplt.plot(k_range,scores_values)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Using KNN neighbours = 3"},{"metadata":{"scrolled":true,"trusted":true},"cell_type":"code","source":"knn = KNeighborsClassifier(n_neighbors=3)\nknn.fit(X_train, y_train)","execution_count":null,"outputs":[]},{"metadata":{"scrolled":true,"trusted":true},"cell_type":"code","source":"y_pred = knn.predict(X_test)\nprint(accuracy_score(y_pred, y_test))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"Accuracy on the training data: {}%\".format(round(knn.score(X_train, y_train)*100,2)))\nprint(\"Accuracy on the testing data: {}%\".format(round(knn.score(X_test, y_test)*100,2)))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Predicting on the test dataset"},{"metadata":{"trusted":true},"cell_type":"code","source":"testing = cudf.read_csv(\"../input/digit-recognizer/test.csv\")\ntesting.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred = knn.predict(testing)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"id_ = cudf.Series(np.linspace(1,28000,28000)).astype('int32')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_dict = {\n    'ImageId':id_,\n    'Label':pred\n}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_file = cudf.DataFrame(df_dict)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_file.to_csv('submission.csv', index = False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}