Advertisement
Guest User

Untitled

a guest
May 25th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.79 KB | None | 0 0
  1. class FeatureStats(object):
  2.     def __init__(self,word, ld, net):
  3.         f = net.getConvFunction()
  4.         inds = ld.word2inds(word)
  5.         windows = []
  6.         for i in range(len(inds) + 1 - net.filter_height):
  7.             windows.append(inds[i:i+net.filter_height])
  8.         windows = map(lambda x: ld.inds2word(x), windows)
  9.         features = f([[inds]])
  10.         shape = features.shape
  11.         features = features.reshape((shape[1],shape[2])).transpose()
  12.         self.data = zip(windows,features)
  13.  
  14.     def thresholdStats(self, threshold):
  15.         res = []
  16.         for pair in self.data:
  17.             l = len(filter(lambda x: x >= threshold, pair[1]))
  18.             res.append((pair[0],l))
  19.         return res
  20.  
  21.     def normStats(self):
  22.         res = []
  23.         for pair in self.data:
  24.             m = np.linalg.norm(pair[1])
  25.             res.append((pair[0],m))
  26.         return res
  27.  
  28.     def maxStats(self):
  29.         ngramms
  30.         res = {i:0 for i in map(lambda x: x[0], self.data)}
  31.         for i in xrange(len(self.data)):
  32.             l = lambda x: x[1][i]
  33.             cur_data = self.data
  34.             cur_data.sort(key=l, reverse=True)
  35.             res[cur_data[0][0]] += 1
  36.         return [res[i] for i in ]
  37.  
  38.  
  39.     def printStats(self, metrics='threshold',threshold=None, ordered=True, mask="%s - %d"):
  40.         if metrics == 'threshold':
  41.             pairs = self.thresholdStats(threshold)
  42.         elif metrics == 'norm':
  43.             pairs = self.normStats()
  44.         if ordered:
  45.             pairs.sort(key=(lambda x: x[1]), reverse=True)
  46.         for i in pairs:
  47.             print mask % (i[0],i[1])
  48.  
  49.  
  50.  
  51.  
  52. def test(word):
  53.     print
  54.     print
  55.     print word
  56.     print
  57.     print "Максимальные нграммы по количеству фильтров прошедших порог 0.03"
  58.     FeatureStats(word, letterDict, network).printStats(metrics='threshold', threshold=0.04)
  59.     print
  60.     print "Максимсальные нграммы по норме"
  61.     FeatureStats(word, letterDict, network).printStats(metrics='norm', mask="%s - %.3f")
  62.     print
  63.     print
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement