示例#1
0
 def test_measure_raise(self):
     ctx = [
         dict(x_name2=i,
              va=numpy.arange(i),
              vb=numpy.arange(10) - 5,
              pydot=pydot) for i in range(10, 10000, 100)
     ]
     with self.assertRaises(ValueError):
         list(measure_time_dim('pydot(va, vb)', ctx, verbose=1))
示例#2
0
 def test_measure_time_dim(self):
     ctx = [
         dict(x_name=i,
              va=numpy.arange(i),
              vb=numpy.arange(10) - 5,
              pydot=pydot) for i in range(10, 10000, 100)
     ]
     ms = list(measure_time_dim('pydot(va, vb)', ctx, verbose=1))
     self.assertIsInstance(ms, list)
     self.assertEqual(len(ms), 100)
示例#3
0
    ctxs = [
        dict(va=numpy.random.randn(n).astype(dtype),
             vb=numpy.random.randn(n).astype(dtype),
             dot=fct,
             x_name=n) for n in range(10, n, h)
    ]
    return ctxs


##############################
# numpy dot
# +++++++++
#

ctxs = get_vectors(numpy.dot, 10000)
df = DataFrame(list(measure_time_dim('dot(va, vb)', ctxs, verbose=1)))
df['fct'] = 'numpy.dot'
print(df.tail(n=3))
dfs = [df]

##############################
# Several cython dot
# ++++++++++++++++++
#

for fct in [
        dot_product, ddot_cython_array, ddot_cython_array_optim, ddot_array,
        ddot_array_16, ddot_array_16_sse
]:
    ctxs = get_vectors(fct, 10000 if fct.__name__ != 'dot_product' else 1000)
示例#4
0
dfs = []
sets = list(range(2, 145, 20))

##############################
# numpy mul
# +++++++++
#

ctxs = [
    dict(va=numpy.random.randn(n, n).astype(numpy.float64),
         vb=numpy.random.randn(n, n).astype(numpy.float64),
         mul=lambda x, y: x @ y,
         x_name=n) for n in sets
]

res = list(measure_time_dim('mul(va, vb)', ctxs, verbose=1))
dfs.append(DataFrame(res))
dfs[-1]['fct'] = 'numpy'
pprint.pprint(dfs[-1].tail(n=2))

##############################
# Simple multiplication
# +++++++++++++++++++++
#

ctxs = [
    dict(va=numpy.random.randn(n, n).astype(numpy.float64),
         vb=numpy.random.randn(n, n).astype(numpy.float64),
         mul=dmul_cython_omp,
         x_name=n) for n in sets
]
##############################
# python dot: pydot
# +++++++++++++++++
#
# The first function :func:`pydot
# <td3a_cpp.tutorial.pydot>` uses
# python to implement the dot product.

ctxs = [
    dict(va=numpy.random.randn(n).astype(numpy.float64),
         vb=numpy.random.randn(n).astype(numpy.float64),
         pydot=pydot,
         x_name=n) for n in range(10, 1000, 100)
]

res_pydot = list(measure_time_dim('pydot(va, vb)', ctxs, verbose=1))

pprint.pprint(res_pydot[:2])

##############################
# numpy dot
# +++++++++
#

ctxs = [
    dict(va=numpy.random.randn(n).astype(numpy.float64),
         vb=numpy.random.randn(n).astype(numpy.float64),
         dot=numpy.dot,
         x_name=n) for n in range(10, 50000, 100)
]
示例#6
0
def numpy_filter(va, mx):
    va[va > mx] = mx


all_res = []
for fct in [numpy_filter,
            pyfilter_dmax, filter_dmax_cython,
            filter_dmax_cython_optim,
            cyfilter_dmax,
            cfilter_dmax, cfilter_dmax2,
            cfilter_dmax16, cfilter_dmax4]:

    print(fct)
    ctxs = get_vectors(fct, 1000 if fct == pyfilter_dmax else 40000)
    res = list(measure_time_dim('fil(va, mx)', ctxs, verbose=1))
    for r in res:
        r['fct'] = fct.__name__
    all_res.extend(res)

pprint.pprint(all_res[:2])

#############################
# Let's display the results
# +++++++++++++++++++++++++

cc = DataFrame(all_res)
cc['N'] = cc['x_name']

fig, ax = plt.subplots(2, 2, figsize=(10, 10))
cc[cc.N <= 1100].pivot('N', 'fct', 'average').plot(
    y = y.astype(numpy.float64)
    fct = ElasticNet(alpha=alpha, l1_ratio=L1_ratio)
    ctxs = [
        dict(X=X, y=y, linear_regression=fct.fit, x_name=n)
        for n in range(10, n, h)
    ]
    return ctxs


## Get time execution

# sklearn ElasticNet

ctxs = get_vectors_elastic(100000)
df = DataFrame(
    list(measure_time_dim('linear_regression(X, y)', ctxs, verbose=1)))
df['fct'] = 'ElasticNet'
print(df.tail(n=3))
dfs = [df]

# naive & c++ implemantation

for fct in [nv_regular_linreg, cpp_regular_linreg]:
    ctxs = get_vectors(fct, 100000)

    df = DataFrame(
        list(
            measure_time_dim(
                'linear_regression(X, y, beta, alpha, L1_ratio, max_iter, tol, num_samples, num_features)',
                ctxs,
                verbose=1)))
def get_vectors_sklearn(n, h=10, dtype=numpy.float64):
    X, y = make_blobs(n_samples=n, centers=3, n_features=4)
    _, p = X.shape
    X = X.astype(numpy.float64)
    y = y.astype(numpy.float64)
    fct = RandomForestClassifier(n_estimators=5, max_depth=2, max_features=2)
    ctxs = [dict(X=X,
                 y=y,
                 RandomForestClassifier=fct.fit,
                 x_name=n
                 )
            for n in range(10, n, h)]
    return ctxs

ctxs = get_vectors_sklearn(100)
df = DataFrame(list(measure_time_dim('RandomForestClassifier(X,y)', ctxs, verbose=1)))
df['fct'] = 'RandomForestClassifier'
print(df.tail(n=3))
dfs = [df]


ctxs = get_vectors_cpp(create_forest_prime, 100)
df = DataFrame(list(measure_time_dim('random_forest(data, n_trees, max_depth, max_x)', ctxs, verbose=1)))
df['fct'] = create_forest_prime.__name__
dfs.append(df)
print(df.tail(n=3))

ctxs = get_vectors_nv(random_forest, 100)
df = DataFrame(list(measure_time_dim('random_forest(data, max_depth, min_size, n_trees, max_x)', ctxs, verbose=1)))
df['fct'] = random_forest.__name__
dfs.append(df)