예제 #1
0
 def denoise(self, data, wavelet):
     noiseSigma = median(absolute(data - median(data))) / 0.6745
     levels = int(floor(log(len(data))))
     WC = pywt.wavedec(data, wavelet, level=levels)
     threshold = noiseSigma * sqrt(2 * log(len(data)))
     NWC = map(lambda x: pywt.thresholding.hard(x, threshold), WC)
     return pywt.waverec(NWC, wavelet)
예제 #2
0
 def period_guess(self, y_s, t_0):
     extrema = []
     spacings = [log(extrema[i+1] - t_0) - log(extrema[i] - t_0)
                 for i in range(1, len(extrema))]
     omegas = spacings / 2 * 22 / 7
     omega_guess = average(omegas)
     omega_std = std(omegas)
     return omega_guess, omega_std
예제 #3
0
def calculate_j(h, lambda_c, m, theta1, theta2, y_tmp):
    j = 0
    for i in range(m):
        j -= np.dot(y_tmp[i], log(h[i])) - np.dot(1 - y_tmp[i], log(1 - h[i]))

    theta1_new = set_zero_column(theta1)
    theta2_new = set_zero_column(theta2)
    j += lambda_c * (sum_squared(theta1_new) + sum_squared(theta2_new)) / 2

    return j / m
예제 #4
0
def calculate_j(h, lambda_c, m, theta1, theta2, y_tmp):
    j = 0
    for i in range(m):
        j -= np.dot(y_tmp[i], log(h[i])) - np.dot(1 - y_tmp[i], log(1 - h[i]))

    theta1_new = set_zero_column(theta1)
    theta2_new = set_zero_column(theta2)
    j += lambda_c * (sum_squared(theta1_new) + sum_squared(theta2_new)) / 2

    return j / m
예제 #5
0
def log2(x, y=None):
    """Returns the base 2 logarithm of x

    If y is an array, the result replaces the contents of y.
    """
    x = asanyarray(x)
    if y is None:
        y = umath.log(x)
    else:
        umath.log(x, y)
    y /= _log2
    return y
예제 #6
0
def log2(x, y=None):
    """Returns the base 2 logarithm of x

    If y is an array, the result replaces the contents of y.
    """
    x = asanyarray(x)
    if y is None:
        y = umath.log(x)
    else:
        umath.log(x, y)
    y /= _log2
    return y
예제 #7
0
def intensity_and_normalized_spectr(wf, sr=None, tile_size=DFLT_TILE_SIZE,
                                    tile_step=None, intensity_normalization_factor=1):
    tile_step = tile_step or tile_size
    S = abs_stft(wf, tile_size=tile_size, tile_step=tile_step)
    intensity = S.sum(axis=0)
    S /= intensity
    return np.vstack((log(intensity + 1) / intensity_normalization_factor, S))
예제 #8
0
    def train(self, data, labels, max_iter=1000, tolerance=0.01, learning_rate=0.0001):
        classes_count = len(set(labels))
        if classes_count != self.theta.shape[0]:
            raise Exception("error: classes_count!")
        features_count = data.shape[1]
        if features_count != self.theta.shape[1]:
            raise Exception("error: features_count!")
        labels_m = matrix([make_output(i, classes_count) for i in labels]).T
        iteration = 1
        last_cost = inf
        while True:
            nabla_theta = matrix(repeat(0.0, prod(self.theta.shape))).reshape(self.theta.shape)

            numerators = exp(self.theta * data.T + repeat(self.bias, data.shape[0], 1))
            denumenators = repeat(numerators.sum(axis=0), numerators.shape[0], 0)
            y = numerators / denumenators  # outputs of softmax

            cost = -sum(multiply(log(y), labels_m))
            print "Iteration #" + str(iteration) + ", cost is " + str(cost)
            if iteration > max_iter:
                print "break: iterations"
                break
            if abs(last_cost - cost) < tolerance:
                print "break: tolerance"
                break
            last_cost = cost

            dE_dz = y - labels_m
            nabla_bias = dE_dz.sum(axis=1)

            for idx_data in range(data.shape[0]):
                for idx_neuron in range(self.theta.shape[0]):
                    for idx_weight in range(self.theta.shape[1]):
                        nabla_theta[idx_neuron, idx_weight] += data[idx_data, idx_weight] * dE_dz[idx_neuron, idx_data]

            self.theta -= learning_rate*nabla_theta
            self.bias -= learning_rate*nabla_bias

            iteration += 1
 def test_log1p(self):
     assert_almost_equal(ncu.log1p(0.2), ncu.log(1.2))
     assert_almost_equal(ncu.log1p(1e-6), ncu.log(1 + 1e-6))
예제 #10
0
 def value(self, t):
     return self.A + self.B * (1 / (t - self.t_0) ** self.m +
                               cos(self.omega * log(t - self.t_0) + self.phi))
