# 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
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import accuracy_score, classification_report,f1_score,roc_auc_score
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
from sklearn.model_selection import KFold, cross_val_score,StratifiedKFold
from sklearn.model_selection import train_test_split, GridSearchCV,RandomizedSearchCV
from sklearn.ensemble import RandomForestClassifier
df = pd.read_csv("../input/airline-passenger-satisfaction/train.csv",index_col=0)
df.head()
df.info()
df.isna().sum()
df = df.dropna()
df.isna().sum()
df.select_dtypes(include="object").columns
cat = ['Gender', 'Customer Type', 'Type of Travel', 'Class', 'satisfaction']
df.describe()
df.columns
convert_list = ['Inflight wifi service',
       'Departure/Arrival time convenient', 'Ease of Online booking',
       'Gate location', 'Food and drink', 'Online boarding', 'Seat comfort',
       'Inflight entertainment', 'On-board service', 'Leg room service',
       'Baggage handling', 'Checkin service', 'Inflight service',
       'Cleanliness']
data = df.copy()
for i in convert_list:
  data[i] = data[i].astype("category") 
    
data.info()
data.nunique()
data.drop("id", inplace=True, axis = 1)
data1 = data.groupby("satisfaction").mean()
plt.figure(dpi=85)
plt.bar(data1.index,data1["Arrival Delay in Minutes"])
plt.xlabel("Satisfaction")
plt.ylabel("Avarage Minutes")
plt.title("Arrival Delay in Minutes")
plt.figure(dpi=80)
plt.bar(data1.index,data1["Departure Delay in Minutes"],color = "green")
plt.xlabel("Satisfaction")
plt.ylabel("Avarage Minutes")
plt.title("Departure Delay in Minutes")
plt.figure(dpi=85)
plt.bar(data1.index,data1["Age"],color = "r")
plt.xlabel("Satisfaction")
plt.ylabel("Age")
plt.title("Age vs Satisfaction")
plt.figure(dpi=85)
plt.bar(data1.index,data1["Flight Distance"],color = "green")
plt.xlabel("Satisfaction")
plt.ylabel("Distance")
plt.title("Flight Distance")
data.head()
data1 = data.groupby("Class").mean()

plt.bar(data1.index, data1.Age)
plt.ylabel("age")
data.head()
data.satisfaction.replace({"satisfied": 1, "neutral or dissatisfied" : 0},inplace = True)
data.satisfaction
sns.barplot(x="Class",y = "satisfaction",data = data)
plt.show()
"""
data1
"""
sns.barplot(x="Gender",y = "satisfaction",data = data)

sns.barplot(x="Type of Travel",y = "satisfaction",data = data)
data  = pd.get_dummies(data, columns = cat, drop_first = True)
df.value_counts().sum()
data.info()
data["Satisfaction"] = data.satisfaction_1
data= data.drop("satisfaction_1", axis = 1)
y = data["Satisfaction"]
data = data.drop("Satisfaction",axis = 1)
cols = data.columns
"""
# ****Feature Scaling****
"""
from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()

# scaler'ı train data üzerinde eğitelim ve 
# train datayı scale edelim
data = scaler.fit_transform(data)
data = pd.DataFrame(data = data, columns=cols)
data
"""
# **Train-Test Split**
"""
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(data, y, test_size=0.2, random_state=2)
y_train
"""
# **Modeling**
"""
from xgboost import XGBClassifier

estimator = XGBClassifier()
parameters = {
    'max_depth': range (6,36 , 6),
    'n_estimators': range(10, 50, 10),
    'learning_rate': [0.3,0.5,0.8,1,2],
     'penalty':['l2', 'l1']
    
}
rsc = RandomizedSearchCV(estimator=estimator,param_distributions= parameters ,cv=4, n_iter = 6, verbose=1, scoring='roc_auc') 
rsc.fit(X_train,y_train)
rsc.best_params_
rsc.best_score_
xgb = XGBClassifier(
   max_depth= 24,
    n_estimators= 40,
    learning_rate= 0.3,
    penalty = "l2",
    random_state = 42
)
xgb.fit(X_train,y_train)
feature_important = xgb.get_booster().get_score(importance_type='weight')
keys = list(feature_important.keys())
values = list(feature_important.values())

data = pd.DataFrame(data=values, index=keys, columns=["score"]).sort_values(by = "score", ascending=False)
data.nlargest(50, columns="score").plot(kind='barh', figsize = (20,10)) ## plot top 40 features
y_pred = xgb.predict(X_test)
roc_auc_score(y_pred,y_test)