About Me

My photo
Mumbai, Maharastra, India
He has more than 7.6 years of experience in the software development. He has spent most of the times in web/desktop application development. He has sound knowledge in various database concepts. You can reach him at viki.keshari@gmail.com https://www.linkedin.com/in/vikrammahapatra/ https://twitter.com/VikramMahapatra http://www.facebook.com/viki.keshari

Search This Blog

Sunday, November 3, 2019

Python: Multiprocessing with 4 Core CPU

We have some processing to perform on existing Dataframe, where will try to add few columns on the bases of existing columns values, this we will try to do it serially and then compare the output performance with multiprocessing

Below we have a definition in the file, which takes a dataframe as an input and add 9 further columns in it based on existing column value and some mathematical expression.

import pandas as pd
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import multiprocessing as mp
import time


def operationonDFs(dfinput):
    dfinput['z'] = dfinput.apply(lambda newCol: 2 * newCol['x'] , axis = 1)
    dfinput['a'] = dfinput.apply(lambda newCol: 3.4 * newCol['x'] , axis = 1)
    dfinput['b'] = dfinput.apply(lambda newCol: 2.1 * newCol['x'] , axis = 1)
    dfinput['c'] = dfinput.apply(lambda newCol: 5.9 * newCol['x'] , axis = 1)
    dfinput['d'] = dfinput.apply(lambda newCol: 7.3 * newCol['x'] , axis = 1)
    dfinput['d'] = dfinput.apply(lambda newCol: 3.3 * newCol['x'] , axis = 1)
    dfinput['e'] = dfinput.apply(lambda newCol: 7.1 * newCol['x'] , axis = 1)
    dfinput['f'] = dfinput.apply(lambda newCol: 4.3 * newCol['x'] , axis = 1)
    dfinput['g'] = dfinput.apply(lambda newCol: 5.3 * newCol['x'] , axis = 1)
    return dfinput

Now in second part I have created a dataframe and call the def operationDfs by passing the newly created dataframe, here if you see the we have used time package to record the total execution time to run the entire program.

if __name__ ==  '__main__':
    start_time = time.time()   

    dfinput = pd.DataFrame({'x':range(1,100000),
                       'y':range(1,100000)})

    df = operationonDFs(dfinput)
   
    print(df)
    print("--- %s seconds ---" % (time.time() - start_time))

Lets see the output

C:\Users\Atoshi\mypy>python operationonDFWOMP.py
           x      y       z         a         b         c         d         e         f         g
0          1      1       2       3.4       2.1       5.9       3.3       7.1       4.3       5.3
1          2      2       4       6.8       4.2      11.8       6.6      14.2       8.6      10.6
2          3      3       6      10.2       6.3      17.7       9.9      21.3      12.9      15.9
3          4      4       8      13.6       8.4      23.6      13.2      28.4      17.2      21.2
4          5      5      10      17.0      10.5      29.5      16.5      35.5      21.5      26.5
...      ...    ...     ...       ...       ...       ...       ...       ...       ...       ...
99994  99995  99995  199990  339983.0  209989.5  589970.5  329983.5  709964.5  429978.5  529973.5
99995  99996  99996  199992  339986.4  209991.6  589976.4  329986.8  709971.6  429982.8  529978.8
99996  99997  99997  199994  339989.8  209993.7  589982.3  329990.1  709978.7  429987.1  529984.1
99997  99998  99998  199996  339993.2  209995.8  589988.2  329993.4  709985.8  429991.4  529989.4
99998  99999  99999  199998  339996.6  209997.9  589994.1  329996.7  709992.9  429995.7  529994.7

[99999 rows x 10 columns]
--- 24.10855269432068 seconds ---
So it took 24 second to execute the program.

Lets re-write the program to use multiprocessing, first importing multiprocessing package in our program and checking the number of cpu core available in our system using cpu_count method of multiprocessing.

import pandas as pd
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import multiprocessing as mp
import time

