Ejemplo n.º 1
0
    def generate_record(self):
        retval = SparseVector()
        for i in range(0, self.dim):
            retval.components[i] = random.choice([-1, 1]) + random.uniform(
                -self.noiseEps, +self.noiseEps)

        return retval
Ejemplo n.º 2
0
 def testLinearKernel(self):
     sigma = 1.0
     kernel = LinearKernel()
     x1 = SparseVector()
     x1[0] = 1
     x1[1] = 0
     x2 = SparseVector()
     x2[0] = 0
     x2[1] = 1
     val = kernel.compute(x1, x2)
     true_val = np.dot(np.array([1, 0]), np.array([0, 1]))
     self.assert_(
         val == true_val,
         "Linear kernel of two vectors is not how I expected it...")
Ejemplo n.º 3
0
    def _generate_example(self):
        iNumberOfFeaturesPerStock = 4
        iPosCurStock = self.m_aStocks.index(self.m_stActStock)
        iTargetStockPos = iNumberOfFeaturesPerStock * iPosCurStock
        iFeatureIndex = self.prediction_index - 1
        if iFeatureIndex < 0:
            iFeatureIndex = len(self.m_lFeatures) - 1
        record = SparseVector()
        features = self.getFeatures(iFeatureIndex)
        label = self.getLabel(self.prediction_index)
        for i in xrange(len(features)):
            val = features[i]
            if math.isnan(val) or math.isinf(val):
                print "Error, invalid feature value: " + str(val)
                val = 0.0
            #the following code ensures that the features of the target stock are always at the beginning of the feature vector
#             if i < iTargetStockPos or i >= iTargetStockPos + iNumberOfFeaturesPerStock:
#                 record[i+iNumberOfFeaturesPerStock] = val
#             else:
#                 record[i-iTargetStockPos] = val
            record[i] = val
        self.prediction_index += 1
        if self.prediction_index >= len(self.m_hPrices[self.m_stActStock]):
            self.prediction_index = 0
        if self.generated_examples % self.number_of_nodes == 0:
            self.drift()
        return (record, label)
Ejemplo n.º 4
0
    def generate_example(self):
        record = SparseVector()
        for i in xrange(len(self.m_aFeatureNames)):
            aFeatures = self.getFeatures(self.prediction_index)
            record.components[self.m_aFeatureNames[i]] = aFeatures[i]
        label = self.m_aAnnotations[self.m_stTargetStock][
            self.prediction_index]
        if self.m_bDelay == True:
            if self.prediction_index % self.m_iAccountingPeriod == 0:
                label = []
                for t in xrange(
                        self.prediction_index - self.m_iAccountingPeriod + 1,
                        self.prediction_index + 1):
                    if t >= 0:
                        label.append(
                            self.m_aAnnotations[self.m_stTargetStock][t])
            else:
                label = None

        self.prediction_index += 1
        if self.prediction_index % self.m_iRepetitionInterval == 0:
            if self.m_iCurrRepitions < self.m_iRepetitions:
                self.prediction_index -= self.m_iRepetitionInterval
                self.m_iCurrRepitions += 1
            else:
                self.m_iCurrRepitions = 0
        if self.prediction_index >= len(self.m_aPrices[self.m_stTargetStock]):
            if len(self.m_aTargetStocks) > 0:
                self.m_stTargetStock = self.m_aTargetStocks.pop(0)
                self.m_oCorrel.changeSet([self.m_stTargetStock])
                self.prediction_index = 0

        return (record, label)
Ejemplo n.º 5
0
 def _get_sparse_rep(self, example_batch):
     sv_list = []
     for ex in example_batch:
         rec = SparseVector()
         for i in range(len(ex)):
             rec[i] = ex[i]
         sv_list.append(rec)
     return sv_list
