{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":84896,"databundleVersionId":10305135,"sourceType":"competition"}],"dockerImageVersionId":31234,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nimport matplotlib.pyplot as plt\nimport plotly.express as px\nfrom sklearn.preprocessing import MinMaxScaler, StandardScaler, RobustScaler , LabelEncoder , OneHotEncoder\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.neighbors import KNeighborsClassifier\nimport seaborn as sns\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.svm import SVR\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.metrics import mean_squared_error ,mean_absolute_error , r2_score","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:45:47.205333Z","iopub.execute_input":"2026-02-06T08:45:47.205969Z","iopub.status.idle":"2026-02-06T08:45:49.633836Z","shell.execute_reply.started":"2026-02-06T08:45:47.20594Z","shell.execute_reply":"2026-02-06T08:45:49.633033Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Step 1 - Investigating Data\n","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/playground-series-s4e12/train.csv\")\ndf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:45:49.635085Z","iopub.execute_input":"2026-02-06T08:45:49.635432Z","iopub.status.idle":"2026-02-06T08:45:54.73386Z","shell.execute_reply.started":"2026-02-06T08:45:49.635408Z","shell.execute_reply":"2026-02-06T08:45:54.733181Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cat = ['Gender' ,'Marital Status' , 'Education Level', 'Occupation', 'Location', 'Policy Type', 'Customer Feedback' , 'Smoking Status', 'Exercise Frequency','Property Type']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:45:54.734618Z","iopub.execute_input":"2026-02-06T08:45:54.734895Z","iopub.status.idle":"2026-02-06T08:45:54.738579Z","shell.execute_reply.started":"2026-02-06T08:45:54.734866Z","shell.execute_reply":"2026-02-06T08:45:54.737909Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\ndf['Policy Start Date'] = pd.to_datetime(df['Policy Start Date'])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:45:54.740292Z","iopub.execute_input":"2026-02-06T08:45:54.740501Z","iopub.status.idle":"2026-02-06T08:45:55.079409Z","shell.execute_reply.started":"2026-02-06T08:45:54.740481Z","shell.execute_reply":"2026-02-06T08:45:55.078624Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = df.drop('id' , axis=1)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:45:55.080289Z","iopub.execute_input":"2026-02-06T08:45:55.080551Z","iopub.status.idle":"2026-02-06T08:45:55.25723Z","shell.execute_reply.started":"2026-02-06T08:45:55.08053Z","shell.execute_reply":"2026-02-06T08:45:55.256384Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#df = df.drop('Policy Start Date' , axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:45:55.258091Z","iopub.execute_input":"2026-02-06T08:45:55.258322Z","iopub.status.idle":"2026-02-06T08:45:55.261548Z","shell.execute_reply.started":"2026-02-06T08:45:55.258301Z","shell.execute_reply":"2026-02-06T08:45:55.260783Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for col in cat:\n    df[col] = df[col].astype('category')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:45:55.262306Z","iopub.execute_input":"2026-02-06T08:45:55.262508Z","iopub.status.idle":"2026-02-06T08:45:56.132786Z","shell.execute_reply.started":"2026-02-06T08:45:55.262489Z","shell.execute_reply":"2026-02-06T08:45:56.132204Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:45:56.13366Z","iopub.execute_input":"2026-02-06T08:45:56.133921Z","iopub.status.idle":"2026-02-06T08:45:56.170527Z","shell.execute_reply.started":"2026-02-06T08:45:56.133889Z","shell.execute_reply":"2026-02-06T08:45:56.170012Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for column in df.columns:\n    print(df[column].unique())\n    print(\"____________________________________\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:45:56.17142Z","iopub.execute_input":"2026-02-06T08:45:56.171656Z","iopub.status.idle":"2026-02-06T08:45:56.438Z","shell.execute_reply.started":"2026-02-06T08:45:56.171636Z","shell.execute_reply":"2026-02-06T08:45:56.437421Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:45:56.440208Z","iopub.execute_input":"2026-02-06T08:45:56.440488Z","iopub.status.idle":"2026-02-06T08:45:56.444824Z","shell.execute_reply.started":"2026-02-06T08:45:56.440468Z","shell.execute_reply":"2026-02-06T08:45:56.444239Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Step 2 - Calculate the percentaage of missing values.\n","metadata":{}},{"cell_type":"code","source":"for i in df.columns:\n    col_non = df[i].isnull().sum()\n    na_per  = (col_non / df.shape[0]) * 100\n    print(f\"missing in {i} is : {na_per.round(2)} % \")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:45:56.445632Z","iopub.execute_input":"2026-02-06T08:45:56.445945Z","iopub.status.idle":"2026-02-06T08:45:56.481806Z","shell.execute_reply.started":"2026-02-06T08:45:56.445908Z","shell.execute_reply":"2026-02-06T08:45:56.48127Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# لسا ماعالجنا الاصفار , لازم نعالجها","metadata":{}},{"cell_type":"code","source":"numerical_columns = df.select_dtypes(['float64' , 'int64']).columns\nnumerical_columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:45:56.516383Z","iopub.execute_input":"2026-02-06T08:45:56.516634Z","iopub.status.idle":"2026-02-06T08:45:56.553258Z","shell.execute_reply.started":"2026-02-06T08:45:56.516605Z","shell.execute_reply":"2026-02-06T08:45:56.552466Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def detect_outlier(column):\n    Q1 = np.quantile( column , 0.25)\n    Q3 = np.quantile( column , 0.75)\n    IQR = Q3 - Q1\n    Lower = Q1 - 1.5*IQR\n    Upper = Q3 + 1.5*IQR\n    Outlier = column[ (  ( column < Lower) |  (column > Upper ) ) ]\n    return Outlier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:45:56.554374Z","iopub.execute_input":"2026-02-06T08:45:56.554624Z","iopub.status.idle":"2026-02-06T08:45:56.559441Z","shell.execute_reply.started":"2026-02-06T08:45:56.554604Z","shell.execute_reply":"2026-02-06T08:45:56.558777Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"out = {}\nfor col in numerical_columns:\n    out[col] = detect_outlier(df[col])\n\nout\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:45:56.560493Z","iopub.execute_input":"2026-02-06T08:45:56.560771Z","iopub.status.idle":"2026-02-06T08:45:56.918806Z","shell.execute_reply.started":"2026-02-06T08:45:56.560737Z","shell.execute_reply":"2026-02-06T08:45:56.91804Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"out = pd.DataFrame(out)\nout","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:45:56.919766Z","iopub.execute_input":"2026-02-06T08:45:56.919999Z","iopub.status.idle":"2026-02-06T08:45:56.938121Z","shell.execute_reply.started":"2026-02-06T08:45:56.919977Z","shell.execute_reply":"2026-02-06T08:45:56.937522Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cor = df.corr(numeric_only=True)\ncor","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:45:56.938993Z","iopub.execute_input":"2026-02-06T08:45:56.939226Z","iopub.status.idle":"2026-02-06T08:45:57.235798Z","shell.execute_reply.started":"2026-02-06T08:45:56.939183Z","shell.execute_reply":"2026-02-06T08:45:57.235216Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"px.imshow(cor)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:45:57.236693Z","iopub.execute_input":"2026-02-06T08:45:57.236987Z","iopub.status.idle":"2026-02-06T08:45:59.922418Z","shell.execute_reply.started":"2026-02-06T08:45:57.236957Z","shell.execute_reply":"2026-02-06T08:45:59.921734Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = df.drop('Policy Start Date' , axis=1)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:45:59.923344Z","iopub.execute_input":"2026-02-06T08:45:59.923619Z","iopub.status.idle":"2026-02-06T08:45:59.959697Z","shell.execute_reply.started":"2026-02-06T08:45:59.92359Z","shell.execute_reply":"2026-02-06T08:45:59.958908Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"LE = LabelEncoder()\nfor i in cat:\n   df[i]= LE.fit_transform(df[i])\n\nprint(df.info())\ndf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:45:59.9607Z","iopub.execute_input":"2026-02-06T08:45:59.960995Z","iopub.status.idle":"2026-02-06T08:46:01.620292Z","shell.execute_reply.started":"2026-02-06T08:45:59.960953Z","shell.execute_reply":"2026-02-06T08:46:01.619651Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = df.drop('Premium Amount' , axis=1)\ny = df['Premium Amount']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:46:01.62114Z","iopub.execute_input":"2026-02-06T08:46:01.621402Z","iopub.status.idle":"2026-02-06T08:46:01.693516Z","shell.execute_reply.started":"2026-02-06T08:46:01.621381Z","shell.execute_reply":"2026-02-06T08:46:01.692936Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train , X_test , y_train, y_test = train_test_split(X ,y , test_size = 0.3 , random_state = 42 )\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:46:01.694404Z","iopub.execute_input":"2026-02-06T08:46:01.694681Z","iopub.status.idle":"2026-02-06T08:46:02.021548Z","shell.execute_reply.started":"2026-02-06T08:46:01.694653Z","shell.execute_reply":"2026-02-06T08:46:02.020981Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"scaler = MinMaxScaler()\nX_train_scaled = scaler.fit_transform(X_train)\nX_train_scaled","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:46:02.022359Z","iopub.execute_input":"2026-02-06T08:46:02.022607Z","iopub.status.idle":"2026-02-06T08:46:02.225198Z","shell.execute_reply.started":"2026-02-06T08:46:02.022575Z","shell.execute_reply":"2026-02-06T08:46:02.224617Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"LR = LinearRegression()\nLR","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:46:02.225964Z","iopub.execute_input":"2026-02-06T08:46:02.226232Z","iopub.status.idle":"2026-02-06T08:46:02.235685Z","shell.execute_reply.started":"2026-02-06T08:46:02.2262Z","shell.execute_reply":"2026-02-06T08:46:02.235151Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"LR.fit(X_train_scaled , y_train)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:46:02.236637Z","iopub.execute_input":"2026-02-06T08:46:02.236929Z","iopub.status.idle":"2026-02-06T08:46:02.287288Z","shell.execute_reply.started":"2026-02-06T08:46:02.236901Z","shell.execute_reply":"2026-02-06T08:46:02.286288Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_test_scaled = scaler.fit_transform(X_test)\nX_test_scaled","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:46:02.287755Z","iopub.status.idle":"2026-02-06T08:46:02.287982Z","shell.execute_reply.started":"2026-02-06T08:46:02.287871Z","shell.execute_reply":"2026-02-06T08:46:02.287884Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = LR.predict(X_test_scaled)\ny_pred","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:46:02.289899Z","iopub.status.idle":"2026-02-06T08:46:02.290135Z","shell.execute_reply.started":"2026-02-06T08:46:02.29002Z","shell.execute_reply":"2026-02-06T08:46:02.290033Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"LR_r2 = r2_score(y_test , y_pred)\nLR_r2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:46:02.291457Z","iopub.status.idle":"2026-02-06T08:46:02.291711Z","shell.execute_reply.started":"2026-02-06T08:46:02.291603Z","shell.execute_reply":"2026-02-06T08:46:02.291617Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"LR_MSE = mean_squared_error(y_test , y_pred)\nLR_MSE","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:46:02.293105Z","iopub.status.idle":"2026-02-06T08:46:02.2934Z","shell.execute_reply.started":"2026-02-06T08:46:02.293279Z","shell.execute_reply":"2026-02-06T08:46:02.293298Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import math\nmath.sqrt(LR_MSE)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:46:02.294614Z","iopub.status.idle":"2026-02-06T08:46:02.295204Z","shell.execute_reply.started":"2026-02-06T08:46:02.295066Z","shell.execute_reply":"2026-02-06T08:46:02.295083Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"LR_MAE = mean_absolute_error(y_test , y_pred)\nLR_MAE","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T08:46:02.295973Z","iopub.status.idle":"2026-02-06T08:46:02.296247Z","shell.execute_reply.started":"2026-02-06T08:46:02.296107Z","shell.execute_reply":"2026-02-06T08:46:02.296128Z"}},"outputs":[],"execution_count":null}]}