{"cells":[{"metadata":{"_uuid":"546c81ae44d2679b802b43d24ea3752fe85c40bc"},"cell_type":"markdown","source":"## 1. Import des librairies"},{"metadata":{"trusted":true,"_uuid":"dceacc21aa80ea0aa7839f071f0896415a77d716"},"cell_type":"code","source":"import os\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport os\n# print(os.listdir(\"../input\"))\n%matplotlib inline\nsns.set({'figure.figsize':(16,8)})","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8a230358f1518a704e5c3e182606dbc30ea3f0a5"},"cell_type":"markdown","source":"## 2. Chargement et exploration du Dataset"},{"metadata":{"trusted":true,"_uuid":"c1539fc8fdd43fb770f96799ea4e2168b0a1186e"},"cell_type":"code","source":"train_data=pd.read_csv(\"../input/train.csv\")\ntest_data=pd.read_csv(\"../input/test.csv\")\n#train_data=pd.read_csv(\"/Users/mbp/Desktop/HETIC/KAGGLE/nyc-taxi-trip-duration/train.csv\")\n#test_data=pd.read_csv(\"/Users/mbp/Desktop/HETIC/KAGGLE/nyc-taxi-trip-duration/test.csv\")\n\ntrain_data.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"564fd62b632b74c684d3b9503e42e2da925a738f"},"cell_type":"code","source":"train_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cfd1c73a1dc19e50488be82baf8a5664fe13dbbd"},"cell_type":"code","source":"print(test_data.shape)\nprint(train_data.shape)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9ccb5a575811e764cd883f034afbe5681ec3c4de"},"cell_type":"markdown","source":"## 3. Verification des doublons"},{"metadata":{"trusted":true,"_uuid":"bfa00f09bc7310b76a46fbefc53073ac4cd5f2b5"},"cell_type":"code","source":"print(f\"Data Frame a {train_data.shape[0]} lignes et {train_data.shape[1]} colonnes\")\nprint(f\"La colonne d'id a {train_data.id.nunique()} de la valeur identique\")\ntrain_data.duplicated().sum()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"231243f53600d3e650503b0a3a32dcb9f091f76a"},"cell_type":"markdown","source":"On n'a aucun doublon dans le Dataset"},{"metadata":{"_uuid":"ebbb9b9e4e34bd11946954cfee59dd6f7e74bdea"},"cell_type":"markdown","source":"## 4. Verification des données manquantes"},{"metadata":{"trusted":true,"_uuid":"ae29f96dd7e5b01a5fda906e46539b191ea923d5"},"cell_type":"code","source":"train_data.isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"983c8ba3e3c4e9e579035728583f9c6ba38f1701"},"cell_type":"markdown","source":"## 5. Analyse des outliers"},{"metadata":{"trusted":true,"_uuid":"a65d038affea44e95dce0b736d560235c807b552"},"cell_type":"code","source":"fig, ax = plt.subplots(7, sharex=True)\nfor i,c in enumerate([\"vendor_id\",\"passenger_count\",\"pickup_longitude\",\"pickup_latitude\",\"dropoff_longitude\",\"dropoff_latitude\",\"trip_duration\"]):\n    sns.boxplot(train_data[c],ax=ax[i],width=1.5)\n    ax[i].set_xscale(\"log\")\n    ax[i].set_xlabel(\"\")\n    ax[i].set_ylabel(c, fontsize=15,rotation=45)\nfig.suptitle('Analyse des outliers', fontsize=20)\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"611891164a890387add7c8cbb331ffa5efbf7c95"},"cell_type":"markdown","source":"On a beaucoup de outliers dans des colonnes numériques, mais on ne pourrait pas les supprimer parce qu'ils influencent sur la prédiction du projet\n\n- De plus 'Trip duration' en compte le plus et c'est la feature qu'on cherche à prédire\n\n"},{"metadata":{"_uuid":"f529279e522d81f1c6c9125ed463ab75e3eba1ce"},"cell_type":"markdown","source":"## 6. Conversion des données et attribution des variables"},{"metadata":{"trusted":true,"_uuid":"4898684679a83091bca6a7790067072b825e8ff1"},"cell_type":"code","source":"train_data['pickup_datetime'] = pd.to_datetime(train_data['pickup_datetime'])\ntrain_data['dropoff_datetime'] = pd.to_datetime(train_data['dropoff_datetime'])\ntrain_data.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9892026317dd79ff2af5076e94833ecdfb5d826f"},"cell_type":"code","source":"train_data[\"store_and_fwd_flag\"]=np.where(train_data[\"store_and_fwd_flag\"] == \"N\",0,1)\ntrain_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"27406fc6e5cc6ccc9179696f092a71a86195fbc3"},"cell_type":"code","source":"X=train_data[['vendor_id',\n       'passenger_count', 'pickup_longitude', 'pickup_latitude',\n       'dropoff_longitude', 'dropoff_latitude']]\nX.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b139e1430a4f31c53fe7cc463f99a33d88cda82c"},"cell_type":"code","source":"y=train_data['trip_duration']\ny.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"686c0b1584c98f48dcc764ba13fbcf4ce912f210"},"cell_type":"markdown","source":"## 7. Train/Test Split"},{"metadata":{"trusted":true,"_uuid":"62fe2b6b6c6f1a01d36b5c0fbc0d76300ffd85dd"},"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor\nfrom sklearn.model_selection import train_test_split, cross_val_score;","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"39c305489607dc19a22dcef428a85b9f0bef9fac"},"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2, random_state=42)\nX_train.shape, y_train.shape, X_test.shape, y_test.shape\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"caed63d4cc3724ff3152e101e01f19c052e45e38"},"cell_type":"code","source":"rf = RandomForestRegressor(n_estimators=70)\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"08be7e0fc3d4e1bcf199f3141980b9c2b89752e5"},"cell_type":"markdown","source":"## 8. Modeling"},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"26b7b1b71d4818c7dc41904c470c9fdb3740af9b"},"cell_type":"code","source":"rf.fit(X_train,y_train)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c7849645f660315b7a675ad70e37466004d3fdea"},"cell_type":"markdown","source":"## 9. Prediction"},{"metadata":{"trusted":true,"_uuid":"f05415b4e8b581d5e238f0c1b0ae7fd1983db8fe"},"cell_type":"code","source":"pred = rf.predict(X_test)\npred","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"224bead5785aa99ddac174995aba5766d73d681c"},"cell_type":"code","source":"from sklearn.metrics import mean_squared_log_error ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"db268efe7def676d3e15bad1a545b5ad600d39ab"},"cell_type":"code","source":"print(mean_squared_log_error(y_test,pred))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d163362d570eeb1b90056fcebb265906c4c5276c"},"cell_type":"code","source":"submission =pd.read_csv(\"../input/sample_submission.csv\")\n#submission =pd.read_csv(\"/Users/mbp/Desktop/HETIC/KAGGLE/nyc-taxi-trip-duration/train.csv\")\nsubmission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"443dee95df76938a6af6a985cb620f1566cb2faa"},"cell_type":"code","source":"test_pred = rf.predict(test_data[['vendor_id',\n       'passenger_count', 'pickup_longitude', 'pickup_latitude',\n       'dropoff_longitude', 'dropoff_latitude']])\nprint(test_pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c90fd8fc5d1e96c10b59864fb2c07c747734c0b4"},"cell_type":"code","source":"submission['trip_duration'] = test_pred","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"321719a56d1c1a767c3d00508875ddb2a315b018"},"cell_type":"code","source":"submission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"48827ca2e97e73403230010539c54308169b6a68"},"cell_type":"code","source":"# my_submission.to_csv('/Users/mbp/Desktop/HETIC/KAGGLE/nyc-taxi-trip-duration/submission.csv', index=False)\nsubmission.to_csv('submission_file.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"a7f1a01e65f8ce631d316e226643a9ed9e88d0e5"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}