def operationonDFs(dfinput):
    dfinput['z'] = dfinput.apply(lambda newCol: 2 * newCol['x'] , axis = 1)
    dfinput['a'] = dfinput.apply(lambda newCol: 3.4 * newCol['x'] , axis = 1)
    dfinput['b'] = dfinput.apply(lambda newCol: 2.1 * newCol['x'] , axis = 1)
    dfinput['c'] = dfinput.apply(lambda newCol: 5.9 * newCol['x'] , axis = 1)
    dfinput['d'] = dfinput.apply(lambda newCol: 7.3 * newCol['x'] , axis = 1)
    dfinput['d'] = dfinput.apply(lambda newCol: 3.3 * newCol['x'] , axis = 1)
    dfinput['e'] = dfinput.apply(lambda newCol: 7.1 * newCol['x'] , axis = 1)
    dfinput['f'] = dfinput.apply(lambda newCol: 4.3 * newCol['x'] , axis = 1)
    dfinput['g'] = dfinput.apply(lambda newCol: 5.3 * newCol['x'] , axis = 1)
    return dfinput


if __name__ ==  '__main__':
    start_time = time.time()
   
    cpu_count = mp.cpu_count()
    no_of_split = 50

    dfinput = pd.DataFrame({'x':range(1,100000),
                       'y':range(1,100000)})

    dfinput_split = np.array_split(dfinput, no_of_split)
    pool = mp.Pool(cpu_count)
    df = pd.concat(pool.map(operationonDFs, dfinput_split))
    pool.close()
    pool.join()

    print(df)
    print("--- %s seconds ---" % (time.time() - start_time))

We have used pool class, the pool distributes the tasks to the available processors using a FIFO scheduling. It works like a map reduce architecture. It maps the input to the different processors and collects the output from all the processors.
The input to the pool.map method is definition which we want to execute in parallel with the splited dataframe.
Once the execution is finished, it joins all the output to form a single set of dataframe.

Let’s see how much we save with this architecture:

 C:\Users\Atoshi\mypy>python operationonDF.py
           x      y       z         a         b         c         d         e         f         g
0          1      1       2       3.4       2.1       5.9       3.3       7.1       4.3       5.3
1          2      2       4       6.8       4.2      11.8       6.6      14.2       8.6      10.6
2          3      3       6      10.2       6.3      17.7       9.9      21.3      12.9      15.9
3          4      4       8      13.6       8.4      23.6      13.2      28.4      17.2      21.2
4          5      5      10      17.0      10.5      29.5      16.5      35.5      21.5      26.5
...      ...    ...     ...       ...       ...       ...       ...       ...       ...       ...
99994  99995  99995  199990  339983.0  209989.5  589970.5  329983.5  709964.5  429978.5  529973.5
99995  99996  99996  199992  339986.4  209991.6  589976.4  329986.8  709971.6  429982.8  529978.8
99996  99997  99997  199994  339989.8  209993.7  589982.3  329990.1  709978.7  429987.1  529984.1
99997  99998  99998  199996  339993.2  209995.8  589988.2  329993.4  709985.8  429991.4  529989.4
99998  99999  99999  199998  339996.6  209997.9  589994.1  329996.7  709992.9  429995.7  529994.7

[99999 rows x 10 columns]
--- 13.999533653259277 seconds ---

That’s really a good save compare to serial method.
Serial Method: 24 sec
With Multiprocessing: 13 sec


Data Science with…Python J
Post Reference: Vikram Aristocratic Elfin Share

Saturday, November 2, 2019

Performance of Various Slicing Method of Pandas Dataframe

There are multiple way to slice your data, but let’s see which way is the most efficient way to slice the data. Let’s take a Dataframe and check the various data slicing

import pandas as pd
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import glob

df = pd.DataFrame({'A': 'aaa bbb xxx zzz aaa aaa kkk aaa'.split(),
                   'B': 'one two two three two two one three'.split(),
                   'C': np.arange(8), 'D': np.arange(8) * 2})
df
Output:
         A        B        C        D
0        aaa      one      0        0
1        bbb      two      1        2
2        xxx      two      2        4
3        zzz      three    3        6
4        aaa      two      4        8
5        aaa      two      5        10
6        kkk      one      6        12
7        aaa      three    7        14               
 +-     

Now lets slice the data with A=’aaa’ condition using various methods

