0
class TISP:

def __init__ (self, x, y, w, k, l, it):
#predict is a 2d list of observed values
    #actual is a 1d list corresponding to a 0-1 value normalized 
    #and descriptive of each observations outcome
    self.predicter = x
    self.actual = y
    self.normal_predicter = []
    self.normal_actual = []
    self.weights = w
    self.kap = k
    self.lam = l
    self.iterations = it
    return

def normalize_predicter (self):
    #create a listed array of predicter, with each value replaced with
    #z score from standardized collumn
    #also cuts off first column
    self.normal_predicter = []
    nppredict = numpy.array(self.predicter)
    nppredict_collumns = nppredict.T
    nppredict_collumns = numpy.delete(nppredict_collumns, 0, 0)
    for col in nppredict_collumns:
        self.normal_predicter.append(scipy.stats.mstats.zscore(col, axis=0))
    self.normal_predicter = numpy.array(self.normal_predicter).T
    return


def normalize_actual (self):
    self.normal_actual = []
    npactual = numpy.array(self.actual)
    self.normal_actual = scipy.stats.mstats.zscore(npactual)
    return

def predicter_matrix (self):
    #(I - 1/k^2 * X^t * X)
    identity_n = self.normal_predicter.shape[1]
    return numpy.identity(identity_n) - self.kap**-2 * numpy.dot(self.normal_predicter.T, self.normal_predicter)


def predicter_actual_matrix (self):
    #1/k^2 * X^t * Y
    return self.kap**-2 * numpy.dot(self.normal_predicter.T, self.normal_actual)


def pseudo_weights (self):
    #theta on slides
    return numpy.dot(self.predicter_matrix(), self.weights) + self.predicter_actual_matrix()



def adjust_lambda (self):
    return self.lam * self.kap**-2


def hard_threshhold (self, pseudo_weight):
    #put pseudo weights through hard threshhold
    adj_lam = self.adjust_lambda()
    if abs(pseudo_weight) < adj_lam:
        return adj_lam**2 - (abs(pseudo_weight) - adj_lam)**2
    else:
        return adj_lam**2


def update_weights (self):
    #update weights, call all
    weights = []
    for col, pw in enumerate(self.pseudo_weights()):
        weights.append(self.hard_threshhold(pw))
    self.weights = weights
    return


def run_all (self):
    self.normalize_all()
    for i in range(self.iterations):
        self.update_weights()
        print self.weights
    return

我省略了规范化函数,但它们设置了属性并且似乎工作正常。

obs 0的前几列:

1,-0.0057548,-0.13457,0.027341,-0.23031,-0.25396,-0.084033,0.50101,-0.15747,-0.20175,0.027615,-0.24926,0.0090723,-0.0516621,6806.

我正在实现一个称为 tisp 的阈值算法。

从权重的零开始,我第一次更新到其他一些低权重:

[0.0004, 0.0004, 0.0004, 0.00020879693209322012, 0.0004, 0.00010554674122089871, 0.0002128103652660953, 0.00018560221711856617, 0.00020718718704488368, 0.00025472546603877807, 0.0004, 0.00032653814690485235, 0.00028773649339636052, 0.00014511605239718185, 0.00026039641181569817, 0.00032812782641074132, 0.00010513077856280048, 0.00020507079525590476, 0.0002843078256187282, 0.00018170990580009818]

但是,随着它的不断发展,它不会再改变(我正在运行 10 次迭代)。

这可能是什么原因造成的?

4

0 回答 0