{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "8a37d8d9", "metadata": { "_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19", "_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5", "execution": { "iopub.execute_input": "2025-01-09T23:35:10.269591Z", "iopub.status.busy": "2025-01-09T23:35:10.269195Z", "iopub.status.idle": "2025-01-09T23:35:10.658005Z", "shell.execute_reply": "2025-01-09T23:35:10.656632Z" }, "papermill": { "duration": 0.395174, "end_time": "2025-01-09T23:35:10.659966", "exception": false, "start_time": "2025-01-09T23:35:10.264792", "status": "completed" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/kaggle/input/africa-gdp/Africa_GDP.csv\n" ] } ], "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", "\n", "import numpy as np # linear algebra\n", "import 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", "\n", "import os\n", "for 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" ] }, { "cell_type": "code", "execution_count": 2, "id": "3f865c92", "metadata": { "execution": { "iopub.execute_input": "2025-01-09T23:35:10.666585Z", "iopub.status.busy": "2025-01-09T23:35:10.666100Z", "iopub.status.idle": "2025-01-09T23:35:10.683517Z", "shell.execute_reply": "2025-01-09T23:35:10.682542Z" }, "papermill": { "duration": 0.022595, "end_time": "2025-01-09T23:35:10.685442", "exception": false, "start_time": "2025-01-09T23:35:10.662847", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "data = pd.read_csv('/kaggle/input/africa-gdp/Africa_GDP.csv')" ] }, { "cell_type": "code", "execution_count": 3, "id": "6079ebb6", "metadata": { "execution": { "iopub.execute_input": "2025-01-09T23:35:10.691660Z", "iopub.status.busy": "2025-01-09T23:35:10.691313Z", "iopub.status.idle": "2025-01-09T23:35:10.710088Z", "shell.execute_reply": "2025-01-09T23:35:10.709239Z" }, "papermill": { "duration": 0.024075, "end_time": "2025-01-09T23:35:10.712108", "exception": false, "start_time": "2025-01-09T23:35:10.688033", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "data.fillna(data.mean(),inplace=True)" ] }, { "cell_type": "code", "execution_count": 4, "id": "2683890c", "metadata": { "execution": { "iopub.execute_input": "2025-01-09T23:35:10.718525Z", "iopub.status.busy": "2025-01-09T23:35:10.718111Z", "iopub.status.idle": "2025-01-09T23:35:10.733589Z", "shell.execute_reply": "2025-01-09T23:35:10.732439Z" }, "papermill": { "duration": 0.020627, "end_time": "2025-01-09T23:35:10.735403", "exception": false, "start_time": "2025-01-09T23:35:10.714776", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "data.drop_duplicates(inplace=True)" ] }, { "cell_type": "code", "execution_count": 5, "id": "5eff6792", "metadata": { "execution": { "iopub.execute_input": "2025-01-09T23:35:10.741486Z", "iopub.status.busy": "2025-01-09T23:35:10.741132Z", "iopub.status.idle": "2025-01-09T23:35:12.076529Z", "shell.execute_reply": "2025-01-09T23:35:12.075251Z" }, "papermill": { "duration": 1.340317, "end_time": "2025-01-09T23:35:12.078262", "exception": false, "start_time": "2025-01-09T23:35:10.737945", "status": "completed" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Year Algeria Benin Botswana Burkina Faso Burundi \\\n", "0 1960 2.723615e+09 226195578.4 30411413.66 330442815.8 195999990.0 \n", "1 1961 2.434747e+09 235668220.5 32902612.87 350247234.3 202999992.0 \n", "2 1962 2.001445e+09 236434954.0 35644956.64 379567099.2 213500006.0 \n", "3 1963 2.702982e+09 253927697.3 38091842.85 394040667.1 232749998.0 \n", "4 1964 2.909316e+09 269819005.9 41616347.79 410321645.0 260750008.0 \n", "\n", " Cameroon Central African Republic Chad Eswatini ... \\\n", "0 614206068.5 112155598.5 313582728.1 35076845.97 ... \n", "1 652777608.3 123134583.5 333975336.1 43026042.79 ... \n", "2 694247864.4 124482773.8 357635713.4 45927961.63 ... \n", "3 718320845.0 129379123.8 371767002.2 54129438.35 ... \n", "4 776650176.9 142025078.7 392247517.7 64980554.01 ... \n", "\n", " Seychelles Sierra Leone Somalia South Africa Sudan \\\n", "0 12012024.62 322151470.6 180459936.8 8.748597e+09 1.127011e+09 \n", "1 11592023.76 327979248.4 191659914.4 9.225996e+09 1.223563e+09 \n", "2 12642025.92 342872712.4 203531927.5 9.813996e+09 1.329023e+09 \n", "3 13923028.54 348700653.6 216145935.9 1.085420e+10 1.352011e+09 \n", "4 15393031.56 372012091.5 229529912.7 1.195600e+10 1.389080e+09 \n", "\n", " Tanzania Togo Uganda Zambia Zimbabwe \n", "0 2.651730e+09 171057069.1 423008385.7 713000000.0 1.052990e+09 \n", "1 2.826179e+09 178497098.3 441524109.0 696285714.3 1.096647e+09 \n", "2 3.101590e+09 186745757.9 449012578.6 693142857.1 1.117602e+09 \n", "3 3.456579e+09 202305865.2 516147798.7 718714285.7 1.159512e+09 \n", "4 3.748841e+09 234572186.5 589056603.8 839428571.4 1.217138e+09 \n", "\n", "[5 rows x 34 columns]\n" ] } ], "source": [ "from sklearn.model_selection import train_test_split\n", "print(data.head())\n", "X = data.drop('Algeria',axis=1)\n", "y = data['Algeria']\n", "\n", "X_train, X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=42)\n" ] }, { "cell_type": "code", "execution_count": 6, "id": "4d2ecbca", "metadata": { "execution": { "iopub.execute_input": "2025-01-09T23:35:12.084723Z", "iopub.status.busy": "2025-01-09T23:35:12.084288Z", "iopub.status.idle": "2025-01-09T23:35:12.211636Z", "shell.execute_reply": "2025-01-09T23:35:12.210433Z" }, "papermill": { "duration": 0.132424, "end_time": "2025-01-09T23:35:12.213399", "exception": false, "start_time": "2025-01-09T23:35:12.080975", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/html": [ "
LinearRegression()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" ], "text/plain": [ "LinearRegression()" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.linear_model import LinearRegression\n", "from sklearn.metrics import mean_squared_error , mean_absolute_error, r2_score\n", "\n", "model = LinearRegression()\n", "model.fit(X_train,y_train)" ] }, { "cell_type": "code", "execution_count": 7, "id": "447cddae", "metadata": { "execution": { "iopub.execute_input": "2025-01-09T23:35:12.220052Z", "iopub.status.busy": "2025-01-09T23:35:12.219685Z", "iopub.status.idle": "2025-01-09T23:35:12.230343Z", "shell.execute_reply": "2025-01-09T23:35:12.229397Z" }, "papermill": { "duration": 0.015608, "end_time": "2025-01-09T23:35:12.231921", "exception": false, "start_time": "2025-01-09T23:35:12.216313", "status": "completed" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Mean Squared error:9.904758040019183e+19\n", "Mean Absolute Error:5989398763.378249\n", "r2:0.9849119641737768\n" ] } ], "source": [ "y_pred = model.predict(X_test)\n", "mse = mean_squared_error(y_test,y_pred)\n", "mae = mean_absolute_error(y_test,y_pred)\n", "r2 = r2_score(y_test,y_pred)\n", "print(f\"Mean Squared error:{mse}\")\n", "print(f\"Mean Absolute Error:{mae}\")\n", "print(f\"r2:{r2}\")" ] } ], "metadata": { "kaggle": { "accelerator": "none", "dataSources": [ { "datasetId": 6455551, "sourceId": 10415885, "sourceType": "datasetVersion" } ], "dockerImageVersionId": 30822, "isGpuEnabled": false, "isInternetEnabled": true, "language": "python", "sourceType": "notebook" }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.12" }, "papermill": { "default_parameters": {}, "duration": 4.759216, "end_time": "2025-01-09T23:35:12.854678", "environment_variables": {}, "exception": null, "input_path": "__notebook__.ipynb", "output_path": "__notebook__.ipynb", "parameters": {}, "start_time": "2025-01-09T23:35:08.095462", "version": "2.6.0" } }, "nbformat": 4, "nbformat_minor": 5 }