<jupyter_start><jupyter_text>ANALYSIS<jupyter_code>#Filling the null values with the median, because the null values in them are low...

#df.fillna(df.median(), inplace=True)
#df.dropna(inplace=True)
df.isnull().sum()
def time_train(x):
    if x <= 15:
        return '0_early'
    return '1_late'
df['arrived'] = df['Arrive_Variance_Mins_arriv'].astype(float).apply(lambda x: time_train(x))
df['arrived'].value_counts()<jupyter_output><empty_output><jupyter_text>PREDICTORS<jupyter_code>df_predictors = df[['Train_Weight', 'Train_Length', 'wagon_count', 'Train_Distance_KM', 'month_arrival', 'weight_wagon', 'arrived']]
df_predictors.describe(include='all')
fig = plt.figure(figsize=(100, 100))
sns.pairplot(df_predictors, hue='arrived')
plt.tick_params(labelsize=25)
# Creating ranges of Train Weight

def weight_train(x):
    if x <= 1000:
        return '<1000'
    if 1000 < x and x <= 1250:
        return '1000-1250'
    if 1250 < x and x <= 1500:
        return '1250-1500'
    if 1500 < x and x <= 1750:
        return '1500-1750'
    return '>1750'

def length_train(x):
    if x <= 500:
        return '<500'
    if 500 < x and x <= 550:
        return '500-550'
    if 550 < x and x <= 650:
        return '550-650'
    return '>650'

def wagons_count(x):
    if x <= 15:
        return '<15'
    if 15 < x and x <= 25:
        return '15-25'    
    return '>25'

def weight_per_wagon(x):
    if x <= 40:
        return '<40'
    if 40 < x and x <= 60:
        return '40-60'
    if 60 < x and x <= 80:
        return '60-80'
    return '>80'

def distance_km(x):
    if x <= 200:
        return '<200'
    if 200 < x and x <= 500:
        return '200-500'
    return '>500'

df_predictors['weight_bin'] = df_predictors['Train_Weight'].apply(lambda x: weight_train(x))
df_predictors['length_bin'] = df_predictors['Train_Length'].apply(lambda x: length_train(x))
df_predictors['wagon_count_bin'] = df_predictors['wagon_count'].apply(lambda x: wagons_count(x))
df_predictors['weight_per_wagon_bin'] = df_predictors['weight_wagon'].apply(lambda x: weight_per_wagon(x))
df_predictors['distance_bin'] = df_predictors['Train_Distance_KM'].apply(lambda x: distance_km(x))
# Train Weight
df_predictors['weight_bin'] = pd.Categorical(df_predictors['weight_bin'],
                                                categories=['<1000', '1000-1250', '1250-1500', '1500-1750', '>1750'],
                                                ordered=True)


# Analysing statistical correlations between different parameters
plt.figure(figsize=(80,40))

dfP = pd.crosstab(index=df_predictors['weight_bin'], columns=df_predictors['arrived'])
dfP.plot(kind='bar')

plt.xticks(rotation=0)
plt.xlabel('Train Weight [t]', fontsize='large')
plt.tick_params(labelsize=10)
#plt.title("Choice vs. travel time")
   
    
L=plt.legend(loc='best', fontsize=10)
L.get_texts()[0].set_text('Non-delayed trains')
L.get_texts()[1].set_text('Delayed trains')


Ma = dfP.sum().sum()             #Ma = 8658 en caso de que quieran ver la comparacion con EL TOTAL DE TODA LA MUESTRA
plt.yticks(ticks = np.linspace(0,Ma, 21), labels=["{:.0f}%".format(i*100) for i in np.linspace(0, 1, 21)])
plt.margins(0,0.3)

plt.savefig("train_weight.png", dpi=400,  bbox_inches='tight')
plt.show();
# Train Length
df_predictors['length_bin'] = pd.Categorical(df_predictors['length_bin'],
                                                categories=['<500', '500-550', '550-650', '>650'],
                                                ordered=True)


# Analysing statistical correlations between different parameters
plt.figure(figsize=(80,40))

dfP = pd.crosstab(index=df_predictors['length_bin'], columns=df_predictors['arrived'])
dfP.plot(kind='bar')

plt.xticks(rotation=0)
plt.xlabel('Train Length [m]', fontsize='large')
plt.tick_params(labelsize=10)
#plt.title("Choice vs. travel time")
   
    
L=plt.legend(loc='best', fontsize=10)
L.get_texts()[0].set_text('Non-delayed trains')
L.get_texts()[1].set_text('Delayed trains')


Ma = dfP.sum().sum()             #Ma = 8658 en caso de que quieran ver la comparacion con EL TOTAL DE TODA LA MUESTRA
plt.yticks(ticks = np.linspace(0,Ma, 21), labels=["{:.0f}%".format(i*100) for i in np.linspace(0, 1, 21)])
plt.margins(0,0.3)

plt.savefig("train_length.png", dpi=400,  bbox_inches='tight')
plt.show();
# Number of wagons
df_predictors['wagon_count_bin'] = pd.Categorical(df_predictors['wagon_count_bin'],
                                                categories=['<15', '15-25', '>25'],
                                                ordered=True)

# Analysing statistical correlations between different parameters
plt.figure(figsize=(80,40))

dfP = pd.crosstab(index=df_predictors['wagon_count_bin'], columns=df_predictors['arrived'])
dfP.plot(kind='bar')

plt.xticks(rotation=0)
plt.xlabel('Wagon count', fontsize='large')
plt.tick_params(labelsize=10)
#plt.title("Choice vs. travel time")
   
    
L=plt.legend(loc='best', fontsize=10)
L.get_texts()[0].set_text('Non-delayed trains')
L.get_texts()[1].set_text('Delayed trains')


