Tags down


Can i use Numba to make this piece of python code run fast?

By : Anna Gwynne
Date : September 16 2020, 02:00 AM
this one helps. you could numba-njit a Python implementation of the Haversine distance:
code :
from math import sin, cos, sqrt, atan2, radians
from numba import njit

def calc_latlon_dist(lat, lon):
    calculate Haversine distance along lat/lon coordinates
    R = 6373.0 # approximate radius of earth in km
    dist = 0.

    for j in range(lat.shape[0]-1):
        lat0, lat1 = radians(lat[j]), radians(lat[j+1])
        lon0, lon1 = radians(lon[j]), radians(lon[j+1])

        dlon = lon1 - lon0
        dlat = lat1 - lat0

        a = sin(dlat / 2)**2 + cos(lat0) * cos(lat1) * sin(dlon / 2)**2
        c = 2 * atan2(sqrt(a), sqrt(1 - a))

        dist += R * c

    return dist

Share : facebook icon twitter icon

How does this piece of code function as fast i/o?

By : kamlesh
Date : March 29 2020, 07:55 AM
it should still fix some issue I am unable to comprehend how this function results in faster i/o in c++. please help me out!! , Assuming first line is: int ch = getchar();
code :

Is there any way I can make this small piece of code shorter? (Python)

By : vincent.lee
Date : March 29 2020, 07:55 AM
wish helps you
code :
while all(0 in i for i in [first,second,third,fourth]):
while any(0 in i for i in [first,second,third,fourth]):

Python - Can you help me make a class from a piece of code I'm repeating?

By : user2546985
Date : March 29 2020, 07:55 AM
Any of those help Just look, which values are constant between this lines and wrap them inside a function and define the others to be parameters.
code :
def make_label(my_text, my_offset):
      temp = tk.Label(frame, text=my_text, anchor='nw')
      temp.config(width=30, font=('Microsoft JhengHei Light', 15), bg='#08295e', fg='white')
      temp.place(relx=0.05, rely=my_offset)
      return temp

instruct1 = make_label('Step1: Do step 1', 0.1)       
instruct2 = make_label('Step2: Do step 2', 0.2)

numpy faster than numba and cython , how to improve numba code

By : Blaise Vignon
Date : March 29 2020, 07:55 AM
should help you out As we will see the behavior is dependent on which numpy-distribution is used.
This answer will focus on Anacoda-distribution with Intel's VML (vector math library), millage can vary given another hardware and numpy-version.
code :
N,M=2*10**4, 10**3
a=np.random.rand(N, M)
%timeit py_expsum(a)  #   87ms
%timeit nb_expsum(a)  #  672ms
%timeit nb_expsum2(a)  #  412ms
Overhead  Command  Shared Object                                      Symbol                                                             
  62,56%  python   libm-2.23.so                                       [.] __ieee754_exp_avx
  16,16%  python   libm-2.23.so                                       [.] __GI___exp
   5,25%  python   perf-28936.map                                     [.] 0x00007f1658d53213
   2,21%  python   mtrand.cpython-37m-x86_64-linux-gnu.so             [.] rk_random
  31,84%  python   libmkl_vml_avx.so                                  [.] mkl_vml_kernel_dExp_E9HAynn                                   ▒
   9,47%  python   libiomp5.so                                        [.] _INTERNAL_25_______src_kmp_barrier_cpp_38a91946::__kmp_wait_te▒
   6,21%  python   [unknown]                                          [k] 0xffffffff8140290c                                            ▒
   5,27%  python   mtrand.cpython-37m-x86_64-linux-gnu.so             [.] rk_random  
%%cython -L=<path_mkl_libs> --link-args=-Wl,-rpath=<path_mkl_libs> --link-args=-Wl,--no-as-needed -l=mkl_intel_ilp64 -l=mkl_core -l=mkl_gnu_thread -l=iomp5
# path to mkl can be found via np.show_config()
# which libraries needed: https://software.intel.com/en-us/articles/intel-mkl-link-line-advisor

# another option would be to wrap mkl.h:
cdef extern from *:
    // MKL_INT is 64bit integer for mkl-ilp64
    // see https://software.intel.com/en-us/mkl-developer-reference-c-c-datatypes-specific-to-intel-mkl
    #define MKL_INT long long int
    void  vdExp(MKL_INT n, const double *x, double *y);
    void vdExp(long long int n, const double *x, double *y)

def cy_expsum(const double[:,:] v):
            double[1024] w;
            int n = v.size
            int current = 0;
            double res = 0.0
            int size = 0
            int i = 0
        while current<n:
            size = n-current
            if size>1024:
                size = 1024
            vdExp(size, &v[0,0]+current, w)
            for i in range(size):
        return res
 import numexpr as ne
 def ne_expsum(x):
     return ne.evaluate("sum(exp(x))")
import numpy as np
def py_expsum(x):
    return np.sum(np.exp(x))

