{"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 in \n\nimport numpy as np\nimport pandas as pd\nimport os\nimport matplotlib.pyplot as plt\nimport time \nfrom sklearn.svm import SVC\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn.decomposition import PCA\nfrom sklearn.preprocessing import StandardScaler\n\n%matplotlib inline\n\ntrain = pd.read_csv('../input/train.csv')\ntest = pd.read_csv(\"../input/test.csv\")\ntrain_data = train.values\nprint(\"Train Data Shape is: \",train_data.shape)\ntrain.head()\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_data = test.values\nprint(\"Test Data Shape is: \",test_data.shape)\ntest.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"X_train = train_data[:, 1:]\ny_train = train_data[:, 0]\nprint('Train Data shape: ', X_train.shape)\nprint('Label shape: ', y_train.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_test = test_data[:]\nprint('Test Data shape: ', X_test.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sc = StandardScaler()\nsc.fit(X_train)\nX_std = sc.transform(X_train)\nprint('Standard Data shape:', X_std.shape)\nX_std[:5, :]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_test_std = sc.transform(X_test)\nprint('Standard Test Data shape:', X_test_std.shape)\nX_test_std[:5, :]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def find_SVC_hyper_param(X, y):\n    param_grid = [\n        {\n            'C': [100, 10, 1],\n            # 'kernel': ['linear', 'poly', 'rbf', 'sigmoid'],\n            # 'degree': [3, 4, 5],\n            'gamma': ['auto', 0.1, 0.01, 0.001],\n            # 'tol': [1e-3, 1e-4, 1e-5],\n            # 'random_state': [0, 100, 200, 500]\n        },\n    ]\n    grid_search = GridSearchCV(SVC(), param_grid, n_jobs=-1, verbose=1, cv=5)\n    grid_search.fit(X, y)\n    print('best_SVC_score', grid_search.best_score_)\n    print('best_SVC_param', grid_search.best_params_)\n    return grid_search.best_estimator_","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pca = PCA(n_components=0.95)\npca.fit(X_std)\nX_std_pca = pca.transform(X_std)\nprint('Standard PCA Data shape: ', X_std_pca.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_test_std_pca = pca.transform(X_test_std)\nprint('Standard PCA Test Data shape: ', X_test_std_pca.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"start_time = time.time()\nbest_svc = find_SVC_hyper_param(X_std_pca, y_train)\nelapsed_time = time.time() - start_time\nprint('best_SVC_estimator', best_svc)\nprint(\"Time consumed to find hyper param: \",time.strftime(\"%H:%M:%S\", time.gmtime(elapsed_time)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_predict = best_svc.predict(X_test_std_pca)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submissions=pd.DataFrame({\"ImageId\": list(range(1,len(y_predict)+1)),\n                         \"Label\": y_predict})\nsubmissions.to_csv(\"PCA95-SVM.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":1}