Ma = dfP.sum().sum()             #Ma = 8658 en caso de que quieran ver la comparacion con EL TOTAL DE TODA LA MUESTRA
plt.yticks(ticks = np.linspace(0,Ma, 21), labels=["{:.0f}%".format(i*100) for i in np.linspace(0, 1, 21)])
plt.margins(0,0.3)

plt.savefig("wagon_count.png", dpi=400,  bbox_inches='tight')
plt.show();
# Weight per wagon
df_predictors['weight_per_wagon_bin'] = pd.Categorical(df_predictors['weight_per_wagon_bin'],
                                                       categories=['<40', '40-60', '60-80', '>80'],
                                                       ordered=True)

# Analysing statistical correlations between different parameters
plt.figure(figsize=(80,40))

dfP = pd.crosstab(index=df_predictors['weight_per_wagon_bin'], columns=df_predictors['arrived'])
dfP.plot(kind='bar')

plt.xticks(rotation=0)
plt.xlabel('Weight per wagon', fontsize='large')
plt.tick_params(labelsize=10)
#plt.title("Choice vs. travel time")
   
    
L=plt.legend(loc='best', fontsize=10)
L.get_texts()[0].set_text('Non-delayed trains')
L.get_texts()[1].set_text('Delayed trains')


Ma = dfP.sum().sum()             #Ma = 8658 en caso de que quieran ver la comparacion con EL TOTAL DE TODA LA MUESTRA
plt.yticks(ticks = np.linspace(0,Ma, 21), labels=["{:.0f}%".format(i*100) for i in np.linspace(0, 1, 21)])
plt.margins(0,0.3)

plt.savefig("weight_per_wagon.png", dpi=400,  bbox_inches='tight')
plt.show();
# Distance
df_predictors['distance_bin'] = pd.Categorical(df_predictors['distance_bin'],
                                                categories=['<200', '200-500', '>500'],
                                                ordered=True)

# Analysing statistical correlations between different parameters
plt.figure(figsize=(80,40))

dfP = pd.crosstab(index=df_predictors['distance_bin'], columns=df_predictors['arrived'])
dfP.plot(kind='bar')

plt.xticks(rotation=0)
plt.xlabel('Travel distance [km]', fontsize='large')
plt.tick_params(labelsize=10)
#plt.title("Choice vs. travel time")
   
    
L=plt.legend(loc='best', fontsize=10)
L.get_texts()[0].set_text('Non-delayed trains')
L.get_texts()[1].set_text('Delayed trains')


Ma = dfP.sum().sum()             #Ma = 8658 en caso de que quieran ver la comparacion con EL TOTAL DE TODA LA MUESTRA
plt.yticks(ticks = np.linspace(0,Ma, 21), labels=["{:.0f}%".format(i*100) for i in np.linspace(0, 1, 21)])
plt.margins(0,0.3)

plt.savefig("travel_distance.png", dpi=400,  bbox_inches='tight')
plt.show();
# Month of arrival

df_predictors['month_arrival'] = pd.Categorical(df_predictors['month_arrival'],
                                                categories=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
                                                ordered=True)


plt.figure(figsize=(80,40))

dfP = pd.crosstab(index=df_predictors['month_arrival'], columns=df_predictors['arrived'])
dfP.plot(kind='bar')

plt.xticks(rotation=0)
plt.xlabel('Month of arrival', fontsize='large')
plt.tick_params(labelsize=10)
#plt.title("Choice vs. travel time")
   
    
L=plt.legend(loc='best', fontsize=10)
L.get_texts()[0].set_text('Non-delayed trains')
L.get_texts()[1].set_text('Delayed trains')


Ma = dfP.sum().sum()             #Ma = 8658 en caso de que quieran ver la comparacion con EL TOTAL DE TODA LA MUESTRA
plt.yticks(ticks = np.linspace(0,Ma, 21), labels=["{:.0f}%".format(i*100) for i in np.linspace(0, 1, 21)])
plt.margins(0,0.3)

plt.savefig("month.png", dpi=400,  bbox_inches='tight')
plt.show();<jupyter_output><empty_output><jupyter_text>Train weight in different ranges of train length<jupyter_code>#Filtering data for df_predictors_length_lower_500
df_predictors_length_lower_500 = df_predictors.copy()   # ranges: '<500', '500-550', '550-650', '>650'
df_predictors_length_lower_500 = df_predictors_length_lower_500.loc[(df_predictors_length_lower_500['Train_Length'] <500)].reset_index(drop=True)



# Train Weight
df_predictors_length_lower_500['weight_bin'] = pd.Categorical(df_predictors_length_lower_500['weight_bin'],
                                                              categories=['<1000', '1000-1250', '1250-1500', '1500-1750', '>1750'],
                                                              ordered=True)


# Analysing statistical correlations between different parameters
plt.figure(figsize=(80,40))

dfP = pd.crosstab(index=df_predictors_length_lower_500['weight_bin'], columns=df_predictors_length_lower_500['arrived'])
dfP.plot(kind='bar')

plt.xticks(rotation=0)
plt.xlabel('Train Weight [t]', fontsize='large')
plt.tick_params(labelsize=10)
#plt.title("Choice vs. travel time")
   
    
L=plt.legend(loc='best', fontsize=10)
L.get_texts()[0].set_text('Non-delayed trains')
L.get_texts()[1].set_text('Delayed trains')


Ma = dfP.sum().sum()             #Ma = 8658 en caso de que quieran ver la comparacion con EL TOTAL DE TODA LA MUESTRA
plt.yticks(ticks = np.linspace(0,Ma, 21), labels=["{:.0f}%".format(i*100) for i in np.linspace(0, 1, 21)])
plt.margins(0,0.3)

