Mission_Impossible / Normalizing_Dataset_and_calculating_TrainNumber.py
prathamg007's picture
Upload 5 files
25a0890 verified
Raw History Blame Contribute Delete
2.4 kB
import pandas as pd
df_raw = pd.read_csv("train_dataset_named.csv") #Reading the CSV file made in the previous code
city_mapping = {"New York": 1, "Boston": 2, "Chicago": 3, "Washington": 4, "Philadelphia": 5} # Define mappings for normalization
day_mapping = {"Monday": 1, "Tuesday": 2, "Wednesday": 3, "Thursday": 4, "Friday": 5, "Saturday": 6, "Sunday": 7}
train_type_mapping = {"Local": 0, "Express": 1}
def time_to_hours(time_str): #Converting time string to hours
hours, minutes = map(int, time_str.split(":"))
return hours + minutes / 60.0
df_normalized = pd.DataFrame() #Normalized dataframe
#Normalizing all of the features and converting them into 0-1 Range
df_normalized["departure_station"] = df_raw["departure_station"].map(city_mapping) / 5.0
df_normalized["arrival_station"] = df_raw["arrival_station"].map(city_mapping) / 5.0
df_normalized["departure_time"] = df_raw["departure_time"].apply(time_to_hours) / 24.0
df_normalized["arrival_time"] = df_raw["arrival_time"].apply(time_to_hours) / 24.0
df_normalized["day_of_week"] = df_raw["day_of_week"].map(day_mapping) / 7.0
df_normalized["train_type"] = df_raw["train_type"].map(train_type_mapping)
df_normalized["distance_km"] = df_raw["distance_km"].astype(float) / 1000.0
df_normalized["distance_km"] = (df_normalized["distance_km"] - 0.15) / (1.4 - 0.15)
# Pre calculated weights and biases to calculate train number manually
weights = {
"departure_station": 0.5,
"arrival_station": 0.5,
"departure_time": 0.5,
"arrival_time": 0.5,
"day_of_week": 0.5,
"train_type": 3.5,
"distance_km": 4.5
}
bias = 0.5
df_normalized["train_number"] = (
weights["departure_station"] * df_normalized["departure_station"] +
weights["arrival_station"] * df_normalized["arrival_station"] +
weights["departure_time"] * df_normalized["departure_time"] +
weights["arrival_time"] * df_normalized["arrival_time"] +
weights["day_of_week"] * df_normalized["day_of_week"] +
weights["train_type"] * df_normalized["train_type"] +
weights["distance_km"] * df_normalized["distance_km"] +
bias
).round().astype(int) #Rounding of train number to integers
df_normalized["train_number"] = df_normalized["train_number"].clip(1, 10) #Values greater than 10 are clipped to 10
df_normalized.to_csv("normalized_train_dataset_with_train_number.csv", index=False) # Saved as a CSV file