{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = pd.read_csv(\"../input/digit-recognizer/train.csv\")\nX = dataset.iloc[:, 1:].values # [all rows, everything after 1st column]\ny = dataset.iloc[:, 0].values # [all rows, only 1st column]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n# splitting the 'train.csv' into X_train, X_test, y_train, y_test\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\n\n#standardizing the features\nsc = StandardScaler()\n\nX_train = sc.fit_transform(X_train)\nX_test = sc.fit_transform(X_test)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.svm import SVC #SVM\n\n# using the classifier kernel SVM 'rbf' to train the features against the labels of X_train and y_train resp.\n\nclassifier = SVC(kernel = 'rbf')\nclassifier.fit(X_train, y_train)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#predicting against X_test\ny_pred = classifier.predict(X_test)\n\n#reshaping y_pred and y_test into n x 1 and concatenating them \nprint(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, accuracy_score\n# getting the confusion matrix\ncm = confusion_matrix(y_test, y_pred)\nprint(cm)\n\n#getting the accuarcy\naccuracy_score(y_test, y_pred)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#importing 'test.csv'\ntest_dataset = pd.read_csv(\"../input/digit-recognizer/test.csv\")\ntest_X = test_dataset.iloc[:, :].values","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#standardizing it with the same sc object\ny_results = sc.fit_transform(test_X)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#predicting it with the same classifier\ny_results = classifier.predict(y_results)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#reshaping it\ny_results.reshape(len(y_results), 1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# here's the function which I wrote to write the data column by column intsead of plain rows\n#ImageId, Label\n# 1        0\n\nimport csv \nheader = [\"ImageId\", \"Label\"]\n\ndef create_csv(results):\n with open('submission.csv', 'w', newline= '') as file:\n  id = 1\n  index = 0\n  \n  writer = csv.DictWriter(file, fieldnames = header) \n  writer.writeheader()\n  for i in range(0, len(results)):\n    writer.writerow({\n        header[0]: id,\n        header[1]: results[index]\n    })\n    id += 1\n    index += 1","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_csv(y_results)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}