import numba as nb
@nb.jit( nopython=True)    
def nb_expsum(x):
    nx, ny = x.shape
    val = 0.0
    for ix in range(nx):
        for iy in range(ny):
            val += np.exp( x[ix, iy] )
    return val

@nb.jit( nopython=True, parallel=True)    
def nb_expsum2(x):
    nx, ny = x.shape
    val = 0.0
    for ix in range(nx):
        for iy in nb.prange(ny):
            val += np.exp( x[ix, iy]   )
    return val

import perfplot
factor = 1.0 # 0.0 or 1e4
    setup=lambda n: factor*np.random.rand(1,n),
    n_range=[2**k for k in range(0,27)],

How can I make Numba access arrays as fast as Numpy can?

By : Shauno
Date : March 29 2020, 07:55 AM
it should still fix some issue
I'm not expecting Numba to be faster but certainly not 70% slower!
code :
%load_ext cython

cimport cython

@cython.boundscheck(False)  # Deactivate bounds checking
@cython.wraparound(False)   # Deactivate negative indexing.
cpdef copyto_cython(double[::1] in_array, double[::1] out_array):
    cdef Py_ssize_t idx
    for idx in range(len(in_array)):
        out_array[idx] = in_array[idx]
import numpy as np
import numba as nb

def copyto_numpy(a, b):
    np.copyto(a, b, 'no')

def copyto_numba(a, b):
    N = len(a)
    for i in range(N):
        b[i] = a[i]

from simple_benchmark import BenchmarkBuilder, MultiArgument

b = BenchmarkBuilder()

b.add_functions([copyto_cython, copyto_numpy, copyto_numba])

@b.add_arguments('array size')
def argument_provider():
    for exp in range(4, 21):
        size = 2**exp
        arr = np.random.rand(size)
        arr2 = np.empty(size)
        yield size, MultiArgument([arr, arr2])

r = b.run()
Related Posts Related Posts :
  • Processing my dataframes with conditions - python jupyter notebook
  • How to remove section of string in pandas dataframe?
  • New column based on a filter and an index of multiples columns?
  • High Precision Word Alignment Algorithm in Python
  • Optimise this function -- numpy broadcasting issue
  • How to run django files in another virtualenv environment on another server?
  • Wrong plotting in bokeh
  • can pytest ignore a specific warning?
  • Pandas column value arrangement
  • No Module Named Zeep - Python
  • Rolling stats pandas with based of fixed period or available data
  • Why are model predictions not updating in tensorflow (python)?
  • in python how to make a dictionary of a function of lists
  • Multilingual Bert sentence vector captures language used more than meaning - working as interned?
  • How does one fix the issue of not writable paths with conda?
  • In python, how to compare two lists: same length and could have duplicate elements
  • Getting min and max values from Pandas dataframe that uses d, w, m, y to represent days, weeks, months, years
  • Round to nearest hour pd.Timedelta
  • How to display subfolders by numerical order in python
  • asyncio loop.run_until_complete not sleeping with asyncio.sleep
  • How to pass a greater than or less than sign through a parameter?
  • Pythonic way to set variable from another sublist match
  • Breaking the Python code into multiple lines
  • Temporarily remove item from list
  • How can I remove rows in Pandas based on the combined sum of multiple values?
  • Machine learning and SVM
  • Python editing in a list based on the data type of an element
  • Python: Function always returning zero from loop
  • How to extract data using groupby under specific condition?
  • Why does Python int have a __round__ method?
  • Sort values by columns and not rows
  • Memory not freed after Python's multiprocessing Pool is finished
  • Order list of tuples by multiple elements in both ascending and descending order
  • Related Field got invalid lookup: blog_posts_name
  • How to smooth signals statistically correct in Python?
  • jupyter-console fails to start, but jupyter-notebook is fine
  • How to add a Validation and Test Set in Pytorch Model?
  • lambdify returns TypeError: _lambdifygenerated() missing 1 required positional argument: 'k'
  • Invalid Syntax on 'column name' When Dropping Rows based on Column Values
  • Direct instantiation of `typing.Union` in Python
  • Put forward a chart with matplotlib
  • Strange behavior when initializing a list of lists in Python 3
  • Complications in for loop for Capitalizing words in a string in Python 3
  • Write every combination from 2 CSV files
  • Is it possible to move the last character of a column to the first character of another column in excel or using python
  • Check if servers are available using one client in python
  • Getting exception message
  • Override __new__() to replace class method with default arguments
  • How to find the point on a circle when a point within it is projected out on to it?
  • BigQuery: How to set `require_partition_filter` by TableReference
  • Connect Cloud Storage to Cloud Function
  • Managing User Authentication for a web-app with MySQL
  • How to concatenate multiple variables?
  • Why does this result in a syntax error? Is it something to do with the Boolean value?
  • why am i getting attribute error "AttributeError: 'Values' object has no attribute 'interface'"
  • Progress bar for loading large Pickle objects?
  • remove duplicates without using remove or fromkeys;list slicing; python
  • How to create encrypted password in django rest framework
  • Create a column name based on today date?
  • Jupyter - widget to play audio with playhead on graph
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk