{"cells":[{"metadata":{},"cell_type":"markdown","source":"This is my first notebook! Like ever. Sounds like those cringy \"FIRST\" comments 🤦🏻‍♂️","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# Importing critical libraries\nimport pandas as pd\nimport numpy as np","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"So without further ado, I'll get to the point.\n\nFrom the looks of it, this is a supervised machine learning problem, since the data is labled. So, I'm going to solve it using different classification methods. I'm going to use KNN, SVM, etc and compare the results. Finally, I will submit the best result and improve upon it if possible.","execution_count":null},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# Importing train & test data\ntrain = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Data Visualization and Analysis\n%matplotlib inline \nimport matplotlib as mpl\nimport matplotlib.pyplot as plt\nmpl.style.use('ggplot') # optional: for ggplot-like style\n\ntrain['label'].value_counts(sort = False).plot(kind='bar')\n\nplt.xlabel('Digit') # add to x-label to the plot\nplt.ylabel('Number of instances') # add y-label to the plot\nplt.title('Digits in the training data') # add title to the plot\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Lets define feature sets, X_train:\nX_train = train.drop(['label'], axis = 1).values # to use scikit-learn library, we have to convert the Pandas data frame to a Numpy array.\nX_test = test.values\nprint(X_train.dtype) # checking data type.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Lets define our labels, y:\ny_train = train['label'].values\nprint(y_train.dtype)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Normalize Data\n# X_train = preprocessing.StandardScaler().fit(X_train).transform(X_train.astype(float))\n# I don't think this is necessary in this case since all our data is based on pixel brightness levels.","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Our feature set consists of 748 features! It takes a lot of time and processing power to fit a KNN model to it. So to counter this curse of dimensionality, we use the PCA spell 😋","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.decomposition import PCA\n\nn_components = 50\n# Finding the top n principal components in the data\npca_rand = PCA(n_components = n_components, svd_solver='randomized', whiten = True).fit(X_train)\n\n# Projecting the data onto the eigenspace\nX_train_pca = pca_rand.transform(X_train)\nX_test_pca = pca_rand.transform(X_test)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* ** K nearest neighbor (K-NN)**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# Classifier implementing the k-nearest neighbors vote.\nfrom sklearn.neighbors import KNeighborsClassifier\n\nn_neighbors = 1\nneigh = KNeighborsClassifier(n_neighbors = n_neighbors).fit(X_train,y_train)\noutput = neigh.predict(X_test)\noutput","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.DataFrame(data = output, columns=[\"Label\"])\nImageId = np.arange(1, len(submission.index) + 1)\nsubmission.insert(0, \"ImageId\", ImageId, True)\nsubmission.to_csv(\"submission.csv\", index = False)\n","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}