File size: 2,044 Bytes
29a6c53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import csv
import os
import numpy as np
import pandas as pd


def loadData(purpose, fpath, has_labels=True):
    print("Reading labels from: ", fpath)
    print("Loading %s data..." % purpose)
    #return lat, longi
    if has_labels:
        return parseFileWithLabel(fpath)

    return parseFileWithoutLabel(fpath)

# parse training and validation files
def parseFileWithLabel(file_path):
    with open(file_path, "r") as f:
        data = f.read().splitlines()
        features = [splitted_line[1] for splitted_line in
                    [line.split("\t", maxsplit=1) for line in data[1:]]]
        labels = np.array([splitted_line[0] for splitted_line in
                [line.split("\t", maxsplit=1) for line in data[1:]]])
        
        print("Labels shape: ", labels.shape)
        print("Samples length: ", len(features))

        return features, labels #list(labels[:, 0]), list(labels[:, 1])

# parse test file
def parseFileWithoutLabel(file_path):
    with open(file_path, "r") as f:
        data = f.read().splitlines()
        features = [splitted_line[0] for splitted_line in
                    [line.split("\t", maxsplit=1) for line in data[1:]]]
    return features


def labels_to_numeric(labels):
    # Data frame from list
    labels_df = pd.DataFrame(labels)

    # 0 - BE, 1 - CA, 2 - CH, 3 - FR
    labels_df[0] = labels_df[0].replace({'BE': 0})
    labels_df[0] = labels_df[0].replace({'CA': 1})
    labels_df[0] = labels_df[0].replace({'CH': 2})
    labels_df[0] = labels_df[0].replace({'FR': 3})

    print(np.array(labels_df.values).flatten())

    return list(np.array(labels_df.values).flatten())


def flatten_labels(true_labels, predictions):
    true_labels_flat = []
    predictions_flat = []
    for index in range(len(true_labels)):
        true_labels_flat += list(true_labels[index])
        pred_flat = np.argmax(predictions[index], axis=1).flatten()
        predictions_flat += list(pred_flat)

    #print(true_labels_flat)
    #print(predictions_flat)

    return true_labels_flat, predictions_flat