-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassify.py
74 lines (52 loc) · 2.16 KB
/
classify.py
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
67
68
69
70
71
72
73
74
import pandas as pd
import numpy as np
from knn import KNN
from ast import literal_eval
from distances import DTW
from sklearn import svm, preprocessing
from sklearn.model_selection import cross_val_score, KFold
from sklearn.metrics import accuracy_score
def crossValidation(clf, allSequences, categoryIds, le):
splits, accur = 10, 0
kf = KFold(n_splits=splits)
# Splits data to train and test data
for trainIndex, testIndex in kf.split(allSequences):
kfSeqTrain = [allSequences[ind] for ind in trainIndex]
kfSeqTest = [allSequences[ind] for ind in testIndex]
clf.fit(kfSeqTrain, categoryIds[trainIndex])
kfTestPredIds = clf.predict(kfSeqTest)
accur += accuracy_score(categoryIds[testIndex], kfTestPredIds)
accur /= float(splits)
print 'Accuracy: ', accur
def writeInCsv(predCategs):
# write prediction in csv file
predInfo = {
'Test_Trip_ID': [],
'Predicted_JourneyPatternID': []
}
for i in range(len(predCategs)):
predInfo['Test_Trip_ID'].append(i)
predInfo['Predicted_JourneyPatternID'].append(predCategs[i])
df = pd.DataFrame(predInfo, columns=['Test_Trip_ID', 'Predicted_JourneyPatternID'])
df.to_csv('testSet_JourneyPatternIDs.csv', sep='\t', index=False)
def main():
trainSet = pd.read_csv('datasets/train_set.csv',
converters={'Trajectory': literal_eval})
testSet = pd.read_csv('datasets/test_set_a2.csv',
converters={'Trajectory': literal_eval})
# labels for categories
le = preprocessing.LabelEncoder()
categoryIds = le.fit_transform(trainSet['journeyPatternId'])
allSequences = []
for trainIndex, trainRow in trainSet.iterrows():
allSequences.append(trainRow['Trajectory'])
# initialize KNN classifier
clf = KNN(5, DTW)
crossValidation(clf, allSequences, categoryIds, le)
clf.fit(allSequences, categoryIds)
# predict the categories for the testSet
predIds = clf.predict(testSet['Trajectory'])
predCategs = le.inverse_transform(predIds)
writeInCsv(predCategs)
if __name__ == '__main__':
main()