<jupyter_start><jupyter_text>Logistic Regression<jupyter_code># import libraries
import numpy as np
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split,cross_val_score
from sklearn import metrics
from sklearn.metrics import confusion_matrix, classification_report
# read the data using read_csv
df = pd.read_csv('Operation Reimagine_Airline Passenger Satisfaction Dataset.csv')
# drop the first column
df.drop(columns=df.columns[0], inplace=True)
df.head(3)
df.info()
# convert categorical labels to numeric labels
df['Satisfaction_lvl'] = df['satisfaction'].apply(lambda x: 1 if x == 'satisfied' else 0)
df['Gender_type'] = df['Gender'].apply(lambda x: 1 if x == 'Male' else 0)
df['Customer_type_satis'] = df['Customer Type'].apply(lambda x: 1 if x == 'Loyal Customer' else 0)  
df['Travel_type'] = df['Type of Travel'].apply(lambda x: 1 if x == 'Personal Travel' else 0)


def class_transform(x):
    if x == 'Business':
        return 0
    elif x == 'Eco':
        return 1
    else:
        return 2

df['Class_type'] = df['Class'].apply(class_transform)
# drop na
sub_df = df.dropna()
# define x and y
features = ['Age', 'Flight Distance', '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', 'Departure Delay in Minutes', 'Arrival Delay in Minutes',
             'Gender_type', 'Customer_type_satis', 'Travel_type', 'Class_type']

y = sub_df['Satisfaction_lvl']
X = sub_df[features]
# data split
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0, test_size=0.2)
# initiate LogisticRegression method
model = LogisticRegression(max_iter=100)
# train the model using X and y training data
log_model = model.fit(X_train, y_train)
log_model.intercept_
# create a dataframe displaying the features and its coefficient
coef = pd.DataFrame(log_model.coef_).transpose() 
coef.columns =['coefficient']
coef['feature'] = features
coef = coef[['feature', 'coefficient']]
coef
# predict the test data - label
prediction_label = log_model.predict(X_test)
print(classification_report(y_test, prediction_label, target_names=['neutral or dissatisfied', 'satisfied']))<jupyter_output>precision    recall  f1-score   support

neutral or dissatisfied       0.86      0.91      0.88      2879
              satisfied       0.87      0.82      0.85      2300

               accuracy                           0.87      5179
              macro avg       0.87      0.86      0.87      5179
           weighted avg       0.87      0.87      0.87      5179<jupyter_text>Visualisation<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt; 
plt.rcdefaults()
import numpy as np
from matplotlib import rcParams
# figure size in inches
rcParams['figure.figsize'] = 8,3
 
objects = ('Inflight wifi service', 'Class', 'Online boarding', 'Customer type', 'Type of travel')
x_pos = np.arange(len(objects))
performance = [0.429, 0.466, 0.599, 2.068, 2.854]

plt.barh(x_pos, performance, align='center', alpha=0.5, color='dimgrey') 
plt.yticks(x_pos, objects)
plt.xticks([0, 1, 2, 3, 4], labels="") 
plt.title('Top 5 Drivers of Satisfaction (Feature Importance)', fontweight="bold")

for index, value in enumerate(performance):
    plt.text(value, index, str(value))  

plt.show()
%matplotlib inline
import matplotlib.pyplot as plt; 
plt.rcdefaults()
import numpy as np
from matplotlib import rcParams
# figure size in inches
rcParams['figure.figsize'] = 8,3
 
objects = ('Personal travel', 'Business travel')
x_pos = np.arange(len(objects))
performance = [10, 59]

plt.barh(x_pos, performance, align='center', alpha=0.5, color='green') 
plt.yticks(x_pos, objects)
plt.xticks([0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100], labels="") 
plt.title('Business Travellers are More Satisfied than Personal Travellers \n \n To Focus on Strengthening Satisfaction of Business Travellers', fontweight="bold")

for index, value in enumerate(performance):
    plt.text(value, index, str(value))

plt.show()
%matplotlib inline
import matplotlib.pyplot as plt; 
plt.rcdefaults()
import numpy as np
from matplotlib import rcParams
# figure size in inches
rcParams['figure.figsize'] = 8,3
 
objects = ('Disloyal customer', 'Loyal customer')
x_pos = np.arange(len(objects))
performance = [25, 48]

plt.barh(x_pos, performance, align='center', alpha=0.5, color='green') 
plt.yticks(x_pos, objects)
plt.xticks([0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100], labels="") 
plt.title('Loyal Customers are More Satisfied than Disloyal Customers \n \n To Focus on Tailoring Incentives for Loyalty Program', fontweight="bold")

for index, value in enumerate(performance):
    plt.text(value, index, str(value))

plt.show()
%matplotlib inline
import matplotlib.pyplot as plt; 
plt.rcdefaults()
import numpy as np
from matplotlib import rcParams
# figure size in inches
rcParams['figure.figsize'] = 8,3
 
objects = ('Neutral/dissatisfied with online boarding', 'Satisfied with online boarding') 
x_pos = np.arange(len(objects))
performance = [15, 72]

plt.barh(x_pos, performance, align='center', alpha=0.5, color='green') 
plt.yticks(x_pos, objects)
plt.xticks([0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100], labels="") 
plt.title('Customers Satisfied with Online Boarding Gave Higher Overall Satisfaction Ratings \n \n To Focus on Strengthening Online Boarding Experience', fontweight="bold", fontsize=10)

for index, value in enumerate(performance):
    plt.text(value, index, str(value))

plt.show()
%matplotlib inline
import matplotlib.pyplot as plt; 
plt.rcdefaults()
import numpy as np
from matplotlib import rcParams
# figure size in inches
rcParams['figure.figsize'] = 8,3
 
objects = ('Eco class', 'Eco Plus class', 'Business class')
x_pos = np.arange(len(objects))
performance = [19, 25, 70]

plt.barh(x_pos, performance, align='center', alpha=0.5, color='green') 
plt.yticks(x_pos, objects)
plt.xticks([0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100], labels="") 
plt.title('Business Class Travellers are More Satisfied than Other Classes \n \n To Focus on Strengthening Satisfaction of Business Class Travellers', fontweight="bold")

for index, value in enumerate(performance):
    plt.text(value, index, str(value))

plt.show()
%matplotlib inline
import matplotlib.pyplot as plt; 
plt.rcdefaults()
import numpy as np
from matplotlib import rcParams
# figure size in inches
rcParams['figure.figsize'] = 8,3
 
objects = ('Neutral/dissatisfied with inflight wifi service', 'Satisfied with inflight wifi service') 
x_pos = np.arange(len(objects))
performance = [31, 75]

plt.barh(x_pos, performance, align='center', alpha=0.5, color='green') 
plt.yticks(x_pos, objects)
plt.xticks([0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100], labels="") 
# plt.xlabel('Satisfaction')
plt.title("Customers Satisfied with Inflight Wifi Gave Higher Overall Satisfaction Ratings \n \n To Focus on Managing Customer's Expectations on Inflight Wifi", fontweight="bold", fontsize=11)

for index, value in enumerate(performance):
    plt.text(value, index, str(value))

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1241154055525313, 'fraction_numerical': 0.03606423516603157, 'mean_word_length': 4.614209320091673, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '525389', 'license': 'Unlicense', 'n_tokens_mistral': 2697, 'n_tokens_neox': 2379, 'n_words': 746, 'path': 'Logistics regression.ipynb', 'repo_name': 'winleexz/Logistics-Regression---Satisfaction-Data', 'size': '7348'}