예제 #11
0
    if y is None:
        x = asarray(x)
        y = empty(x.shape, dtype=nx.bool_)
    umath.logical_and(isinf(x), ~signbit(x), y)
    return y

def isneginf(x, y=None):
    """Return a boolean array y with y[i] True for x[i] = -Inf.

    If y is an array, the result replaces the contents of y.
    """
    if y is None:
        x = asarray(x)
        y = empty(x.shape, dtype=nx.bool_)
    umath.logical_and(isinf(x), signbit(x), y)
    return y

_log2 = umath.log(2)
def log2(x, y=None):
    """Returns the base 2 logarithm of x

    If y is an array, the result replaces the contents of y.
    """
    x = asanyarray(x)
    if y is None:
        y = umath.log(x)
    else:
        umath.log(x, y)
    y /= _log2
    return y
예제 #12
0
 def power_law_guess(self, y_s):
     log_x, log_y = log(range(1, len(y_s))), log(y_s)
     alpha, logy0, _, _, _ = linregress(log_x[1:], log_y[1:])
     y0 = exp(logy0)
     return y0, alpha
예제 #13
0
# coding:utf-8

# 朴素贝叶斯 算法分类(即朴素贝叶斯分类器)测试

import numpy as np;
import bayes_lib as bys;
import log_lib as logg;
from bayes_lib import textParse, createVocabList, bagOfWords2VecMN, trainNB2,\
    classifyNB
from numpy import random
import numpy.core.umath as math

print math.log(1/25.0)

def loadDataSet():
    # 生成 6组 array组成的2维数组
    postingList = [['my', 'dog', 'has', 'flea', 'problems', 'help', 'please'],
                 ['maybe', 'not', 'take', 'him', 'to', 'dog', 'park', 'stupid'],
                 ['my', 'dalmation', 'is', 'so', 'cute', 'I', 'love', 'him'],
                 ['stop', 'posting', 'stupid', 'worthless', 'garbage'],
                 ['mr', 'licks', 'ate', 'my', 'steak', 'how', 'to', 'stop', 'him']
                 , ['quit', 'buying', 'worthless', 'dog', 'food', 'stupid']
                 ]
    classVec = [0, 1, 0, 1, 0, 1]  # 1 is abusive, 0 not
    return postingList, classVec

log1 = logg.Logger("bayes_test");
log1.setLogLevel(logg.LEVEL_DEBUG, True);

postList, classVec = loadDataSet();
myVocabList = bys.createVocabList(postList);
예제 #14
0
def log_spectr(wf, sr=None, tile_size=DFLT_TILE_SIZE, tile_step=None):
    return log(1 + spectr(wf, sr=sr, tile_size=tile_size, tile_step=tile_step))
예제 #15
0
    return y


def isneginf(x, y=None):
    """Return a boolean array y with y[i] True for x[i] = -Inf.

    If y is an array, the result replaces the contents of y.
    """
    if y is None:
        x = asarray(x)
        y = empty(x.shape, dtype=nx.bool_)
    umath.logical_and(isinf(x), signbit(x), y)
    return y


_log2 = umath.log(2)


def log2(x, y=None):
    """Returns the base 2 logarithm of x

    If y is an array, the result replaces the contents of y.
    """
    x = asanyarray(x)
    if y is None:
        y = umath.log(x)
    else:
        umath.log(x, y)
    y /= _log2
    return y