plt.savefig("train_weight_lower_500.png", dpi=400,  bbox_inches='tight')
plt.show();
#Filtering data for df_predictors_length_500_550
df_predictors_length_500_550 = df_predictors.copy()   # ranges: '<500', '500-550', '550-650', '>650'
df_predictors_length_500_550 = df_predictors_length_500_550.loc[(df_predictors_length_500_550['Train_Length'] >=500) & (df_predictors_length_500_550['Train_Length'] <550)].reset_index(drop=True)



# Train Weight
df_predictors_length_500_550['weight_bin'] = pd.Categorical(df_predictors_length_500_550['weight_bin'],
                                                              categories=['<1000', '1000-1250', '1250-1500', '1500-1750', '>1750'],
                                                              ordered=True)


# Analysing statistical correlations between different parameters
plt.figure(figsize=(80,40))

dfP = pd.crosstab(index=df_predictors_length_500_550['weight_bin'], columns=df_predictors_length_500_550['arrived'])
dfP.plot(kind='bar')

plt.xticks(rotation=0)
plt.xlabel('Train Weight [t]', fontsize='large')
plt.tick_params(labelsize=10)
#plt.title("Choice vs. travel time")
   
    
L=plt.legend(loc='best', fontsize=10)
L.get_texts()[0].set_text('Non-delayed trains')
L.get_texts()[1].set_text('Delayed trains')


Ma = dfP.sum().sum()             #Ma = 8658 en caso de que quieran ver la comparacion con EL TOTAL DE TODA LA MUESTRA
plt.yticks(ticks = np.linspace(0,Ma, 21), labels=["{:.0f}%".format(i*100) for i in np.linspace(0, 1, 21)])
plt.margins(0,0.3)

plt.savefig("df_predictors_length_500_550.png", dpi=400,  bbox_inches='tight')
plt.show();
#Filtering data for df_predictors_length_550_650
df_predictors_length_550_650 = df_predictors.copy()   # ranges: '<500', '500-550', '550-650', '>650'
df_predictors_length_550_650 = df_predictors_length_550_650.loc[(df_predictors_length_550_650['Train_Length'] >=550) & (df_predictors_length_550_650['Train_Length'] <650)].reset_index(drop=True)



# Train Weight
df_predictors_length_550_650['weight_bin'] = pd.Categorical(df_predictors_length_550_650['weight_bin'],
                                                              categories=['<1000', '1000-1250', '1250-1500', '1500-1750', '>1750'],
                                                              ordered=True)


# Analysing statistical correlations between different parameters
plt.figure(figsize=(80,40))

dfP = pd.crosstab(index=df_predictors_length_550_650['weight_bin'], columns=df_predictors_length_550_650['arrived'])
dfP.plot(kind='bar')

plt.xticks(rotation=0)
plt.xlabel('Train Weight [t]', fontsize='large')
plt.tick_params(labelsize=10)
#plt.title("Choice vs. travel time")
   
    
L=plt.legend(loc='best', fontsize=10)
L.get_texts()[0].set_text('Non-delayed trains')
L.get_texts()[1].set_text('Delayed trains')


Ma = dfP.sum().sum()             #Ma = 8658 en caso de que quieran ver la comparacion con EL TOTAL DE TODA LA MUESTRA
plt.yticks(ticks = np.linspace(0,Ma, 21), labels=["{:.0f}%".format(i*100) for i in np.linspace(0, 1, 21)])
plt.margins(0,0.3)

plt.savefig("df_predictors_length_550_650.png", dpi=400,  bbox_inches='tight')
plt.show();
#Filtering data for df_predictors_length_larger_650
df_predictors_length_larger_650 = df_predictors.copy()   # ranges: '<500', '500-550', '550-650', '>650'
df_predictors_length_larger_650 = df_predictors_length_larger_650.loc[(df_predictors_length_larger_650['Train_Length'] >650) ].reset_index(drop=True)



# Train Weight
df_predictors_length_larger_650['weight_bin'] = pd.Categorical(df_predictors_length_larger_650['weight_bin'],
                                                              categories=['<1000', '1000-1250', '1250-1500', '1500-1750', '>1750'],
                                                              ordered=True)


# Analysing statistical correlations between different parameters
plt.figure(figsize=(80,40))

dfP = pd.crosstab(index=df_predictors_length_larger_650['weight_bin'], columns=df_predictors_length_larger_650['arrived'])
dfP.plot(kind='bar')

plt.xticks(rotation=0)
plt.xlabel('Train Weight [t]', fontsize='large')
plt.tick_params(labelsize=10)
#plt.title("Choice vs. travel time")
   
    
L=plt.legend(loc='best', fontsize=10)
L.get_texts()[0].set_text('Non-delayed trains')
L.get_texts()[1].set_text('Delayed trains')


Ma = dfP.sum().sum()             #Ma = 8658 en caso de que quieran ver la comparacion con EL TOTAL DE TODA LA MUESTRA
plt.yticks(ticks = np.linspace(0,Ma, 21), labels=["{:.0f}%".format(i*100) for i in np.linspace(0, 1, 21)])
plt.margins(0,0.3)

plt.savefig("df_predictors_length_larger_650.png", dpi=400,  bbox_inches='tight')
plt.show();<jupyter_output><empty_output><jupyter_text>Weight per wagon in different months<jupyter_code>df_predictors.head()
# Filtering data for df_predictors_weight_wagon_Jan
df_predictors_weight_wagon_Jan = df_predictors.copy()
df_predictors_weight_wagon_Jan = df_predictors_weight_wagon_Jan.loc[(df_predictors_weight_wagon_Jan['month_arrival'] =='Jan')].reset_index(drop=True)



# Weight per wagon
df_predictors_weight_wagon_Jan['weight_per_wagon_bin'] = pd.Categorical(df_predictors_weight_wagon_Jan['weight_per_wagon_bin'],
                                                              categories=['<40', '40-60', '60-80', '>80'],
                                                              ordered=True)


# Analysing statistical correlations between different parameters
plt.figure(figsize=(80,40))

dfP = pd.crosstab(index=df_predictors_weight_wagon_Jan['weight_per_wagon_bin'], columns=df_predictors_weight_wagon_Jan['arrived'])
dfP.plot(kind='bar')

plt.xticks(rotation=0, fontsize=12)
plt.xlabel('Weight/wagon [t]', fontsize=15)
plt.tick_params(labelsize=10)
   
    
L=plt.legend(loc='best', fontsize=15)
L.get_texts()[0].set_text('Non-delayed trains')
L.get_texts()[1].set_text('Delayed trains')


Ma = dfP.sum().sum()             #Ma = 8658 en caso de que quieran ver la comparacion con EL TOTAL DE TODA LA MUESTRA
plt.yticks(ticks = np.linspace(0,Ma, 21), labels=["{:.0f}%".format(i*100) for i in np.linspace(0, 1, 21)])
plt.margins(0,0.3)
plt.xticks(rotation=0, fontsize=14)
plt.yticks(rotation=0, fontsize=14)
plt.savefig("weight_wagon_Jan.png", dpi=400,  bbox_inches='tight')
plt.show();
#Filtering data for df_predictors_weight_wagon_Feb
df_predictors_weight_wagon_Feb = df_predictors.copy()
df_predictors_weight_wagon_Feb = df_predictors_weight_wagon_Feb.loc[(df_predictors_weight_wagon_Feb['month_arrival'] =='Feb')].reset_index(drop=True)



# Weight per wagon
df_predictors_weight_wagon_Feb['weight_per_wagon_bin'] = pd.Categorical(df_predictors_weight_wagon_Feb['weight_per_wagon_bin'],
                                                              categories=['<40', '40-60', '60-80', '>80'],
                                                              ordered=True)


# Analysing statistical correlations between different parameters
plt.figure(figsize=(80,40))

dfP = pd.crosstab(index=df_predictors_weight_wagon_Feb['weight_per_wagon_bin'], columns=df_predictors_weight_wagon_Feb['arrived'])
dfP.plot(kind='bar')

plt.xticks(rotation=0, fontsize=12)
plt.xlabel('Weight/wagon [t]', fontsize=15)
plt.tick_params(labelsize=10)
   
    
L=plt.legend(loc='best', fontsize=15)
L.get_texts()[0].set_text('Non-delayed trains')
L.get_texts()[1].set_text('Delayed trains')


Ma = dfP.sum().sum()             #Ma = 8658 en caso de que quieran ver la comparacion con EL TOTAL DE TODA LA MUESTRA
plt.yticks(ticks = np.linspace(0,Ma, 21), labels=["{:.0f}%".format(i*100) for i in np.linspace(0, 1, 21)])
plt.margins(0,0.3)
plt.xticks(rotation=0, fontsize=14)
plt.yticks(rotation=0, fontsize=14)
plt.savefig("weight_wagon_Feb.png", dpi=400,  bbox_inches='tight')
plt.show();
#Filtering data for df_predictors_weight_wagon_Mar
df_predictors_weight_wagon_Mar = df_predictors.copy()
df_predictors_weight_wagon_Mar = df_predictors_weight_wagon_Mar.loc[(df_predictors_weight_wagon_Mar['month_arrival'] =='Mar')].reset_index(drop=True)



# Weight per wagon
df_predictors_weight_wagon_Mar['weight_per_wagon_bin'] = pd.Categorical(df_predictors_weight_wagon_Mar['weight_per_wagon_bin'],
                                                              categories=['<40', '40-60', '60-80', '>80'],
                                                              ordered=True)


# Analysing statistical correlations between different parameters
plt.figure(figsize=(80,40))

dfP = pd.crosstab(index=df_predictors_weight_wagon_Mar['weight_per_wagon_bin'], columns=df_predictors_weight_wagon_Mar['arrived'])
dfP.plot(kind='bar')

plt.xticks(rotation=0, fontsize=12)
plt.xlabel('Weight/wagon [t]', fontsize=15)
plt.tick_params(labelsize=10)
   
    
L=plt.legend(loc='best', fontsize=15)
L.get_texts()[0].set_text('Non-delayed trains')
L.get_texts()[1].set_text('Delayed trains')


Ma = dfP.sum().sum()             #Ma = 8658 en caso de que quieran ver la comparacion con EL TOTAL DE TODA LA MUESTRA
plt.yticks(ticks = np.linspace(0,Ma, 21), labels=["{:.0f}%".format(i*100) for i in np.linspace(0, 1, 21)])
plt.margins(0,0.3)
plt.xticks(rotation=0, fontsize=14)
plt.yticks(rotation=0, fontsize=14)
plt.savefig("weight_wagon_Mar.png", dpi=400,  bbox_inches='tight')
plt.show();
#Filtering data for df_predictors_weight_wagon_Apr
df_predictors_weight_wagon_Apr = df_predictors.copy()
df_predictors_weight_wagon_Apr = df_predictors_weight_wagon_Apr.loc[(df_predictors_weight_wagon_Apr['month_arrival'] =='Apr')].reset_index(drop=True)



# Weight per wagon
df_predictors_weight_wagon_Apr['weight_per_wagon_bin'] = pd.Categorical(df_predictors_weight_wagon_Apr['weight_per_wagon_bin'],
                                                              categories=['<40', '40-60', '60-80', '>80'],
                                                              ordered=True)


# Analysing statistical correlations between different parameters
plt.figure(figsize=(80,40))

dfP = pd.crosstab(index=df_predictors_weight_wagon_Apr['weight_per_wagon_bin'], columns=df_predictors_weight_wagon_Apr['arrived'])
dfP.plot(kind='bar')

plt.xticks(rotation=0, fontsize=12)
plt.xlabel('Weight/wagon [t]', fontsize=15)
plt.tick_params(labelsize=10)
   
    
L=plt.legend(loc='best', fontsize=15)
L.get_texts()[0].set_text('Non-delayed trains')
L.get_texts()[1].set_text('Delayed trains')


Ma = dfP.sum().sum()             #Ma = 8658 en caso de que quieran ver la comparacion con EL TOTAL DE TODA LA MUESTRA
plt.yticks(ticks = np.linspace(0,Ma, 21), labels=["{:.0f}%".format(i*100) for i in np.linspace(0, 1, 21)])
plt.margins(0,0.3)
plt.xticks(rotation=0, fontsize=14)
plt.yticks(rotation=0, fontsize=14)
plt.savefig("weight_wagon_Apr.png", dpi=400,  bbox_inches='tight')
plt.show();
#Filtering data for df_predictors_weight_wagon_May
df_predictors_weight_wagon_May = df_predictors.copy()
df_predictors_weight_wagon_May = df_predictors_weight_wagon_May.loc[(df_predictors_weight_wagon_May['month_arrival'] =='May')].reset_index(drop=True)



# Weight per wagon
df_predictors_weight_wagon_May['weight_per_wagon_bin'] = pd.Categorical(df_predictors_weight_wagon_May['weight_per_wagon_bin'],
                                                              categories=['<40', '40-60', '60-80', '>80'],
                                                              ordered=True)


# Analysing statistical correlations between different parameters
plt.figure(figsize=(80,40))

dfP = pd.crosstab(index=df_predictors_weight_wagon_May['weight_per_wagon_bin'], columns=df_predictors_weight_wagon_May['arrived'])
dfP.plot(kind='bar')

plt.xticks(rotation=0, fontsize=12)
plt.xlabel('Weight/wagon [t]', fontsize=15)
plt.tick_params(labelsize=10)
   
    
L=plt.legend(loc='best', fontsize=15)
L.get_texts()[0].set_text('Non-delayed trains')
L.get_texts()[1].set_text('Delayed trains')


Ma = dfP.sum().sum()             #Ma = 8658 en caso de que quieran ver la comparacion con EL TOTAL DE TODA LA MUESTRA
plt.yticks(ticks = np.linspace(0,Ma, 21), labels=["{:.0f}%".format(i*100) for i in np.linspace(0, 1, 21)])
plt.margins(0,0.3)
plt.xticks(rotation=0, fontsize=14)
plt.yticks(rotation=0, fontsize=14)
plt.savefig("weight_wagon_May.png", dpi=400,  bbox_inches='tight')
plt.show();
#Filtering data for df_predictors_weight_wagon_Jun
df_predictors_weight_wagon_Jun = df_predictors.copy()
df_predictors_weight_wagon_Jun = df_predictors_weight_wagon_Jun.loc[(df_predictors_weight_wagon_Jun['month_arrival'] =='Jun')].reset_index(drop=True)



# Weight per wagon
df_predictors_weight_wagon_Jun['weight_per_wagon_bin'] = pd.Categorical(df_predictors_weight_wagon_Jun['weight_per_wagon_bin'],
                                                              categories=['<40', '40-60', '60-80', '>80'],
                                                              ordered=True)


# Analysing statistical correlations between different parameters
plt.figure(figsize=(80,40))

dfP = pd.crosstab(index=df_predictors_weight_wagon_Jun['weight_per_wagon_bin'], columns=df_predictors_weight_wagon_Jun['arrived'])
dfP.plot(kind='bar')

plt.xticks(rotation=0, fontsize=12)
plt.xlabel('Weight/wagon [t]', fontsize=15)
plt.tick_params(labelsize=10)
   
    
L=plt.legend(loc='best', fontsize=15)
L.get_texts()[0].set_text('Non-delayed trains')
L.get_texts()[1].set_text('Delayed trains')


Ma = dfP.sum().sum()             #Ma = 8658 en caso de que quieran ver la comparacion con EL TOTAL DE TODA LA MUESTRA
plt.yticks(ticks = np.linspace(0,Ma, 21), labels=["{:.0f}%".format(i*100) for i in np.linspace(0, 1, 21)])
plt.margins(0,0.3)
plt.xticks(rotation=0, fontsize=14)
plt.yticks(rotation=0, fontsize=14)
plt.savefig("weight_wagon_Jun.png", dpi=400,  bbox_inches='tight')
plt.show();
#Filtering data for df_predictors_weight_wagon_Jul
df_predictors_weight_wagon_Jul = df_predictors.copy()
df_predictors_weight_wagon_Jul = df_predictors_weight_wagon_Jul.loc[(df_predictors_weight_wagon_Jul['month_arrival'] =='Jul')].reset_index(drop=True)



# Weight per wagon
df_predictors_weight_wagon_Jul['weight_per_wagon_bin'] = pd.Categorical(df_predictors_weight_wagon_Jul['weight_per_wagon_bin'],
                                                              categories=['<40', '40-60', '60-80', '>80'],
                                                              ordered=True)


# Analysing statistical correlations between different parameters
plt.figure(figsize=(80,40))

dfP = pd.crosstab(index=df_predictors_weight_wagon_Jul['weight_per_wagon_bin'], columns=df_predictors_weight_wagon_Jul['arrived'])
dfP.plot(kind='bar')

plt.xticks(rotation=0, fontsize=12)
plt.xlabel('Weight/wagon [t]', fontsize=15)
plt.tick_params(labelsize=10)
   
    
L=plt.legend(loc='best', fontsize=15)
L.get_texts()[0].set_text('Non-delayed trains')
L.get_texts()[1].set_text('Delayed trains')


