-
Notifications
You must be signed in to change notification settings - Fork 1
/
runSVM_para2id.py
171 lines (135 loc) · 6.71 KB
/
runSVM_para2id.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#! /user/bin/evn python
# -*- coding:utf8 -*-
"""
@Author : Lau James
@Contact : LauJames2017@whu.edu.cn
@Project : Structure_Func_Recognition
@File : runSVM.py
@Time : 18-12-14 上午11:19
@Software : PyCharm
@Copyright: "Copyright (c) 2018 Lau James. All Rights Reserved"
"""
import numpy as np
import os
import sys
from sklearn import svm
from sklearn.metrics import classification_report, confusion_matrix
from data import dataLoader_dt
from data import wordList
base_dir = os.getcwd()
header_data_file_dir = base_dir+'/data/header3500'
header_data_test_file_dir = base_dir+'/data/header500'
header_all_data_path = base_dir + '/data/header'
section_data_file_dir = base_dir+'/data/section3500'
section_data_test_file_dir = base_dir+'/data/section500'
section_all_data_path =base_dir+ '/data/section'
paragraph_data_file_dir = base_dir+'/data/paragraph3500'
paragraph_data_test_file_dir = base_dir+'/data/paragraph500'
paragraph_all_data_path =base_dir+ '/data/paragraph'
save_train_vacab_dir = base_dir+'/SVM/save/train'
save_test_vacab_dir = base_dir+'/SVM/save/test'
if not os.path.exists(save_train_vacab_dir):
os.makedirs(save_train_vacab_dir)
if not os.path.exists(save_test_vacab_dir):
os.makedirs(save_test_vacab_dir)
def svm_header():
print('SVM decision for header...')
x_header_train, y_header_train = wordList.para2id_header(header_data_file_dir, header_all_data_path, 4000)
x_header_test, y_header_test = wordList.para2id_header(header_data_test_file_dir, header_all_data_path,4000)
# 处理y
y_header_train = np.argmax(y_header_train, 1)
y_header_test = np.argmax(y_header_test, 1)
clf = svm.SVC(C=10, kernel='rbf', gamma=0.001, decision_function_shape='ovo', probability=True)
# clf = svm.NuSVC(nu=0.5, gamma=0.001, decision_function_shape='ovo')
# clf = svm.SVC()
print(clf)
clf.fit(x_header_train, y_header_train)
print("fitted...")
y_pred = clf.predict(x_header_test)
y_train_pred = clf.predict(x_header_train)
print('for trainning data: ')
# clf.score(y_header_train, y_train_pred)
print(np.mean(y_train_pred == y_header_train))
print(classification_report(y_header_train, y_train_pred, target_names=['Introduction', 'Relaterd work',
'Methods', 'Experiment', 'Conclusion']))
print(confusion_matrix(y_header_train, y_train_pred))
print('for testing data: ')
# clf.score(y_header_test, y_pred)
print(np.mean(y_pred == y_header_test))
print(classification_report(y_header_test, y_pred, target_names=['Introduction', 'Relaterd work',
'Methods', 'Experiment', 'Conclusion'] ))
print(confusion_matrix(y_header_test, y_pred))
print(clf.predict_proba(x_header_test))
def svm_section():
print('SVM decision for section...')
x_section_train, y_section_train = wordList.para2id(section_data_file_dir, section_all_data_path,7500)
x_section_test, y_section_test = wordList.para2id(section_data_test_file_dir, section_all_data_path,7500)
# x_header_test, y_header_test, x_header_train, y_header_train = dataLoader_dt.load_header_data(header_data_file_dir, save_test_vacab_dir,
# 0.9, 15)
# 处理y
y_section_train = np.argmax(y_section_train, 1)
y_section_test = np.argmax(y_section_test, 1)
clf = svm.SVC(C=10, kernel='rbf', gamma=0.001, decision_function_shape='ovo', probability=True)
# clf = svm.NuSVC()
# clf = svm.SVC()
print(clf)
clf.fit(x_section_train, y_section_train)
print("fitted...")
y_pred = clf.predict(x_section_test)
y_train_pred = clf.predict(x_section_train)
print('for trainning data: ')
# clf.score(y_header_train, y_train_pred)
print(np.mean(y_train_pred == y_section_train))
print(classification_report(y_section_train, y_train_pred, target_names=['Introduction', 'Relaterd work',
'Methods', 'Experiment', 'Conclusion']))
print(confusion_matrix(y_section_train, y_train_pred))
print('for testing data: ')
# clf.score(y_header_test, y_pred)
print(np.mean(y_pred == y_section_test))
print(classification_report(y_section_test, y_pred, target_names=['Introduction', 'Relaterd work',
'Methods', 'Experiment', 'Conclusion'] ))
print(confusion_matrix(y_section_test, y_pred))
print(clf.predict_proba(x_section_test))
def svm_paragraph():
print('SVM decision for paragraph...')
x_para_train, y_para_train = wordList.para2id(paragraph_data_file_dir, paragraph_all_data_path, 7500)
x_para_test, y_para_test = wordList.para2id(paragraph_data_test_file_dir, paragraph_all_data_path, 7500)
# x_para_test, y_para_test, x_para_train, y_para_train = dataLoader_dt.load_paragraph_data(paragraph_data_test_file_dir,
# save_test_vacab_dir, 0.9, 600)
# 处理y
y_para_train = np.argmax(y_para_train, 1)
y_para_test = np.argmax(y_para_test, 1)
clf = svm.SVC(C=10, kernel='rbf', gamma=0.001, decision_function_shape='ovo', probability=True)
# clf = svm.NuSVC()
# clf = svm.SVC()
print(clf)
clf.fit(x_para_train, y_para_train)
print("fitted...")
y_pred = clf.predict(x_para_test)
y_train_pred = clf.predict(x_para_train)
print('for trainning data: ')
# clf.score(y_header_train, y_train_pred)
print(np.mean(y_train_pred == y_para_train))
print(classification_report(y_para_train, y_train_pred, target_names=['Introduction', 'Relaterd work',
'Methods', 'Experiment', 'Conclusion']))
print(confusion_matrix(y_para_train, y_train_pred))
print('for testing data: ')
# clf.score(y_header_test, y_pred)
print(np.mean(y_pred == y_para_test))
print(classification_report(y_para_test, y_pred, target_names=['Introduction', 'Relaterd work',
'Methods', 'Experiment', 'Conclusion'] ))
print(confusion_matrix(y_para_test, y_pred))
print(clf.predict_proba(x_para_test))
if __name__ == '__main__':
if len(sys.argv) != 2 or sys.argv[1] not in ['header', 'section','paragraph']:
raise ValueError("Please input: python3 runLSTM.py [header/section/paragraph]")
if sys.argv[1] == 'header':
svm_header()
elif sys.argv[1] == 'section':
svm_section()
else:
svm_paragraph()
# print(os.getcwd()+'/data/header3500')
# print(os.path.abspath(__file__))
# svm_header()
# svm_paragraph()