-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
53 lines (41 loc) · 1.5 KB
/
util.py
File metadata and controls
53 lines (41 loc) · 1.5 KB
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
import numpy as np
from numpy.core.umath import sign
def reshapeFeatureVector(train_X, columnWidths):
newFeatureMatrix = np.zeros((train_X.shape[0], sum(columnWidths)))
for rowIndex, featureVector in enumerate(train_X):
newColumnIndex = 0
for origColIndex, colWidth in enumerate(columnWidths):
if colWidth == 1:
newFeatureMatrix[rowIndex][newColumnIndex] = featureVector[origColIndex]
else:
for ii in range(int(colWidth)):
if (ii < len(featureVector[origColIndex])):
newFeatureMatrix[rowIndex][newColumnIndex + ii] = featureVector[origColIndex][ii]
newColumnIndex += colWidth
return newFeatureMatrix
def getFeatureColumnWidths(train_X):
columnWidths = np.zeros(train_X.shape[1])
for row in train_X:
for index, col in enumerate(row):
length = getLengthOfFeature(col)
if columnWidths[index] < length:
columnWidths[index] = length;
return columnWidths
def getLengthOfFeature(feature):
try:
return len(feature)
except TypeError:
return 1
def ceilingForY(X, Y):
changes = 0
y = Y.ravel()
lengths = [x[1] for x in X]
y_signs = sign(y)
y_vals = abs(y)
for i in range(len(y)):
if y_vals[i] > lengths[i]:
y[i] = lengths[i]
y[i] *= y_signs[i]
changes += 1
print str(changes) + " ceilings adjusted."
return y