Ma = dfP.sum().sum()             #Ma = 8658 en caso de que quieran ver la comparacion con EL TOTAL DE TODA LA MUESTRA
plt.yticks(ticks = np.linspace(0,Ma, 21), labels=["{:.0f}%".format(i*100) for i in np.linspace(0, 1, 21)])
plt.margins(0,0.3)
plt.xticks(rotation=0, fontsize=14)
plt.yticks(rotation=0, fontsize=14)
plt.savefig("weight_wagon_Jul.png", dpi=400,  bbox_inches='tight')
plt.show();
#Filtering data for df_predictors_weight_wagon_Aug
df_predictors_weight_wagon_Aug = df_predictors.copy()
df_predictors_weight_wagon_Aug = df_predictors_weight_wagon_Aug.loc[(df_predictors_weight_wagon_Aug['month_arrival'] =='Aug')].reset_index(drop=True)



# Weight per wagon
df_predictors_weight_wagon_Aug['weight_per_wagon_bin'] = pd.Categorical(df_predictors_weight_wagon_Aug['weight_per_wagon_bin'],
                                                              categories=['<40', '40-60', '60-80', '>80'],
                                                              ordered=True)


# Analysing statistical correlations between different parameters
plt.figure(figsize=(80,40))

dfP = pd.crosstab(index=df_predictors_weight_wagon_Aug['weight_per_wagon_bin'], columns=df_predictors_weight_wagon_Aug['arrived'])
dfP.plot(kind='bar')

plt.xticks(rotation=0, fontsize=12)
plt.xlabel('Weight/wagon [t]', fontsize=15)
plt.tick_params(labelsize=10)
   
    
L=plt.legend(loc='best', fontsize=15)
L.get_texts()[0].set_text('Non-delayed trains')
L.get_texts()[1].set_text('Delayed trains')


Ma = dfP.sum().sum()             #Ma = 8658 en caso de que quieran ver la comparacion con EL TOTAL DE TODA LA MUESTRA
plt.yticks(ticks = np.linspace(0,Ma, 21), labels=["{:.0f}%".format(i*100) for i in np.linspace(0, 1, 21)])
plt.margins(0,0.3)
plt.xticks(rotation=0, fontsize=14)
plt.yticks(rotation=0, fontsize=14)
plt.savefig("weight_wagon_Aug.png", dpi=400,  bbox_inches='tight')
plt.show();
#Filtering data for df_predictors_weight_wagon_Sep
df_predictors_weight_wagon_Sep = df_predictors.copy()
df_predictors_weight_wagon_Sep = df_predictors_weight_wagon_Sep.loc[(df_predictors_weight_wagon_Sep['month_arrival'] =='Sep')].reset_index(drop=True)



# Weight per wagon
df_predictors_weight_wagon_Sep['weight_per_wagon_bin'] = pd.Categorical(df_predictors_weight_wagon_Sep['weight_per_wagon_bin'],
                                                              categories=['<40', '40-60', '60-80', '>80'],
                                                              ordered=True)


# Analysing statistical correlations between different parameters
plt.figure(figsize=(80,40))

dfP = pd.crosstab(index=df_predictors_weight_wagon_Sep['weight_per_wagon_bin'], columns=df_predictors_weight_wagon_Sep['arrived'])
dfP.plot(kind='bar')

plt.xticks(rotation=0, fontsize=12)
plt.xlabel('Weight/wagon [t]', fontsize=15)
plt.tick_params(labelsize=10)
   
    
L=plt.legend(loc='best', fontsize=15)
L.get_texts()[0].set_text('Non-delayed trains')
L.get_texts()[1].set_text('Delayed trains')


Ma = dfP.sum().sum()             #Ma = 8658 en caso de que quieran ver la comparacion con EL TOTAL DE TODA LA MUESTRA
plt.yticks(ticks = np.linspace(0,Ma, 21), labels=["{:.0f}%".format(i*100) for i in np.linspace(0, 1, 21)])
plt.margins(0,0.3)
plt.xticks(rotation=0, fontsize=14)
plt.yticks(rotation=0, fontsize=14)
plt.savefig("weight_wagon_Sep.png", dpi=400,  bbox_inches='tight')
plt.show();
#Filtering data for df_predictors_weight_wagon_Oct
df_predictors_weight_wagon_Oct = df_predictors.copy()
df_predictors_weight_wagon_Oct = df_predictors_weight_wagon_Oct.loc[(df_predictors_weight_wagon_Oct['month_arrival'] =='Oct')].reset_index(drop=True)



# Weight per wagon
df_predictors_weight_wagon_Oct['weight_per_wagon_bin'] = pd.Categorical(df_predictors_weight_wagon_Oct['weight_per_wagon_bin'],
                                                              categories=['<40', '40-60', '60-80', '>80'],
                                                              ordered=True)


# Analysing statistical correlations between different parameters
plt.figure(figsize=(80,40))

dfP = pd.crosstab(index=df_predictors_weight_wagon_Oct['weight_per_wagon_bin'], columns=df_predictors_weight_wagon_Oct['arrived'])
dfP.plot(kind='bar')

plt.xticks(rotation=0, fontsize=12)
plt.xlabel('Weight/wagon [t]', fontsize=15)
plt.tick_params(labelsize=10)
   
    
L=plt.legend(loc='best', fontsize=15)
L.get_texts()[0].set_text('Non-delayed trains')
L.get_texts()[1].set_text('Delayed trains')


Ma = dfP.sum().sum()             #Ma = 8658 en caso de que quieran ver la comparacion con EL TOTAL DE TODA LA MUESTRA
plt.yticks(ticks = np.linspace(0,Ma, 21), labels=["{:.0f}%".format(i*100) for i in np.linspace(0, 1, 21)])
plt.margins(0,0.3)
plt.xticks(rotation=0, fontsize=14)
plt.yticks(rotation=0, fontsize=14)
plt.savefig("weight_wagon_Oct.png", dpi=400,  bbox_inches='tight')
plt.show();
#Filtering data for df_predictors_weight_wagon_Nov
df_predictors_weight_wagon_Nov = df_predictors.copy()
df_predictors_weight_wagon_Nov = df_predictors_weight_wagon_Nov.loc[(df_predictors_weight_wagon_Nov['month_arrival'] =='Nov')].reset_index(drop=True)



