-
Notifications
You must be signed in to change notification settings - Fork 3
/
ArffData.py
173 lines (137 loc) · 3.72 KB
/
ArffData.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
172
173
# -*- coding: utf-8 -*-
import scipy.io.arff as scarff
import numpy as np
class ArffData:
arffFile=""
uselessFeat=[]
def __init__(self, arg1, arg2):
self.arffFile=arg1#myData.arff
self.uselessFeat=arg2#[sentFeat, tagAB]
"""
Feature selection
"""
def indexBadFeat(self,uselessFeat):
"""
(self,list)->tuple(list, list)
calls the arffLoader/converter for the arfFile of the current object.
Returns the list of features names as a list and the list of useless feqture position
in the arff file. That keeps track of the useless feature position for later elimination.
Elimination via pop() the list must be in decrease order
"@Arff file
Feat0
Feat1
Feat_TagAB
Feat_TagABCD
>>>indexBadFeat(self,[Feat2,Feat_TagAB])
([2,1],[Feat0,Feat_TagABCD])
"""
data=scarff.loadarff(self.arffFile)
indexList=[]#The features are present in a certain order.
#Some are useless. Their index is listed for elimination later
usefulFeat=[]
i=0
for featureName in data[1]:
if featureName in uselessFeat:
indexList.append(i)
else:
usefulFeat.append(featureName)
i+=1
indexList.reverse()
return (indexList,usefulFeat)
def selFeat(self,indexFeat,row):
"""
returns the predictor instance cleaned from its useless data/features
>>>selFeat([0,1,0,0.3333,])
[0,0.3333]
"""
for i in indexFeat:
row.pop(i)
return row
"""
Open and convert the arff Data file
"""
def getX_y(self):#X the predictor variables; y the target variable
"""
(self)->list[array, array, int]
calls the arffLoader/converter for the arfFile of the current object.
Returns the data in python readable format (array)
and the number of true instances (iTrue)
>>>getX_y()
[[0,0.333],[1,0.2]],[[1],[0]],1
"""
iTrue=0
indexList=self.indexBadFeat(self.uselessFeat)[0]
X=[]
y=[]
for row in scarff.loadarff(self.arffFile)[0]:
rowX=self.selFeat(indexList,list(row)[0:-1])
X.append(rowX)
if row[-1]=='True':
y.append(1.0)
iTrue+=1
else:
y.append(0.0)
X = np.array(X)
y = np.array(y)
return [X,y,iTrue]
def getX_y_bord(self):#X the predictor variables; y the target variable
"""
(self)->list[array, array, int]
More sofisticated version of getX_y
calls the arffLoader/converter for the arfFile of the current object.
Returns the data in python readable format (array)
and the number of true instances (iTrue)
>>>getX_y()
[[0,0.333],[1,0.2]],[[1],[0]],1
"""
iTrue=0
indexList=self.indexBadFeat(self.uselessFeat)[0]
X=[]
y=[]
for row in scarff.loadarff(self.arffFile)[0]:
#rowX=self.selFeat(indexList,list(row)[0:-1])
#X.append(rowX)
if row[-1]=='True':
y.append(1.0)
iTrue+=1
elif row[-1]=='Duplicate':
y.append(2.0)
elif row[-1]=='Borderline':
y.append(3.0)
elif row[-1]=='DuplicateOfBorderline':
y.append(4.0)
elif row[-1]=='Unknown':
y.append(5.0)
else:
y.append(0.0)
X = np.array(X)
y = np.array(y)
return [X,y,iTrue]
def getX_y_bord1(self):#X the predictor variables; y the target variable
"""
(self)->list[array, array, int]
More sofisticated version of getX_y
calls the arffLoader/converter for the arfFile of the current object.
Returns the data in python readable format (array)
and the number of true instances (iTrue)
>>>getX_y()
[[0,0.333],[1,0.2]],[[1],[0]],1
"""
iTrue=0
indexList=self.indexBadFeat(self.uselessFeat)[0]
X=[]
y=[]
for row in scarff.loadarff(self.arffFile)[0]:
#rowX=self.selFeat(indexList,list(row)[0:-1])
#X.append(rowX)
if row[-1]=='True':
y.append(1.0)
iTrue+=1
elif row[-1]=='Borderline':
y.append(1.0)
iTrue+=1
else:
y.append(0.0)
X = np.array(X)
y = np.array(y)
return [X,y,iTrue]