{"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","execution":{"iopub.status.busy":"2023-01-08T21:00:03.687424Z","iopub.execute_input":"2023-01-08T21:00:03.688326Z","iopub.status.idle":"2023-01-08T21:00:03.720987Z","shell.execute_reply.started":"2023-01-08T21:00:03.688224Z","shell.execute_reply":"2023-01-08T21:00:03.719701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/emsi-5iir-ds/train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:01:08.345798Z","iopub.execute_input":"2023-01-08T21:01:08.346369Z","iopub.status.idle":"2023-01-08T21:01:13.924960Z","shell.execute_reply.started":"2023-01-08T21:01:08.346325Z","shell.execute_reply":"2023-01-08T21:01:13.923639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train\nX = train.drop('id', axis=1, inplace=False)\nX.drop('f_27', axis=1, inplace=True)\nX.drop('target', axis=1, inplace=True)\nX.describe()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:01:47.459472Z","iopub.execute_input":"2023-01-08T21:01:47.459902Z","iopub.status.idle":"2023-01-08T21:01:48.860793Z","shell.execute_reply.started":"2023-01-08T21:01:47.459866Z","shell.execute_reply":"2023-01-08T21:01:48.859616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train['target']\ny.describe()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:02:07.271362Z","iopub.execute_input":"2023-01-08T21:02:07.271848Z","iopub.status.idle":"2023-01-08T21:02:07.319236Z","shell.execute_reply.started":"2023-01-08T21:02:07.271811Z","shell.execute_reply":"2023-01-08T21:02:07.317645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"existing_columns = X.columns\nfor (column) in existing_columns:\n    if (X[column].corr(train[\"target\"]) <= 0.04):\n        X.drop(column,inplace=True, axis=1)\n\nprint(\"Les Colonnes non-enlevés : \") \nfeatures = X.columns;\nfor (column) in X.columns:\n    print(column)\n\nprint(\"Nombre de colonnes : \" + str(X.columns.size))","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:02:43.398115Z","iopub.execute_input":"2023-01-08T21:02:43.398612Z","iopub.status.idle":"2023-01-08T21:02:44.510160Z","shell.execute_reply.started":"2023-01-08T21:02:43.398577Z","shell.execute_reply":"2023-01-08T21:02:44.508761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# hists = X.hist()\ncolumns_to_normalize =  [\"f_00\", \"f_01\", \"f_02\", \"f_05\", \"f_21\", \"f_22\", \"f_23\", \"f_24\", \"f_26\", \"f_28\"]\nto_normalize = X;\nfor column in X.columns :\n    if column not in columns_to_normalize:\n        to_normalize.drop(column, axis=1, inplace=True)\n\nto_normalize.hist()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:03:04.535511Z","iopub.execute_input":"2023-01-08T21:03:04.536035Z","iopub.status.idle":"2023-01-08T21:03:05.811404Z","shell.execute_reply.started":"2023-01-08T21:03:04.535988Z","shell.execute_reply":"2023-01-08T21:03:05.810236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.datasets import load_iris\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.neighbors import KNeighborsClassifier\n\ndef executeKNN(trainS,testS,K):\n    # Split the data into training and test sets\n    X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=trainS, test_size=testS)\n    # Create the KNN classifier\n    knn = KNeighborsClassifier(n_neighbors=K)\n\n    # Fit the classifier to the training data\n    knn.fit(X_train, y_train)\n\n    # Make predictions on the test data\n    y_pred = knn.predict(X_test)\n\n    # Calculate the accuracy of the predictions\n    accuracy = knn.score(X_test, y_test)\n    print(\"Accuracy:\", accuracy)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:03:38.468509Z","iopub.execute_input":"2023-01-08T21:03:38.468918Z","iopub.status.idle":"2023-01-08T21:03:39.124060Z","shell.execute_reply.started":"2023-01-08T21:03:38.468885Z","shell.execute_reply":"2023-01-08T21:03:39.122794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"executeKNN(0.3, 0.1, 7)\n","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:05:13.000018Z","iopub.execute_input":"2023-01-08T21:05:13.001104Z","iopub.status.idle":"2023-01-08T21:05:51.268801Z","shell.execute_reply.started":"2023-01-08T21:05:13.001056Z","shell.execute_reply":"2023-01-08T21:05:51.267960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/emsi-5iir-ds/train.csv')\ntest= pd.read_csv('/kaggle/input/emsi-5iir-ds/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:08:33.092258Z","iopub.execute_input":"2023-01-08T21:08:33.092797Z","iopub.status.idle":"2023-01-08T21:08:44.459384Z","shell.execute_reply.started":"2023-01-08T21:08:33.092759Z","shell.execute_reply":"2023-01-08T21:08:44.458127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train[features]\ny_train = train['target']\nX_test = test[features]","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:08:53.412241Z","iopub.execute_input":"2023-01-08T21:08:53.412692Z","iopub.status.idle":"2023-01-08T21:08:53.446111Z","shell.execute_reply.started":"2023-01-08T21:08:53.412653Z","shell.execute_reply":"2023-01-08T21:08:53.444915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.head()\n","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:09:07.187233Z","iopub.execute_input":"2023-01-08T21:09:07.187668Z","iopub.status.idle":"2023-01-08T21:09:07.205117Z","shell.execute_reply.started":"2023-01-08T21:09:07.187632Z","shell.execute_reply":"2023-01-08T21:09:07.203840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.head()\n","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:09:22.622734Z","iopub.execute_input":"2023-01-08T21:09:22.623183Z","iopub.status.idle":"2023-01-08T21:09:22.641078Z","shell.execute_reply.started":"2023-01-08T21:09:22.623145Z","shell.execute_reply":"2023-01-08T21:09:22.639626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.head()\n","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:09:37.658430Z","iopub.execute_input":"2023-01-08T21:09:37.658893Z","iopub.status.idle":"2023-01-08T21:09:37.667724Z","shell.execute_reply.started":"2023-01-08T21:09:37.658848Z","shell.execute_reply":"2023-01-08T21:09:37.666317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nmodel = KNeighborsClassifier(n_neighbors=7)\nmodel.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:09:54.760280Z","iopub.execute_input":"2023-01-08T21:09:54.760690Z","iopub.status.idle":"2023-01-08T21:09:57.994988Z","shell.execute_reply.started":"2023-01-08T21:09:54.760658Z","shell.execute_reply":"2023-01-08T21:09:57.993847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(X_test)\n","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:26:39.856236Z","iopub.execute_input":"2023-01-08T21:26:39.856686Z","iopub.status.idle":"2023-01-08T21:40:29.472537Z","shell.execute_reply.started":"2023-01-08T21:26:39.856650Z","shell.execute_reply":"2023-01-08T21:40:29.471289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submition = test[[\"id\"]]\nsubmition[\"target\"] = predictions\nsubmition.to_csv('submission.csv', index=True)\nsubmition","metadata":{"execution":{"iopub.status.busy":"2023-01-08T22:30:03.130785Z","iopub.execute_input":"2023-01-08T22:30:03.131246Z","iopub.status.idle":"2023-01-08T22:30:04.187681Z","shell.execute_reply.started":"2023-01-08T22:30:03.131211Z","shell.execute_reply":"2023-01-08T22:30:04.186864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import HTML\nimport pandas as pd\nimport numpy as np\n\ndef create_download_link(title = \"Download CSV file\", filename = \"data.csv\"):  \n    html = '<a href={filename}>{title}</a>'\n    html = html.format(title=title,filename=filename)\n    return HTML(html)\ncreate_download_link(filename='submission.csv')","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}