# Weight per wagon
df_predictors_weight_wagon_Nov['weight_per_wagon_bin'] = pd.Categorical(df_predictors_weight_wagon_Nov['weight_per_wagon_bin'],
                                                              categories=['<40', '40-60', '60-80', '>80'],
                                                              ordered=True)


# Analysing statistical correlations between different parameters
plt.figure(figsize=(80,40))

dfP = pd.crosstab(index=df_predictors_weight_wagon_Nov['weight_per_wagon_bin'], columns=df_predictors_weight_wagon_Nov['arrived'])
dfP.plot(kind='bar')

plt.xticks(rotation=0, fontsize=12)
plt.xlabel('Weight/wagon [t]', fontsize=15)
plt.tick_params(labelsize=10)
   
    
L=plt.legend(loc='best', fontsize=15)
L.get_texts()[0].set_text('Non-delayed trains')
L.get_texts()[1].set_text('Delayed trains')


Ma = dfP.sum().sum()             #Ma = 8658 en caso de que quieran ver la comparacion con EL TOTAL DE TODA LA MUESTRA
plt.yticks(ticks = np.linspace(0,Ma, 21), labels=["{:.0f}%".format(i*100) for i in np.linspace(0, 1, 21)])
plt.margins(0,0.3)
plt.xticks(rotation=0, fontsize=14)
plt.yticks(rotation=0, fontsize=14)
plt.savefig("weight_wagon_Nov.png", dpi=400,  bbox_inches='tight')
plt.show();
#Filtering data for df_predictors_weight_wagon_Dec
df_predictors_weight_wagon_Dec = df_predictors.copy()
df_predictors_weight_wagon_Dec = df_predictors_weight_wagon_Dec.loc[(df_predictors_weight_wagon_Dec['month_arrival'] =='Dec')].reset_index(drop=True)



# Weight per wagon
df_predictors_weight_wagon_Dec['weight_per_wagon_bin'] = pd.Categorical(df_predictors_weight_wagon_Dec['weight_per_wagon_bin'],
                                                              categories=['<40', '40-60', '60-80', '>80'],
                                                              ordered=True)


# Analysing statistical correlations between different parameters
plt.figure(figsize=(80,40))

dfP = pd.crosstab(index=df_predictors_weight_wagon_Dec['weight_per_wagon_bin'], columns=df_predictors_weight_wagon_Dec['arrived'])
dfP.plot(kind='bar')

plt.xticks(rotation=0, fontsize=12)
plt.xlabel('Weight/wagon [t]', fontsize=15)
plt.tick_params(labelsize=10)
   
    
L=plt.legend(loc='best', fontsize=15)
L.get_texts()[0].set_text('Non-delayed trains')
L.get_texts()[1].set_text('Delayed trains')


Ma = dfP.sum().sum()             #Ma = 8658 en caso de que quieran ver la comparacion con EL TOTAL DE TODA LA MUESTRA
plt.yticks(ticks = np.linspace(0,Ma, 21), labels=["{:.0f}%".format(i*100) for i in np.linspace(0, 1, 21)])
plt.margins(0,0.3)
plt.xticks(rotation=0, fontsize=14)
plt.yticks(rotation=0, fontsize=14)
plt.savefig("weight_wagon_Dec.png", dpi=400,  bbox_inches='tight')
plt.show();<jupyter_output><empty_output><jupyter_text>Interaction effects<jupyter_code>df_interac = df_predictors[['Train_Weight', 'Train_Length', 'wagon_count', 'month_arrival', 'weight_wagon', 'arrived']].reset_index(drop=True)
df_interac['month_arrival'] = df_interac['month_arrival'].astype('object')
df_interac['month_arrival']  = df_interac['month_arrival'] .replace(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], 
                                                                    ['01_Jan', '02_Feb', '03_Mar', '04_Apr', '05_May', '06_Jun', '07_Jul', '08_Aug', '09_Sep', '10_Oct', '11_Nov', '12_Dec'])
df_interac['arrived']  = df_interac['arrived'] .replace(['0_early', '1_late'], 
                                                        ['Non-delayed trains', 'Delayed trains'])
df_interac.head()
# month of arrival - train length

fig, ax = plt.subplots(figsize=(15, 3))
fig = interaction_plot(x=df_interac['month_arrival'], trace=df_interac['arrived'], response=df_interac['Train_Length'],
                       colors=['red', 'green'], markers=['D', 'o'], ms=10, ax=ax)



plt.title('Month - Train length', fontsize=15)
plt.xticks(rotation=0, fontsize=12)
plt.yticks(rotation=0, fontsize=12)
plt.xlabel('Month', fontsize=15)
plt.ylabel('Train length (m)', fontsize=15)
plt.legend(fontsize=15)
plt.savefig('interaction1.png',dpi=400,  bbox_inches='tight')
plt.show()
;
# month of arrival - train weight

fig, ax = plt.subplots(figsize=(15, 3))
fig = interaction_plot(x=df_interac['month_arrival'], trace=df_interac['arrived'], response=df_interac['Train_Weight'], func=np.mean,
                       colors=['red', 'green'], markers=['D', '^'], ms=10, ax=ax)

plt.title('Month - Train weigth', fontsize=15)
plt.xticks(rotation=0, fontsize=12)
plt.yticks(rotation=0, fontsize=12)
plt.xlabel('Month', fontsize=15)
plt.ylabel('Train weight (t)', fontsize=15)
plt.legend(fontsize=15)
plt.savefig('interaction2.png',dpi=400,  bbox_inches='tight')
plt.show()
;
# month of arrival - wagon_count

