示例#1
0
def read_example():
    t1 = time.time()
    A = sdc.ros.read_ros_images("image_test.bag")
    # crop out dashboard
    B = A[:, :-50, :, :]
    # intensity threshold
    threshold = B.mean() + .004 * B.std()
    n = B.shape[0]
    mask = np.empty(n, np.bool_)
    for i in prange(n):
        im = B[i]
        mask[i] = im.mean() > threshold
    C = B[mask]
    D = np.empty_like(C)
    for i in prange(len(C)):
        D[i, :, :, 0] = gaussian_blur(C[i, :, :, 0])
        D[i, :, :, 1] = gaussian_blur(C[i, :, :, 1])
        D[i, :, :, 2] = gaussian_blur(C[i, :, :, 2])
    # K-means model
    numCenter = 4
    numIter = 10
    dn, dh, dw, dc = D.shape
    centroids = np.random.randint(0, 255,
                                  (numCenter, dh, dw, dc)).astype(np.uint8)
    for l in range(numIter):
        dist = np.array([[
            sqrt(np.sum((D[i] - centroids[j])**2)) for j in range(numCenter)
        ] for i in range(dn)])
        labels = np.array([dist[i].argmin() for i in range(dn)])
        for i in range(numCenter):
            mask2 = (labels == i)
            num_points = np.sum(mask2)
            if num_points != 0:
                centroids[i] = np.sum(D[mask2], 0) / num_points
            else:
                centroids[i] = np.random.randint(0, 255,
                                                 (dh, dw, dc)).astype(np.uint8)

    t2 = time.time()
    print("Exec time: ", t2 - t1)
    return centroids
示例#2
0
文件: test_ml.py 项目: sklam/sdc
 def test_impl(n):
     X = np.ones(n)
     b = 0.5
     points = np.array([-1.0, 2.0, 5.0])
     N = points.shape[0]
     exps = 0
     for i in sdc.prange(n):
         p = X[i]
         d = (-(p - points)**2) / (2 * b**2)
         m = np.min(d)
         exps += m - np.log(b * N) + np.log(np.sum(np.exp(d - m)))
     return exps
示例#3
0
 def test_impl(nsyms):
     max_num_days = 100
     all_res = 0.0
     for i in sdc.prange(nsyms):
         s_open = 20 * np.ones(max_num_days)
         s_low = 28 * np.ones(max_num_days)
         s_close = 19 * np.ones(max_num_days)
         df = pd.DataFrame({'Open': s_open, 'Low': s_low, 'Close': s_close})
         df['Stdev'] = df['Close'].rolling(window=90).std()
         df['Moving Average'] = df['Close'].rolling(window=20).mean()
         df['Criteria1'] = (df['Open'] - df['Low'].shift(1)) < -df['Stdev']
         df['Criteria2'] = df['Open'] > df['Moving Average']
         df['BUY'] = df['Criteria1'] & df['Criteria2']
         df['Pct Change'] = (df['Close'] - df['Open']) / df['Open']
         df['Rets'] = df['Pct Change'][df['BUY']]
         all_res += df['Rets'].mean()
     return all_res
示例#4
0
def kde():
    t = pq.read_table('hdfs://localhost:9016/user/etotoni/kde.parquet')
    df = t.to_pandas()
    X = df['points'].values
    b = 0.5
    points = np.array([-1.0, 2.0, 5.0])
    N = points.shape[0]
    n = X.shape[0]
    exps = 0
    t1 = time.time()
    for i in prange(n):
        p = X[i]
        d = (-(p - points)**2) / (2 * b**2)
        m = np.min(d)
        exps += m - np.log(b * N) + np.log(np.sum(np.exp(d - m)))
    t = time.time() - t1
    print("Execution time:", t, "\nresult:", exps)
    return exps
示例#5
0
def intraday_mean_revert():
    nsyms = 1000
    max_num_days = 80000
    all_res = 0.0

    t1 = time.time()
    for i in prange(nsyms):
        # np.random.seed(0)
        s_open = 20 * np.random.randn(max_num_days)
        s_low = 18 * np.random.randn(max_num_days)
        s_close = 19 * np.random.randn(max_num_days)
        df = pd.DataFrame({'Open': s_open, 'Low': s_low, 'Close': s_close})

        # create column to hold our 90 day rolling standard deviation
        df['Stdev'] = df['Close'].rolling(window=90).std()

        # create a column to hold our 20 day moving average
        df['Moving Average'] = df['Close'].rolling(window=20).mean()

        # create a column which holds a TRUE value if the gap down from previous day's low to next
        # day's open is larger than the 90 day rolling standard deviation
        df['Criteria1'] = (df['Open'] - df['Low'].shift(1)) < -df['Stdev']

        # create a column which holds a TRUE value if the opening price of the stock is above the 20 day moving average
        df['Criteria2'] = df['Open'] > df['Moving Average']

        # create a column that holds a TRUE value if both above criteria are also TRUE
        df['BUY'] = df['Criteria1'] & df['Criteria2']

        # calculate daily % return series for stock
        df['Pct Change'] = (df['Close'] - df['Open']) / df['Open']

        # create a strategy return series by using the daily stock returns where the trade criteria above are met
        df['Rets'] = df['Pct Change'][df['BUY']]

        all_res += df['Rets'].mean()

    print(all_res)
    print("execution time:", time.time() - t1)