File size: 1,374 Bytes
1bedd9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import pandas as pd
import pickle
import os

def obtain_params_opt() :
    
    path = os.path.join(os.path.dirname(__file__), "params", "params_opt.pkl")

    with open(path, "rb") as file :
        a = pickle.load(file)
        
    path = os.path.join(os.path.dirname(__file__), "params", "columns.pkl")

    with open(path, "rb") as file :
        columns = pickle.load(file)
    
    return a, columns

def format_data(csv_file, columns) :
    
    df = pd.read_csv(csv_file.name)
    
    df = df.drop(columns=['Unnamed: 0', 'flight'])
    df['class'] = df['class'].apply(lambda x: 1 if x=='Business' else 0)
    df.stops = pd.factorize(df.stops)[0]
    
    for col in ['airline', 'source_city', 'destination_city', 'departure_time', 'arrival_time']:
        counts = df[col].value_counts()
        common = counts[counts > 100].index  
        df[col] = df[col].where(df[col].isin(common), other='Other')

    df = pd.get_dummies(df, columns=[
        'airline', 'source_city', 'destination_city',
        'departure_time', 'arrival_time', 'stops', 'class'
    ], drop_first=True)
    
    df = df.drop_duplicates()
    
    x = df.copy()
    
    x.insert(0, 'intercept', 1)
    
    for col in columns:
        if col not in df.columns:
            df[col] = 0 
            
    df = df[columns]

    X_matrix = df.astype(float).to_numpy()
    
    return X_matrix