fig, ax = plt.subplots(figsize=(15, 3))
fig = interaction_plot(x=df_interac['month_arrival'], trace=df_interac['arrived'], response=df_interac['wagon_count'], func=np.mean,
                       colors=['red', 'green'], markers=['D', '^'], ms=10, ax=ax)

plt.title('Month - Wagon count', fontsize=15)
plt.xticks(rotation=0, fontsize=12)
plt.yticks(rotation=0, fontsize=12)
plt.xlabel('Month', fontsize=15)
plt.ylabel('Wagon count', fontsize=15)
plt.legend(fontsize=15)
plt.savefig('interaction3.png',dpi=400,  bbox_inches='tight')
plt.show()
;
# month of arrival - weight per wagon

fig, ax = plt.subplots(figsize=(15, 3))
fig = interaction_plot(x=df_interac['month_arrival'], trace=df_interac['arrived'], response=df_interac['weight_wagon'], func=np.mean,
                       colors=['red', 'green'], markers=['D', '^'], ms=10, ax=ax)

plt.title('Month - weight/wagon', fontsize=15)
plt.xticks(rotation=0, fontsize=12)
plt.yticks(rotation=0, fontsize=12)
plt.xlabel('Month', fontsize=15)
plt.ylabel('weight/wagon [t]', fontsize=15)
plt.legend(fontsize=15)
plt.savefig('interaction4.png',dpi=400,  bbox_inches='tight')
plt.show()
;
# wagon count - train weight

fig, ax = plt.subplots(figsize=(15, 3))
fig = interaction_plot(x=df_interac['wagon_count'], trace=df_interac['arrived'], response=df_interac['Train_Weight'], func=np.mean,
                       colors=['red', 'green'], markers=['D', '^'], ms=10, ax=ax)

plt.title('Wagon count - Train weight', fontsize=15)
plt.xticks(rotation=0, fontsize=12)
plt.yticks(rotation=0, fontsize=12)
plt.xlabel('Wagon count', fontsize=15)
plt.ylabel('Train weight [t]', fontsize=15)
plt.legend(fontsize=15)
plt.savefig('interaction6.png',dpi=400,  bbox_inches='tight')
plt.show()
;
# train_weight - train length

fig, ax = plt.subplots(figsize=(20, 3))
fig = interaction_plot(x=df_interac['Train_Length'], trace=df_interac['arrived'], response=df_interac['Train_Weight'], func=np.mean,
                       colors=['red', 'green'], markers=['D', '^'], ax=ax, plottype='s')

plt.title('Train weight - Train length', fontsize=15)
plt.xticks(rotation=0, fontsize=12)
plt.yticks(rotation=0, fontsize=12)
plt.xlabel('Train weight [t]', fontsize=15)
plt.ylabel('Train length [m]', fontsize=15)
plt.legend(fontsize=15)
plt.savefig('interaction7.png',dpi=400,  bbox_inches='tight')
plt.show()
;<jupyter_output><empty_output><jupyter_text>Analysis per group  Early trains<jupyter_code># early trains
df_early = df_predictors[['Train_Weight', 'wagon_count', 'Train_Distance_KM', 'weight_wagon', 'arrived']][df_predictors['arrived']=='0_early']
df_early.drop(columns=['arrived'], axis=1, inplace=True)

"""### REMOVING OUTLIERS
# find Q1, Q3, and interquartile range for each column
Q1 = df_early.quantile(q=0.25)
Q3 = df_early.quantile(q=0.85)
IQR = df_early.apply(stats.iqr)

# only keep rows in dataframe that have values within 1.5*IQR of Q1 and Q3
df_early = df_early[~((df_early < (Q1-1.5*IQR)) | (df_early > (Q3+1.5*IQR))).any(axis=1)].reset_index(drop=True)
"""
df_early.describe(include='all')
# Boxplots 

fig = plt.figure(figsize=(25, 8))
for i, col in enumerate(df_early.columns):
    fig.add_subplot(2, 2, i+1)
    ax = sns.boxplot(x = df_early[col], orient='h')
    plt.xlabel(col, fontsize=10)
    plt.ylabel('')
    plt.tick_params(labelsize=10)

plt.show();<jupyter_output><empty_output><jupyter_text>Delayed trains<jupyter_code># delayed trains
df_late = df_predictors[['Train_Weight', 'wagon_count', 'Train_Distance_KM', 'weight_wagon', 'arrived']][df_predictors['arrived']=='1_late']
df_late.drop(columns=['arrived'], axis=1, inplace=True)

"""### REMOVING OUTLIERS
# find Q1, Q3, and interquartile range for each column
Q1 = df_late.quantile(q=0.25)
Q3 = df_late.quantile(q=0.85)
IQR = df_late.apply(stats.iqr)

# only keep rows in dataframe that have values within 1.5*IQR of Q1 and Q3
df_late = df_late[~((df_late < (Q1-1.5*IQR)) | (df_late > (Q3+1.5*IQR))).any(axis=1)].reset_index(drop=True)"""

df_late.describe(include='all')
# Boxplots 

fig = plt.figure(figsize=(25, 8))
for i, col in enumerate(df_late.columns):
    fig.add_subplot(2, 2, i+1)
    ax = sns.boxplot(x = df_late[col], orient='h')
    plt.xlabel(col, fontsize=10)
    plt.ylabel('')
    plt.tick_params(labelsize=10)

plt.show();<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15356372599363255, 'fraction_numerical': 0.04749922974222039, 'mean_word_length': 4.328909563551785, 'pattern_counts': {'":': 0, '<': 88, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 92, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '593886', 'license': 'MIT', 'n_tokens_mistral': 15733, 'n_tokens_neox': 14005, 'n_words': 2336, 'path': 'notebooks/3. final_analysis.ipynb', 'repo_name': 'jdpinedaj/CFLDelays', 'size': '38948'}