{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ntrain_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nprint(train_df.shape,test_df.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_y =train_df['label']\n\ntrain_x = train_df.drop([\"label\"],axis=1).values\ntest_x = test_df.values\n\nprint(train_y.shape)\nprint(train_x.shape)\nprint(test_x.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nimport matplotlib.pyplot as plt\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.preprocessing import StandardScaler\n%pylab inline\nfor i in range(1, 10): # loop that is necessary for making subplot\n    plt.subplot(3, 3, i) # subplot that is 3x3\n    imshow(train_x[i].reshape(28,28), cmap='gray') # showing image of the number\n    title('number ' + str(train_y[i])) # sh","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#SVM\n\nscaler = StandardScaler().fit(train_x) # scaling features\ntrain_x=train_x/255\ntest_x=test_x/255\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_val, y_train, y_val=train_test_split(train_x, train_y,test_size = 0.2, random_state = 2)\nprint('training set')\nprint(X_train.shape)\nprint(y_train.shape)\nprint('cross-validation test')\nprint(X_val.shape)\nprint(X_val.shape)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nfrom sklearn.svm import SVC\nsvm_model = SVC(kernel='linear') # choosing the model\nsvm_model.fit(X_train, y_train) # fitting the model to train set (from cell above)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ny_pred = svm_model.predict(X_val) \ny2_pred = svm_model.predict(test_x) \n\nfrom sklearn import metrics\nprint(\"accuracy y:\", metrics.accuracy_score(y_true=y_val, y_pred=y_pred), \"\\n\") \nprint(metrics.confusion_matrix(y_true=y_val, y_pred=y_pred), \"\\n\") ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nresults = pd.DataFrame(columns=[\"ImageId\",\"Label\"])\nresults.iloc[:,0] = list(range(1, len(test_x)+1))\nresults.iloc[:,1] = list(y2_pred)\nresults.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}