# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# 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" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
data= pd.read_csv('/kaggle/input/fraudulent-claim-on-cars-physical-damage/training data/training data.csv')
data.head()
data.isna().sum()
data.marital_status.dtypes
data.marital_status = data.marital_status.fillna(0.00)
data.isna().sum()
data.witness_present_ind = data.witness_present_ind.fillna(data.witness_present_ind.mean())
data.claim_est_payout.dtypes
data.claim_est_payout = data.claim_est_payout.fillna(data.claim_est_payout.mean())
data.age_of_vehicle=data.age_of_vehicle.fillna(data.age_of_vehicle.mean())
"""
**which gender has done  more fraud ?**
"""
data[data.fraud== 1].groupby('gender').count()['fraud'].sort_values(ascending = False)
"""
**How many people with annual income greater than 40000 did fraud claims ?**
"""
data.fraud.dtypes
data[(data.marital_status == 1.0 )&( data.annual_income > 40000 ) & (data.fraud==1)].count()[0]
"""
**which accident site have more fraud claims when orderd online from a female driver with age greater than 40 ?**
"""
data[(data.channel== 'Online')&(data.gender == 'F')&(data.age_of_driver>40)& (data.fraud==1)].groupby('accident_site').count().iloc[:,0].sort_values(ascending = False)
y =  data.fraud

y.head()
data
data.iloc[:,10:-10]
data.dtypes
data.claim_date =  pd.to_datetime(data.claim_date)
data[['Month']] = pd.DataFrame(data.claim_date.dt.month)
data[['day']] = pd.DataFrame(data.claim_date.dt.day)
data[['year']] = pd.DataFrame(data.claim_date.dt.year)
data.drop(columns=["claim_date"],inplace=True)
data.isna().sum()
data.columns
data.iloc[:,10:-10]
data.head()
nom=[2,8,10,11,15,21]
ordi=[19]
from sklearn.preprocessing import OneHotEncoder , OrdinalEncoder
from sklearn.compose  import make_column_transformer

trans =  make_column_transformer((OneHotEncoder(sparse =False),nom),
                                (OrdinalEncoder(),ordi),
                                remainder='passthrough')
trans
from sklearn.neighbors import KNeighborsClassifier as knn 

model =  knn(n_neighbors = 5)

model
from sklearn.pipeline import make_pipeline
from sklearn import set_config

set_config(display = 'diagram')

pipe = make_pipeline(trans,model)

pipe
x = data.drop(columns =['fraud'])

x.head()
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size = 0.3)
pipe.fit(x_train,y_train)
pred= pipe.predict(x_test)
from sklearn.metrics import accuracy_score

accuracy_score(pred,y_test)*100