%timeit -n 1000 df[df['A'].values == 'aaa']
%timeit -n 1000 df[df['A'] == 'aaa']
%timeit -n 1000 df.query('A == "aaa"')
%timeit -n 1000 df[df["A"]=='aaa']
%timeit -n 1000 df[df["A"].isin(['aaa'])]
%timeit -n 1000 df.set_index('A', append=True, drop=False).xs('aaa', level=1,drop_level=True)
%timeit -n 1000 df.iloc[np.where(df['A']=='aaa')]


Output

454 µs ± 7.97 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
787 µs ± 4.77 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
1.8 ms ± 18.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
787 µs ± 8.38 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
710 µs ± 6.78 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
2.58 ms ± 233 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
855 µs ± 6.98 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

Here you can see the best performance is achived using dataframe.where method and second lead is taken by cross section of index method.
The lowest among all was showed by iloc method.
erience outperform over pandas.series.

Data Science with…Python J
Post Reference: Vikram Aristocratic Elfin Share

Thursday, October 31, 2019

Performance of Slicing Dataframe through Boolean Index with numpy.array vs pandas.series

There are multiple ways to slice your dataframe with given filter criteria, but my preferable way is to do it with Boolean Index. But in Boolean index itself, you need to make a choice of either going with numpy.array to form your matching Boolean set or using pandas.series to form your matching Boolean set

Lets directly jump into practical to find the optimal of these two option, here below I am creating a dataframe  of (3,4) size and let’s suppose we need to slice our dataframe with condition of col1 which is “A” = ‘aaa’.


import pandas as pd
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import glob

import pandas as pd, numpy as np

df = pd.DataFrame({'A': 'aaa bbb xxx zzz aaa aaa kkk aaa'.split(),
                   'B': 'one two two three two two one three'.split(),
                   'C': np.arange(8), 'D': np.arange(8) * 2})
df
Output:
         A        B        C        D
0        aaa      one      0        0
1        bbb      two      1        2
2        xxx      two      2        4
3        zzz      three    3        6
4        aaa      two      4        8
5        aaa      two      5        10
6        kkk      one      6        12
7        aaa      three    7        14      


Now here we are trying to form our matching Boolean set, by
Numpy.array and
Pandas.series

Which consist of TRUE value in the index where there is a match i.e. A=’aaa’

matchSeries = df['A'] == 'aaa'
matchNumpyArry =df['A'].values=='aaa'

display(type(matchSeries),type(matchNumpyArry))

Output
pandas.core.series.Series
numpy.ndarray

Lets try to find out how long it takes to form the Boolean set of conditional match, here we are trying to check the performance by calling 7*1000 times the same operation 

%timeit -n 1000 matchNumpyArry = df['A'].values == 'aaa'
%timeit -n 1000 matchSeries = df['A'] == 'aaa'

Output
7 µs ± 1.82 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
284 µs ± 8.19 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)       

You can see from the above output that numpy.arry outperform, the mean of numpy.arry is 7 micro seconds which is ~40 times faster then pandas.series which takes mean time as 284 micro sec.

Lets try a negative test where we are trying to match a negative criteria, here also you can see from the result that numpy.array outperform pandas.series

%timeit -n 1000 matchNumpyArry = df['A'].values == 'xyz'
%timeit -n 1000 matchSeries = df['A'] == 'xyz'

Output

17 µs ± 1.82 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
284 µs ± 8.19 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
        

Lets now use this Boolean set to slice the dataframe and check the cost of slicing using numpy.array vs pandas.series
Here below we can see there is not much time saving with 1000*7 run but still numpy is leading

%timeit -n 1000 df[matchNumpyArry]
%timeit -n 1000 df[matchSeries]

Output

448 µs ± 32 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
510 µs ± 25.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)       

Result of dataframe after slicing from both way:

display(df[matchNumpyArry],df[matchSeries])

Output

         A        B        C        D
0        aaa      one      0        0
4        aaa      two      4        8
5        aaa      two      5        10
7        aaa      three    7        14
        
         A        B        C        D
0        aaa      one      0        0
4        aaa      two      4        8
5        aaa      two      5        10
7        aaa      three    7        14

Conclusion: Using numpy.array overall slicing experience outperform over pandas.series.


Data Science with…Python J
Post Reference: Vikram Aristocratic Elfin Share