예제 #16
0
파일: ml.py 프로젝트: jpcoles/jcode
def run_sim(data, N0):
    """ Run the lensing simulation with the parameters provided in params.py.
    This consists of four steps:
    (1)  Choose different values for rE.
    (2)  Move the star across the sky and generate "observations". The first is unlensed.
    (3)  Compute the projection and covariance matrices.
    (4)  Computer the effective chi^2.

    data is a 3 dimensional array consisting of n 2D normalized surface brightness 
    distributions. Using shaplets as the basis function, the data is reconstructed
    using test masses of a lensing star. The marginalized likelihood for each
    test mass is returned.
    """

    N = N0 ** 2  # Total number of basis functions

    nepochs = data.shape[0]
    grid_size = data.shape[1]

    print "nepochs = %i grid_size=%i" % (nepochs, grid_size)
    assert nepochs > 0
    assert grid_size > 0

    # ---------------------------------------------------------------------------
    #
    # ---------------------------------------------------------------------------

    # The basis functions evaluated at the lensed positions
    L = empty((N, nepochs, grid_size, grid_size), numpy.float64)
    # Copied, flattened version of the above divided by sigma^2
    Lt = empty((N, nepochs * grid_size * grid_size), numpy.float64)

    # Projection of data on the model
    P = mat(empty((N, 1), numpy.float64))
    # Covariance matrix
    C_inv = mat(empty((N, N), numpy.float64))

    # Output array
    probs = empty((num_samples, 2), numpy.float64)

    # ---------------------------------------------------------------------------
    # Basis function constants, and hermite polynomials.
    # Precompute the coefficients and setup the actual form of each hermite
    # function now, since they are constant over the run.
    # ---------------------------------------------------------------------------

    # vals = [[n, 1.0/sqrt((2**n) * sqrt(pi) * factorial(n,1) * beta), hermite(n), 0,0]
    vals = [[n, 1.0 / sqrt((2 ** n) * sqrt(pi) * factorial(n, 1)), hermite(n), 0, 0] for n in xrange(N0)]

    sqrt_data = sqrt(data)
    beta2 = beta ** 2

    # ---------------------------------------------------------------------------
    # Now we start the simulation.
    #
    # (1) Choose different values for rE.
    # ---------------------------------------------------------------------------

    for i, rE in enumerate(linspace(rE_sample[0], rE_sample[1], num_samples)):

        # XXX: Just for some specific plots
        # if i not in [14]: continue
        # XXX: Just for some specific plots

        # -----------------------------------------------------------------------
        # (2) Move the star across the sky
        # -----------------------------------------------------------------------

        for t, z in star_track(nepochs):

            # -------------------------------------------------------------------
            # (2a) Generate an "observation". The first is unlensed.
            # -------------------------------------------------------------------
            if t == 0:
                print "%4i] rE=%f Epoch %i NO LENS" % (i, rE, t)
                xx = raytrace()
                mask = 1
            else:
                print "%4i] rE=%f Epoch %i @ %f,%f" % (i, rE, t, z.real, z.imag)
                xx = raytrace(rE, z)
                mask = star_mask(z)

            # -------------------------------------------------------------------
            # Basis function approximation
            # -------------------------------------------------------------------
            expreal = exp(-xx.real ** 2 / (2 * beta2))
            expimag = exp(-xx.imag ** 2 / (2 * beta2))
            for n, K, H, _, _ in vals:
                vals[n][3] = K * H(xx.real / beta) * expreal
                vals[n][4] = K * H(xx.imag / beta) * expimag

            n = 0
            for _, _, _, b1, _ in vals:
                for _, _, _, _, b2 in vals:
                    L[n, t] = b1 * b2 / beta * mask
                    n += 1

        # -----------------------------------------------------------------------
        # (3) Compute the projection and covariance matrices.
        # -----------------------------------------------------------------------

        for n in xrange(N):
            sum(L[n], out=P[n])
            Lt[n] = (L[n] / sqrt_data).flatten()

        print "Building C_inv"
        C_inv = mat(inner(Lt, Lt))
        print "Done"
        C = C_inv.I

        # -----------------------------------------------------------------------
        # (3a) Optionally calculate the basis function coefficients and plot
        # the reconstruction of the image.
        # -----------------------------------------------------------------------
        if 0:
            plot_reconstructions(i, data, rE, L, C, P, N)

        # -----------------------------------------------------------------------
        # (4) Computer the effective chi^2. Note that we do not subtract the
        # third term (effectively the gamma_tot) here; we leave that for the
        # plotting program to do. chi2 here will then be about equal to
        # gamma_tot.
        # -----------------------------------------------------------------------

        log_det = sum(log(eig(C, right=False).real))
        PCP = P.T * C * P

        chi2 = log_det + PCP

        probs[i] = rE, chi2
        print "rE,chi2 =", rE, chi2

    return probs
예제 #17
0
 def test_log1p(self):
     assert_almost_equal(ncu.log1p(0.2), ncu.log(1.2))
     assert_almost_equal(ncu.log1p(1e-6), ncu.log(1 + 1e-6))
예제 #18
0
#

numbers = logspace(1, 40, 5)
# [1.00000000e+01 5.62341325e+10 3.16227766e+20 1.77827941e+30
#  1.00000000e+40]
print(numbers)
print("%.2g" % numbers[0])  # 10
print("%.2g" % numbers[1])  # 5.6e+10
print("%.3g" % numbers[2])  # 3.16e+20
print("%.4g" % numbers[3])  # 1.778e+30
print("%.5g" % numbers[4])  # 1e+40

# -------------------------------------------------------------------------------------------------------------

a1 = array([1, 2, 3, 4, 5])
a1 = a1 + 5
print(a1)  # [ 6  7  8  9 10]

a2 = array([6, 1, 9, 3, 2])

a3 = a1 + a2  # vectorized operation
print(a3)  # [12  8 17 12 12]

# min, max, sqrt, pow, sort, etc
print(sin(a1))  # [-0.2794155   0.6569866   0.98935825  0.41211849 -0.54402111]
print(cos(a2))  # [ 0.96017029  0.54030231 -0.91113026 -0.9899925  -0.41614684]
print(log(a3))  # [2.48490665 2.07944154 2.83321334 2.48490665 2.48490665]

a4 = concatenate((a1, a2))
print(a4)  # [ 6  7  8  9 10  6  1  9  3  2]