Ejemplo n.º 6
0
 def _generate_example(self):
     self._try_drift()
     label = self.label_distribution.rvs()
     hidden_values = []
     record = set()
     for i in xrange(len(self.hidden_layer)):
         hidden_values.append(self.hidden_layer[i].rvs(label))
         
     for i in xrange(self.dim):
         if self.output_layer[i].rvs(hidden_values[i % len(self.hidden_layer)]) == 1:
             record.add(i)
     if label == 0: label = -1
     record_vector = SparseVector()
     for i in xrange(self.dim):
         if i in record:
             record_vector.components[i] = 1.0
         else:
             record_vector.components[i] = -1.0
     return (record_vector, label)
Ejemplo n.º 7
0
    def testKernelModelAveraging(self):
        kernel = LinearKernel()  #GaussianKernel(sigma)
        truncOp = NoCompression()

        x1 = SparseVector()
        x1[0] = 1
        x1[1] = 0
        x1[2] = 0
        sv1 = SupportVector()
        sv1.record = x1
        sv1.weight = 0.1

        x2 = SparseVector()
        x2[0] = 0
        x2[1] = 1
        x2[2] = 0
        sv2 = SupportVector()
        sv2.record = x2
        sv2.weight = 0.2

        x3 = SparseVector()
        x3[0] = 0
        x3[1] = 1
        x3[2] = 0
        sv3 = SupportVector()
        sv3.record = x2
        sv3.weight = 0.3

        model1 = KernelClassificationModel(kernel, truncOp)
        model2 = KernelClassificationModel(kernel, truncOp)
        model1.supportVectors.append(sv1)
        model1.supportVectors.append(sv3)
        model2.supportVectors.append(sv2)

        model3 = aritmethic_mean([model1, model2])
        self.assert_(model3.supportVectors[0].weight == sv1.weight / 2.0,
                     "Averaging of models failed.")
        self.assert_(
            model3.supportVectors[1].weight == (sv2.weight + sv3.weight) / 2.0,
            "Averaging of models failed.")
Ejemplo n.º 8
0
    def testGaussKernel(self):
        sigma = 1.0
        kernel = GaussianKernel(sigma)
        x1 = SparseVector()
        x1[0] = 1
        x1[1] = 0
        x2 = SparseVector()
        x2[0] = 0
        x2[1] = 1

        val1 = kernel.compute(x1, x1)
        self.assert_(
            val1 == 1.0,
            "Gaussian kernel of vector with itself is not equal to 1.")

        val2 = kernel.compute(x1, x2)
        true_norm = np.linalg.norm(np.array([1, 0]) - np.array([0, 1]))
        true_exp = -1 * (true_norm**2)
        true_exp = true_exp / (2 * (sigma**2))
        true_val = np.exp(true_exp)
        self.assert_(
            val2 == true_val,
            "Gaussian kernel of two vectors is not how I expected it...")
Ejemplo n.º 9
0
 def _generate_example(self):
     record = SparseVector()
     features = self.getFeatures(self.prediction_index - 1)
     label = self.getLabel(self.prediction_index)
     if math.isnan(label) or math.isinf(label):
         print "Error, invalid label: " + str(label)
         label = 0.0
     for i, val in enumerate(features):
         if math.isnan(val) or math.isinf(val):
             print "Error, invalid feature value: " + str(val)
             val = 0.0
         record[i] = val
     self.prediction_index += 1
     return (record, label)
Ejemplo n.º 10
0
 def generate_record(self):
     return SparseVector()
Ejemplo n.º 11
0
 def _generate_example(self):
     record = SparseVector(0.0)
     for i in xrange(self.dim):
         record.components[i] = 0.0
     return (record, 0.0)
Ejemplo n.º 12
0
 def generate_record(self):
     retval = SparseVector()  
     for i in range(0, self.dim):
         retval.components[i] = random.choice([-1, 1])
       
     return retval
Ejemplo n.º 13
0
 def generate_record(self):
     record = SparseVector()
     for key in self.singletons:
         val = uniform(self.minVal, self.maxVal)
         record.__setitem__(key, val)
     return record