-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeature Extraction.py
136 lines (76 loc) · 2.48 KB
/
Feature Extraction.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# coding: utf-8
# In[11]:
import json
from src.data_processing import print_progress
def ranked_vectors(path):
subs = []
for index in range(1780):
print_progress(index, 1780)
full_path = "results/{}/{}".format(path, index)
with open(full_path) as f:
subs.append(json.load(f))
return subs
# In[12]:
def number_of_substitutions(index, subs):
tot_sum = 0
for k, v in subs[index].items():
tot_sum += len(v)
return tot_sum
# In[13]:
def generate_ranked_vector(index, mvl, subs):
vec = [0] * mvl
scores = []
for k, v in subs[index].items():
scores.extend([score[1] for score in v])
vec[:len(scores)] = list(sorted(scores, reverse=True))
return vec
# In[14]:
import numpy as np
# Load the vectors, dump as binary
# In[15]:
locations = (['phonetic_filter_no_pos',
'phonetic_filter_with_pos',
'all_trigram_no_pos',
'all_trigram_with_pos'])
# In[16]:
# max_columns = 10000
# for p in locations:
# subs = ranked_vectors(p)
# max_vector_length = (min(max(number_of_substitutions(i, subs)
# for i in range(1780)),
# max_columns))
# vectors = ([generate_ranked_vector(i, max_vector_length, subs)
# for i in range(1780)])
# X = np.array(vectors)
# with open('vectors/{}.np'.format(p), 'wb') as f:
# np.save(f, X)
# In[17]:
from src.data_processing import load_data
task1, task2, task3, min_pairs, strings, pun_strings = load_data()
# In[18]:
with open("vectors/phonetic_filter_with_pos.np", 'rb') as f:
X = np.load(f)
# In[19]:
y = np.array([int(context['pun']) for context in task1])
# In[20]:
# from sklearn.model_selection import KFold
# kf = KFold(n_splits=10)
# kf.get_n_splits(X)
# columns = 10
# from sklearn.metrics import classification_report
# for train_index, test_index in kf.split(X):
# print(train_index)
# X_train, X_test = X[train_index, :columns], X[test_index, :columns]
# y_train, y_test = y[train_index], y[test_index]
# clf = svm.SVC()
# clf.fit(X_train, y_train)
# print(classification_report(y_test, clf.predict(X_test)))
# input()
# In[ ]:
from sklearn.model_selection import cross_val_score
from sklearn import svm
examples = 333
clf = svm.SVC(kernel='poly', verbose=True)
scores = cross_val_score(clf, X[:examples, :3], y[:examples,], cv=3)
# In[22]:
print(scores)