Hướng dẫn python average speed code - mã tốc độ trung bình python

Tôi muốn tính toán tốc độ trung bình cho mỗi ID, tôi đã sử dụng mã này

Show
df_Speed=df2.groupby('ID').agg(Total_Speed=('speed Km/h', 'sum'),Total_steps=('ID', 'count')).reset_index()
df_Speed['Avg_Speed']=df_Speed['Total_Speed']/df_Speed['Total_steps']

df_Speed.head()

Nhưng tôi nhận được Inf như một tốc độ!

ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036

Làm thế nào tôi có thể có được tốc độ thay vì INF này

Trong phần này của hướng dẫn, chúng tôi sẽ điều tra cách tăng tốc các chức năng nhất định hoạt động trên gấu trúc

In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
7 bằng ba kỹ thuật khác nhau: Cython, Numba và
In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
8. Chúng ta sẽ thấy sự cải thiện tốc độ ~ 200 khi chúng ta sử dụng Cython và Numba trên một chức năng thử nghiệm hoạt động khôn ngoan trên
In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
7. Sử dụng
In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
8, chúng tôi sẽ tăng tốc một khoản tiền theo thứ tự ~ 2.

Ghi chú

Ngoài việc làm theo các bước trong hướng dẫn này, người dùng quan tâm đến việc tăng cường hiệu suất được khuyến khích cao để cài đặt các phụ thuộc được đề xuất cho gấu trúc. Những phụ thuộc này thường không được cài đặt theo mặc định, nhưng sẽ cung cấp cải tiến tốc độ nếu có.recommended dependencies for pandas. These dependencies are often not installed by default, but will offer speed improvements if present.

Cython (Viết phần mở rộng C cho gấu trúc)#

Đối với nhiều trường hợp sử dụng viết gấu trúc trong Python và Numpy thuần túy là đủ. Tuy nhiên, trong một số ứng dụng nặng về mặt tính toán, có thể đạt được tốc độ tăng tốc khá lớn bằng cách giảm tải công việc cho Cython.

Hướng dẫn này giả định rằng bạn đã tái cấu trúc càng nhiều càng tốt trong Python, ví dụ bằng cách cố gắng loại bỏ các vòng lặp và sử dụng vector hóa numpy. Nó luôn luôn đáng để tối ưu hóa trong Python trước.

Hướng dẫn này đi qua một quá trình điển hình của người Viking về việc tính toán chậm. Chúng tôi sử dụng một ví dụ từ tài liệu Cython nhưng trong bối cảnh của gấu trúc. Giải pháp cythonized cuối cùng của chúng tôi nhanh hơn khoảng 100 lần so với dung dịch Python tinh khiết.

Python thuần túy#

Chúng tôi có một

In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
7 mà chúng tôi muốn áp dụng một hàng chức năng khôn ngoan.

In [1]: df = pd.DataFrame(
   ...:     {
   ...:         "a": np.random.randn(1000),
   ...:         "b": np.random.randn(1000),
   ...:         "N": np.random.randint(100, 1000, (1000)),
   ...:         "x": "x",
   ...:     }
   ...: )
   ...: 

In [2]: df
Out[2]: 
            a         b    N  x
0    0.469112 -0.218470  585  x
1   -0.282863 -0.061645  841  x
2   -1.509059 -0.723780  251  x
3   -1.135632  0.551225  972  x
4    1.212112 -0.497767  181  x
..        ...       ...  ... ..
995 -1.512743  0.874737  374  x
996  0.933753  1.120790  246  x
997 -0.308013  0.198768  157  x
998 -0.079915  1.757555  977  x
999 -1.010589 -1.115680  770  x

[1000 rows x 4 columns]

Ở đây, chức năng trong Python thuần túy:

In [3]: def f(x):
   ...:     return x * (x - 1)
   ...: 

In [4]: def integrate_f(a, b, N):
   ...:     s = 0
   ...:     dx = (b - a) / N
   ...:     for i in range(N):
   ...:         s += f(a + i * dx)
   ...:     return s * dx
   ...: 

Chúng tôi đạt được kết quả của mình bằng cách sử dụng

In [6]: %prun -l 4 df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)  # noqa E999
         621314 function calls (621294 primitive calls) in 0.235 seconds

   Ordered by: internal time
   List reduced from 223 to 4 due to restriction <4>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1000    0.132    0.000    0.200    0.000 :1(integrate_f)
   552423    0.067    0.000    0.067    0.000 :1(f)
     3000    0.006    0.000    0.024    0.000 series.py:967(__getitem__)
     3000    0.003    0.000    0.012    0.000 series.py:1075(_get_value)
2 (hàng khôn ngoan):

In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)

Nhưng rõ ràng đây là đủ nhanh cho chúng tôi. Hãy cùng xem và xem thời gian dành thời gian trong hoạt động này (giới hạn trong bốn cuộc gọi tốn nhiều thời gian nhất) bằng cách sử dụng hàm ma thuật Prun Ipython:

In [6]: %prun -l 4 df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)  # noqa E999
         621314 function calls (621294 primitive calls) in 0.235 seconds

   Ordered by: internal time
   List reduced from 223 to 4 due to restriction <4>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1000    0.132    0.000    0.200    0.000 :1(integrate_f)
   552423    0.067    0.000    0.067    0.000 :1(f)
     3000    0.006    0.000    0.024    0.000 series.py:967(__getitem__)
     3000    0.003    0.000    0.012    0.000 series.py:1075(_get_value)

Cho đến nay, phần lớn thời gian được dành bên trong

In [6]: %prun -l 4 df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)  # noqa E999
         621314 function calls (621294 primitive calls) in 0.235 seconds

   Ordered by: internal time
   List reduced from 223 to 4 due to restriction <4>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1000    0.132    0.000    0.200    0.000 :1(integrate_f)
   552423    0.067    0.000    0.067    0.000 :1(f)
     3000    0.006    0.000    0.024    0.000 series.py:967(__getitem__)
     3000    0.003    0.000    0.012    0.000 series.py:1075(_get_value)
3 hoặc
In [6]: %prun -l 4 df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)  # noqa E999
         621314 function calls (621294 primitive calls) in 0.235 seconds

   Ordered by: internal time
   List reduced from 223 to 4 due to restriction <4>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1000    0.132    0.000    0.200    0.000 :1(integrate_f)
   552423    0.067    0.000    0.067    0.000 :1(f)
     3000    0.006    0.000    0.024    0.000 series.py:967(__getitem__)
     3000    0.003    0.000    0.012    0.000 series.py:1075(_get_value)
4, do đó chúng tôi sẽ tập trung các nỗ lực của mình để hóa hai chức năng này.

Cython đơn giản#

Đầu tiên, chúng tôi sẽ cần nhập chức năng ma thuật Cython vào Ipython:

Bây giờ, hãy để chỉ đơn giản là sao chép các chức năng của chúng tôi vào Cython như hiện tại (hậu tố ở đây để phân biệt giữa các phiên bản chức năng):

In [8]: %%cython
   ...: def f_plain(x):
   ...:     return x * (x - 1)
   ...: def integrate_f_plain(a, b, N):
   ...:     s = 0
   ...:     dx = (b - a) / N
   ...:     for i in range(N):
   ...:         s += f_plain(a + i * dx)
   ...:     return s * dx
   ...: 

Ghi chú

Nếu bạn gặp khó khăn trong việc dán ở trên vào ipython của mình, bạn có thể cần phải sử dụng Ipython Edge Edge để dán tốt để chơi tốt với các phép thuật di động.

In [9]: %timeit df.apply(lambda x: integrate_f_plain(x["a"], x["b"], x["N"]), axis=1)
61.5 ms +- 401 us per loop (mean +- std. dev. of 7 runs, 10 loops each)

Điều này đã cạo một phần ba, không quá tệ cho một bản sao và dán đơn giản.

Thêm loại#

Chúng tôi nhận được một cải tiến lớn khác chỉ bằng cách cung cấp thông tin loại:

In [10]: %%cython
   ....: cdef double f_typed(double x) except? -2:
   ....:     return x * (x - 1)
   ....: cpdef double integrate_f_typed(double a, double b, int N):
   ....:     cdef int i
   ....:     cdef double s, dx
   ....:     s = 0
   ....:     dx = (b - a) / N
   ....:     for i in range(N):
   ....:         s += f_typed(a + i * dx)
   ....:     return s * dx
   ....: 

In [11]: %timeit df.apply(lambda x: integrate_f_typed(x["a"], x["b"], x["N"]), axis=1)
12.9 ms +- 223 us per loop (mean +- std. dev. of 7 runs, 100 loops each)

Bây giờ chúng ta nói chuyện! Bây giờ, nó nhanh hơn gấp mười lần so với triển khai Python ban đầu và chúng tôi đã thực sự sửa đổi mã. Hãy để một cái nhìn khác về những gì mà ăn hết thời gian:

ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
0

Sử dụng Ndarray#

Nó gọi loạt phim rất nhiều! Nó tạo ra một

In [6]: %prun -l 4 df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)  # noqa E999
         621314 function calls (621294 primitive calls) in 0.235 seconds

   Ordered by: internal time
   List reduced from 223 to 4 due to restriction <4>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1000    0.132    0.000    0.200    0.000 :1(integrate_f)
   552423    0.067    0.000    0.067    0.000 :1(f)
     3000    0.006    0.000    0.024    0.000 series.py:967(__getitem__)
     3000    0.003    0.000    0.012    0.000 series.py:1075(_get_value)
5 từ mỗi hàng và gọi nhận từ cả chỉ mục và loạt (ba lần cho mỗi hàng). Các cuộc gọi chức năng rất tốn kém trong Python, vì vậy có lẽ chúng ta có thể giảm thiểu những điều này bằng cách cython hóa phần áp dụng.

Ghi chú

Bây giờ chúng tôi đang chuyển các ndarrays vào chức năng Cython, may mắn thay, Cython chơi rất độc đáo với Numpy.

ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
1

Việc triển khai rất đơn giản, nó tạo ra một mảng số không và các vòng lặp trên các hàng, áp dụng

In [6]: %prun -l 4 df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)  # noqa E999
         621314 function calls (621294 primitive calls) in 0.235 seconds

   Ordered by: internal time
   List reduced from 223 to 4 due to restriction <4>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1000    0.132    0.000    0.200    0.000 :1(integrate_f)
   552423    0.067    0.000    0.067    0.000 :1(f)
     3000    0.006    0.000    0.024    0.000 series.py:967(__getitem__)
     3000    0.003    0.000    0.012    0.000 series.py:1075(_get_value)
6 của chúng tôi và đặt nó vào mảng Zeros.

Cảnh báo

Bạn không thể truyền trực tiếp

In [6]: %prun -l 4 df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)  # noqa E999
         621314 function calls (621294 primitive calls) in 0.235 seconds

   Ordered by: internal time
   List reduced from 223 to 4 due to restriction <4>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1000    0.132    0.000    0.200    0.000 :1(integrate_f)
   552423    0.067    0.000    0.067    0.000 :1(f)
     3000    0.006    0.000    0.024    0.000 series.py:967(__getitem__)
     3000    0.003    0.000    0.012    0.000 series.py:1075(_get_value)
5 dưới dạng tham số gõ
In [6]: %prun -l 4 df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)  # noqa E999
         621314 function calls (621294 primitive calls) in 0.235 seconds

   Ordered by: internal time
   List reduced from 223 to 4 due to restriction <4>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1000    0.132    0.000    0.200    0.000 :1(integrate_f)
   552423    0.067    0.000    0.067    0.000 :1(f)
     3000    0.006    0.000    0.024    0.000 series.py:967(__getitem__)
     3000    0.003    0.000    0.012    0.000 series.py:1075(_get_value)
8 cho hàm cython. Thay vào đó, hãy vượt qua
In [6]: %prun -l 4 df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)  # noqa E999
         621314 function calls (621294 primitive calls) in 0.235 seconds

   Ordered by: internal time
   List reduced from 223 to 4 due to restriction <4>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1000    0.132    0.000    0.200    0.000 :1(integrate_f)
   552423    0.067    0.000    0.067    0.000 :1(f)
     3000    0.006    0.000    0.024    0.000 series.py:967(__getitem__)
     3000    0.003    0.000    0.012    0.000 series.py:1075(_get_value)
8 thực tế bằng cách sử dụng
In [8]: %%cython
   ...: def f_plain(x):
   ...:     return x * (x - 1)
   ...: def integrate_f_plain(a, b, N):
   ...:     s = 0
   ...:     dx = (b - a) / N
   ...:     for i in range(N):
   ...:         s += f_plain(a + i * dx)
   ...:     return s * dx
   ...: 
0. Lý do là định nghĩa Cython dành riêng cho ndarray chứ không phải thông qua
In [6]: %prun -l 4 df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)  # noqa E999
         621314 function calls (621294 primitive calls) in 0.235 seconds

   Ordered by: internal time
   List reduced from 223 to 4 due to restriction <4>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1000    0.132    0.000    0.200    0.000 :1(integrate_f)
   552423    0.067    0.000    0.067    0.000 :1(f)
     3000    0.006    0.000    0.024    0.000 series.py:967(__getitem__)
     3000    0.003    0.000    0.012    0.000 series.py:1075(_get_value)
5.not pass a
In [6]: %prun -l 4 df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)  # noqa E999
         621314 function calls (621294 primitive calls) in 0.235 seconds

   Ordered by: internal time
   List reduced from 223 to 4 due to restriction <4>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1000    0.132    0.000    0.200    0.000 :1(integrate_f)
   552423    0.067    0.000    0.067    0.000 :1(f)
     3000    0.006    0.000    0.024    0.000 series.py:967(__getitem__)
     3000    0.003    0.000    0.012    0.000 series.py:1075(_get_value)
5 directly as a
In [6]: %prun -l 4 df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)  # noqa E999
         621314 function calls (621294 primitive calls) in 0.235 seconds

   Ordered by: internal time
   List reduced from 223 to 4 due to restriction <4>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1000    0.132    0.000    0.200    0.000 :1(integrate_f)
   552423    0.067    0.000    0.067    0.000 :1(f)
     3000    0.006    0.000    0.024    0.000 series.py:967(__getitem__)
     3000    0.003    0.000    0.012    0.000 series.py:1075(_get_value)
8 typed parameter to a Cython function. Instead pass the actual
In [6]: %prun -l 4 df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)  # noqa E999
         621314 function calls (621294 primitive calls) in 0.235 seconds

   Ordered by: internal time
   List reduced from 223 to 4 due to restriction <4>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1000    0.132    0.000    0.200    0.000 :1(integrate_f)
   552423    0.067    0.000    0.067    0.000 :1(f)
     3000    0.006    0.000    0.024    0.000 series.py:967(__getitem__)
     3000    0.003    0.000    0.012    0.000 series.py:1075(_get_value)
8 using the
In [8]: %%cython
   ...: def f_plain(x):
   ...:     return x * (x - 1)
   ...: def integrate_f_plain(a, b, N):
   ...:     s = 0
   ...:     dx = (b - a) / N
   ...:     for i in range(N):
   ...:         s += f_plain(a + i * dx)
   ...:     return s * dx
   ...: 
0. The reason is that the Cython definition is specific to an ndarray and not the passed
In [6]: %prun -l 4 df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)  # noqa E999
         621314 function calls (621294 primitive calls) in 0.235 seconds

   Ordered by: internal time
   List reduced from 223 to 4 due to restriction <4>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1000    0.132    0.000    0.200    0.000 :1(integrate_f)
   552423    0.067    0.000    0.067    0.000 :1(f)
     3000    0.006    0.000    0.024    0.000 series.py:967(__getitem__)
     3000    0.003    0.000    0.012    0.000 series.py:1075(_get_value)
5.

Vì vậy, đừng làm điều này:

ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
2

Nhưng thay vào đó, sử dụng

In [8]: %%cython
   ...: def f_plain(x):
   ...:     return x * (x - 1)
   ...: def integrate_f_plain(a, b, N):
   ...:     s = 0
   ...:     dx = (b - a) / N
   ...:     for i in range(N):
   ...:         s += f_plain(a + i * dx)
   ...:     return s * dx
   ...: 
0 để có được
In [6]: %prun -l 4 df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)  # noqa E999
         621314 function calls (621294 primitive calls) in 0.235 seconds

   Ordered by: internal time
   List reduced from 223 to 4 due to restriction <4>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1000    0.132    0.000    0.200    0.000 :1(integrate_f)
   552423    0.067    0.000    0.067    0.000 :1(f)
     3000    0.006    0.000    0.024    0.000 series.py:967(__getitem__)
     3000    0.003    0.000    0.012    0.000 series.py:1075(_get_value)
8 cơ bản:

ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
3

Ghi chú

Các vòng như thế này sẽ cực kỳ chậm trong Python, nhưng trong Cython lặp lại trên các mảng numpy là nhanh.

ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
4

Chúng tôi đã nhận được một cải tiến lớn khác. Hãy để kiểm tra lại nơi dành thời gian dành cho thời gian:

ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
5

Như người ta có thể mong đợi, phần lớn thời gian hiện đã được sử dụng trong

In [8]: %%cython
   ...: def f_plain(x):
   ...:     return x * (x - 1)
   ...: def integrate_f_plain(a, b, N):
   ...:     s = 0
   ...:     dx = (b - a) / N
   ...:     for i in range(N):
   ...:         s += f_plain(a + i * dx)
   ...:     return s * dx
   ...: 
4, vì vậy nếu chúng ta muốn thực hiện hiệu quả nữa, chúng ta phải tiếp tục tập trung những nỗ lực của mình ở đây.

Kỹ thuật nâng cao hơn#

Vẫn còn hy vọng cải thiện. Ở đây, một ví dụ về việc sử dụng một số kỹ thuật Cython tiên tiến hơn:

ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
6

ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
7

Thậm chí nhanh hơn, với việc cảnh báo rằng một lỗi trong mã Cython của chúng tôi (ví dụ như một lỗi ngoài một) có thể gây ra một segfault vì truy cập bộ nhớ được kiểm tra. Để biết thêm về

In [8]: %%cython
   ...: def f_plain(x):
   ...:     return x * (x - 1)
   ...: def integrate_f_plain(a, b, N):
   ...:     s = 0
   ...:     dx = (b - a) / N
   ...:     for i in range(N):
   ...:         s += f_plain(a + i * dx)
   ...:     return s * dx
   ...: 
5 và
In [8]: %%cython
   ...: def f_plain(x):
   ...:     return x * (x - 1)
   ...: def integrate_f_plain(a, b, N):
   ...:     s = 0
   ...:     dx = (b - a) / N
   ...:     for i in range(N):
   ...:         s += f_plain(a + i * dx)
   ...:     return s * dx
   ...: 
6, hãy xem các tài liệu Cython trên các chỉ thị của trình biên dịch.

Numba (tổng hợp jit)#

Một giải pháp thay thế cho mã Cython biên dịch tĩnh là sử dụng trình biên dịch chỉ trong thời gian (JIT) động với Numba.

Numba cho phép bạn viết một hàm Python thuần túy có thể được biên dịch cho các hướng dẫn máy gốc, tương tự về hiệu suất với C, C ++ và Fortran, bằng cách trang trí chức năng của bạn bằng

In [8]: %%cython
   ...: def f_plain(x):
   ...:     return x * (x - 1)
   ...: def integrate_f_plain(a, b, N):
   ...:     s = 0
   ...:     dx = (b - a) / N
   ...:     for i in range(N):
   ...:         s += f_plain(a + i * dx)
   ...:     return s * dx
   ...: 
7.

Numba hoạt động bằng cách tạo mã máy được tối ưu hóa bằng cơ sở hạ tầng trình biên dịch LLVM tại thời gian nhập khẩu, thời gian chạy hoặc tĩnh (sử dụng công cụ PYCC đi kèm). Numba hỗ trợ biên dịch Python để chạy trên phần cứng CPU hoặc GPU và được thiết kế để tích hợp với ngăn xếp phần mềm khoa học Python.

Ghi chú

Các vòng như thế này sẽ cực kỳ chậm trong Python, nhưng trong Cython lặp lại trên các mảng numpy là nhanh.

Chúng tôi đã nhận được một cải tiến lớn khác. Hãy để kiểm tra lại nơi dành thời gian dành cho thời gian:

  1. Như người ta có thể mong đợi, phần lớn thời gian hiện đã được sử dụng trong

    In [8]: %%cython
       ...: def f_plain(x):
       ...:     return x * (x - 1)
       ...: def integrate_f_plain(a, b, N):
       ...:     s = 0
       ...:     dx = (b - a) / N
       ...:     for i in range(N):
       ...:         s += f_plain(a + i * dx)
       ...:     return s * dx
       ...: 
    
    4, vì vậy nếu chúng ta muốn thực hiện hiệu quả nữa, chúng ta phải tiếp tục tập trung những nỗ lực của mình ở đây.

  2. Kỹ thuật nâng cao hơn#

Vẫn còn hy vọng cải thiện. Ở đây, một ví dụ về việc sử dụng một số kỹ thuật Cython tiên tiến hơn:

Thậm chí nhanh hơn, với việc cảnh báo rằng một lỗi trong mã Cython của chúng tôi (ví dụ như một lỗi ngoài một) có thể gây ra một segfault vì truy cập bộ nhớ được kiểm tra. Để biết thêm về

In [8]: %%cython
   ...: def f_plain(x):
   ...:     return x * (x - 1)
   ...: def integrate_f_plain(a, b, N):
   ...:     s = 0
   ...:     dx = (b - a) / N
   ...:     for i in range(N):
   ...:         s += f_plain(a + i * dx)
   ...:     return s * dx
   ...: 
5 và
In [8]: %%cython
   ...: def f_plain(x):
   ...:     return x * (x - 1)
   ...: def integrate_f_plain(a, b, N):
   ...:     s = 0
   ...:     dx = (b - a) / N
   ...:     for i in range(N):
   ...:         s += f_plain(a + i * dx)
   ...:     return s * dx
   ...: 
6, hãy xem các tài liệu Cython trên các chỉ thị của trình biên dịch.

Numba (tổng hợp jit)#the first time a function is run using the Numba engine will be slow as Numba will have some function compilation overhead. However, the JIT compiled functions are cached, and subsequent calls will be fast. In general, the Numba engine is performant with a larger amount of data points (e.g. 1+ million).

ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
8

Một giải pháp thay thế cho mã Cython biên dịch tĩnh là sử dụng trình biên dịch chỉ trong thời gian (JIT) động với Numba.

ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
9

Numba cho phép bạn viết một hàm Python thuần túy có thể được biên dịch cho các hướng dẫn máy gốc, tương tự về hiệu suất với C, C ++ và Fortran, bằng cách trang trí chức năng của bạn bằng In [8]: %%cython ...: def f_plain(x): ...: return x * (x - 1) ...: def integrate_f_plain(a, b, N): ...: s = 0 ...: dx = (b - a) / N ...: for i in range(N): ...: s += f_plain(a + i * dx) ...: return s * dx ...: 7.

Numba hoạt động bằng cách tạo mã máy được tối ưu hóa bằng cơ sở hạ tầng trình biên dịch LLVM tại thời gian nhập khẩu, thời gian chạy hoặc tĩnh (sử dụng công cụ PYCC đi kèm). Numba hỗ trợ biên dịch Python để chạy trên phần cứng CPU hoặc GPU và được thiết kế để tích hợp với ngăn xếp phần mềm khoa học Python.

In [1]: df = pd.DataFrame(
   ...:     {
   ...:         "a": np.random.randn(1000),
   ...:         "b": np.random.randn(1000),
   ...:         "N": np.random.randint(100, 1000, (1000)),
   ...:         "x": "x",
   ...:     }
   ...: )
   ...: 

In [2]: df
Out[2]: 
            a         b    N  x
0    0.469112 -0.218470  585  x
1   -0.282863 -0.061645  841  x
2   -1.509059 -0.723780  251  x
3   -1.135632  0.551225  972  x
4    1.212112 -0.497767  181  x
..        ...       ...  ... ..
995 -1.512743  0.874737  374  x
996  0.933753  1.120790  246  x
997 -0.308013  0.198768  157  x
998 -0.079915  1.757555  977  x
999 -1.010589 -1.115680  770  x

[1000 rows x 4 columns]
0

In [1]: df = pd.DataFrame(
   ...:     {
   ...:         "a": np.random.randn(1000),
   ...:         "b": np.random.randn(1000),
   ...:         "N": np.random.randint(100, 1000, (1000)),
   ...:         "x": "x",
   ...:     }
   ...: )
   ...: 

In [2]: df
Out[2]: 
            a         b    N  x
0    0.469112 -0.218470  585  x
1   -0.282863 -0.061645  841  x
2   -1.509059 -0.723780  251  x
3   -1.135632  0.551225  972  x
4    1.212112 -0.497767  181  x
..        ...       ...  ... ..
995 -1.512743  0.874737  374  x
996  0.933753  1.120790  246  x
997 -0.308013  0.198768  157  x
998 -0.079915  1.757555  977  x
999 -1.010589 -1.115680  770  x

[1000 rows x 4 columns]
1

Việc biên dịch

In [8]: %%cython
   ...: def f_plain(x):
   ...:     return x * (x - 1)
   ...: def integrate_f_plain(a, b, N):
   ...:     s = 0
   ...:     dx = (b - a) / N
   ...:     for i in range(N):
   ...:         s += f_plain(a + i * dx)
   ...:     return s * dx
   ...: 
7 sẽ thêm chi phí vào thời gian chạy của hàm, do đó, lợi ích hiệu suất có thể không được thực hiện đặc biệt là khi sử dụng các bộ dữ liệu nhỏ. Xem xét bộ nhớ đệm chức năng của bạn để tránh biên dịch chi phí mỗi khi chức năng của bạn được chạy.

Numba có thể được sử dụng theo 2 cách với gấu trúc:

In [1]: df = pd.DataFrame(
   ...:     {
   ...:         "a": np.random.randn(1000),
   ...:         "b": np.random.randn(1000),
   ...:         "N": np.random.randint(100, 1000, (1000)),
   ...:         "x": "x",
   ...:     }
   ...: )
   ...: 

In [2]: df
Out[2]: 
            a         b    N  x
0    0.469112 -0.218470  585  x
1   -0.282863 -0.061645  841  x
2   -1.509059 -0.723780  251  x
3   -1.135632  0.551225  972  x
4    1.212112 -0.497767  181  x
..        ...       ...  ... ..
995 -1.512743  0.874737  374  x
996  0.933753  1.120790  246  x
997 -0.308013  0.198768  157  x
998 -0.079915  1.757555  977  x
999 -1.010589 -1.115680  770  x

[1000 rows x 4 columns]
2

In [1]: df = pd.DataFrame(
   ...:     {
   ...:         "a": np.random.randn(1000),
   ...:         "b": np.random.randn(1000),
   ...:         "N": np.random.randint(100, 1000, (1000)),
   ...:         "x": "x",
   ...:     }
   ...: )
   ...: 

In [2]: df
Out[2]: 
            a         b    N  x
0    0.469112 -0.218470  585  x
1   -0.282863 -0.061645  841  x
2   -1.509059 -0.723780  251  x
3   -1.135632  0.551225  972  x
4    1.212112 -0.497767  181  x
..        ...       ...  ... ..
995 -1.512743  0.874737  374  x
996  0.933753  1.120790  246  x
997 -0.308013  0.198768  157  x
998 -0.079915  1.757555  977  x
999 -1.010589 -1.115680  770  x

[1000 rows x 4 columns]
3

Caveats#

Chỉ định từ khóa

In [8]: %%cython
   ...: def f_plain(x):
   ...:     return x * (x - 1)
   ...: def integrate_f_plain(a, b, N):
   ...:     s = 0
   ...:     dx = (b - a) / N
   ...:     for i in range(N):
   ...:         s += f_plain(a + i * dx)
   ...:     return s * dx
   ...: 
9 trong các phương thức Pandas chọn

Xác định chức năng Python của riêng bạn được trang trí bằng

In [8]: %%cython
   ...: def f_plain(x):
   ...:     return x * (x - 1)
   ...: def integrate_f_plain(a, b, N):
   ...:     s = 0
   ...:     dx = (b - a) / N
   ...:     for i in range(N):
   ...:         s += f_plain(a + i * dx)
   ...:     return s * dx
   ...: 
7 và chuyển mảng numpy cơ bản của
In [6]: %prun -l 4 df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)  # noqa E999
         621314 function calls (621294 primitive calls) in 0.235 seconds

   Ordered by: internal time
   List reduced from 223 to 4 due to restriction <4>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1000    0.132    0.000    0.200    0.000 :1(integrate_f)
   552423    0.067    0.000    0.067    0.000 :1(f)
     3000    0.006    0.000    0.024    0.000 series.py:967(__getitem__)
     3000    0.003    0.000    0.012    0.000 series.py:1075(_get_value)
5 hoặc
In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
7 (sử dụng
In [9]: %timeit df.apply(lambda x: integrate_f_plain(x["a"], x["b"], x["N"]), axis=1)
61.5 ms +- 401 us per loop (mean +- std. dev. of 7 runs, 10 loops each)
3) vào chức năng

Động cơ Pandas Numba#

Đánh giá biểu thức qua ________ 97#

Hàm cấp cao nhất

In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
8 thực hiện đánh giá biểu thức của các đối tượng
In [6]: %prun -l 4 df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)  # noqa E999
         621314 function calls (621294 primitive calls) in 0.235 seconds

   Ordered by: internal time
   List reduced from 223 to 4 due to restriction <4>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1000    0.132    0.000    0.200    0.000 :1(integrate_f)
   552423    0.067    0.000    0.067    0.000 :1(f)
     3000    0.006    0.000    0.024    0.000 series.py:967(__getitem__)
     3000    0.003    0.000    0.012    0.000 series.py:1075(_get_value)
5 và
In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
7.

Điểm sử dụng

In [11]: %timeit df.apply(lambda x: integrate_f_typed(x["a"], x["b"], x["N"]), axis=1)
12.9 ms +- 223 us per loop (mean +- std. dev. of 7 runs, 100 loops each)
7 để đánh giá biểu thức thay vì trăn đơn giản là hai lần: 1) các đối tượng
In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
7 lớn được đánh giá hiệu quả hơn và 2) biểu thức số học và boolean lớn được đánh giá cùng một lúc bởi động cơ cơ bản (theo mặc định
ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
03 được sử dụng cho sự đánh giá).

Ghi chú

Bạn không nên sử dụng

In [11]: %timeit df.apply(lambda x: integrate_f_typed(x["a"], x["b"], x["N"]), axis=1)
12.9 ms +- 223 us per loop (mean +- std. dev. of 7 runs, 100 loops each)
7 cho các biểu thức đơn giản hoặc cho các biểu thức liên quan đến các khung dữ liệu nhỏ. Trên thực tế,
In [11]: %timeit df.apply(lambda x: integrate_f_typed(x["a"], x["b"], x["N"]), axis=1)
12.9 ms +- 223 us per loop (mean +- std. dev. of 7 runs, 100 loops each)
7 có nhiều thứ tự chậm hơn đối với các biểu thức/đối tượng nhỏ hơn so với python ol đơn giản. Một quy tắc tốt là chỉ sử dụng
In [11]: %timeit df.apply(lambda x: integrate_f_typed(x["a"], x["b"], x["N"]), axis=1)
12.9 ms +- 223 us per loop (mean +- std. dev. of 7 runs, 100 loops each)
7 khi bạn có
In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
7 với hơn 10.000 hàng.

In [11]: %timeit df.apply(lambda x: integrate_f_typed(x["a"], x["b"], x["N"]), axis=1)
12.9 ms +- 223 us per loop (mean +- std. dev. of 7 runs, 100 loops each)
7 hỗ trợ tất cả các biểu thức số học được hỗ trợ bởi động cơ ngoài một số phần mở rộng chỉ có sẵn trong gấu trúc.

Ghi chú

Bạn không nên sử dụng

In [11]: %timeit df.apply(lambda x: integrate_f_typed(x["a"], x["b"], x["N"]), axis=1)
12.9 ms +- 223 us per loop (mean +- std. dev. of 7 runs, 100 loops each)
7 cho các biểu thức đơn giản hoặc cho các biểu thức liên quan đến các khung dữ liệu nhỏ. Trên thực tế,
In [11]: %timeit df.apply(lambda x: integrate_f_typed(x["a"], x["b"], x["N"]), axis=1)
12.9 ms +- 223 us per loop (mean +- std. dev. of 7 runs, 100 loops each)
7 có nhiều thứ tự chậm hơn đối với các biểu thức/đối tượng nhỏ hơn so với python ol đơn giản. Một quy tắc tốt là chỉ sử dụng
In [11]: %timeit df.apply(lambda x: integrate_f_typed(x["a"], x["b"], x["N"]), axis=1)
12.9 ms +- 223 us per loop (mean +- std. dev. of 7 runs, 100 loops each)
7 khi bạn có
In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
7 với hơn 10.000 hàng.

In [11]: %timeit df.apply(lambda x: integrate_f_typed(x["a"], x["b"], x["N"]), axis=1) 12.9 ms +- 223 us per loop (mean +- std. dev. of 7 runs, 100 loops each) 7 hỗ trợ tất cả các biểu thức số học được hỗ trợ bởi động cơ ngoài một số phần mở rộng chỉ có sẵn trong gấu trúc.

Khung càng lớn và biểu thức càng lớn thì bạn càng thấy càng tăng tốc từ việc sử dụng

In [11]: %timeit df.apply(lambda x: integrate_f_typed(x["a"], x["b"], x["N"]), axis=1)
12.9 ms +- 223 us per loop (mean +- std. dev. of 7 runs, 100 loops each)
7.

  • Cú pháp được hỗ trợ#

  • Các hoạt động này được hỗ trợ bởi

    In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
    122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
    
    8:

  • Các hoạt động số học ngoại trừ các toán tử thay đổi bên trái (

    ID       Total_Speed          Total_schritte    Avg_Speed
    1817603  2199.422386          149               14.761224
    1817615  inf                  1178              inf
    1817679  inf                  452               inf
    1817888  5436.540730          271               20.061036
    
    11) và dịch chuyển bên phải (
    ID       Total_Speed          Total_schritte    Avg_Speed
    1817603  2199.422386          149               14.761224
    1817615  inf                  1178              inf
    1817679  inf                  452               inf
    1817888  5436.540730          271               20.061036
    
    12), ví dụ:
    ID       Total_Speed          Total_schritte    Avg_Speed
    1817603  2199.422386          149               14.761224
    1817615  inf                  1178              inf
    1817679  inf                  452               inf
    1817888  5436.540730          271               20.061036
    
    13

  • Các hoạt động so sánh, bao gồm so sánh chuỗi, ví dụ:

    ID       Total_Speed          Total_schritte    Avg_Speed
    1817603  2199.422386          149               14.761224
    1817615  inf                  1178              inf
    1817679  inf                  452               inf
    1817888  5436.540730          271               20.061036
    
    14

  • Hoạt động Boolean, ví dụ:

    ID       Total_Speed          Total_schritte    Avg_Speed
    1817603  2199.422386          149               14.761224
    1817615  inf                  1178              inf
    1817679  inf                  452               inf
    1817888  5436.540730          271               20.061036
    
    15

  • ID       Total_Speed          Total_schritte    Avg_Speed
    1817603  2199.422386          149               14.761224
    1817615  inf                  1178              inf
    1817679  inf                  452               inf
    1817888  5436.540730          271               20.061036
    
    16 và
    ID       Total_Speed          Total_schritte    Avg_Speed
    1817603  2199.422386          149               14.761224
    1817615  inf                  1178              inf
    1817679  inf                  452               inf
    1817888  5436.540730          271               20.061036
    
    17 Biết chữ, ví dụ:
    ID       Total_Speed          Total_schritte    Avg_Speed
    1817603  2199.422386          149               14.761224
    1817615  inf                  1178              inf
    1817679  inf                  452               inf
    1817888  5436.540730          271               20.061036
    
    18 hoặc
    ID       Total_Speed          Total_schritte    Avg_Speed
    1817603  2199.422386          149               14.761224
    1817615  inf                  1178              inf
    1817679  inf                  452               inf
    1817888  5436.540730          271               20.061036
    
    19

  • Truy cập thuộc tính, ví dụ:

    ID       Total_Speed          Total_schritte    Avg_Speed
    1817603  2199.422386          149               14.761224
    1817615  inf                  1178              inf
    1817679  inf                  452               inf
    1817888  5436.540730          271               20.061036
    
    20

  • Biểu thức đăng ký, ví dụ:

    ID       Total_Speed          Total_schritte    Avg_Speed
    1817603  2199.422386          149               14.761224
    1817615  inf                  1178              inf
    1817679  inf                  452               inf
    1817888  5436.540730          271               20.061036
    
    21

Đánh giá biến đơn giản, ví dụ:

ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
22 (điều này không hữu ích lắm)not allowed:

  • Các hàm toán học:

    ID       Total_Speed          Total_schritte    Avg_Speed
    1817603  2199.422386          149               14.761224
    1817615  inf                  1178              inf
    1817679  inf                  452               inf
    1817888  5436.540730          271               20.061036
    
    23,
    ID       Total_Speed          Total_schritte    Avg_Speed
    1817603  2199.422386          149               14.761224
    1817615  inf                  1178              inf
    1817679  inf                  452               inf
    1817888  5436.540730          271               20.061036
    
    24,
    ID       Total_Speed          Total_schritte    Avg_Speed
    1817603  2199.422386          149               14.761224
    1817615  inf                  1178              inf
    1817679  inf                  452               inf
    1817888  5436.540730          271               20.061036
    
    25,
    ID       Total_Speed          Total_schritte    Avg_Speed
    1817603  2199.422386          149               14.761224
    1817615  inf                  1178              inf
    1817679  inf                  452               inf
    1817888  5436.540730          271               20.061036
    
    26,
    ID       Total_Speed          Total_schritte    Avg_Speed
    1817603  2199.422386          149               14.761224
    1817615  inf                  1178              inf
    1817679  inf                  452               inf
    1817888  5436.540730          271               20.061036
    
    27,
    ID       Total_Speed          Total_schritte    Avg_Speed
    1817603  2199.422386          149               14.761224
    1817615  inf                  1178              inf
    1817679  inf                  452               inf
    1817888  5436.540730          271               20.061036
    
    28,
    ID       Total_Speed          Total_schritte    Avg_Speed
    1817603  2199.422386          149               14.761224
    1817615  inf                  1178              inf
    1817679  inf                  452               inf
    1817888  5436.540730          271               20.061036
    
    29,
    ID       Total_Speed          Total_schritte    Avg_Speed
    1817603  2199.422386          149               14.761224
    1817615  inf                  1178              inf
    1817679  inf                  452               inf
    1817888  5436.540730          271               20.061036
    
    30,
    ID       Total_Speed          Total_schritte    Avg_Speed
    1817603  2199.422386          149               14.761224
    1817615  inf                  1178              inf
    1817679  inf                  452               inf
    1817888  5436.540730          271               20.061036
    
    31,
    ID       Total_Speed          Total_schritte    Avg_Speed
    1817603  2199.422386          149               14.761224
    1817615  inf                  1178              inf
    1817679  inf                  452               inf
    1817888  5436.540730          271               20.061036
    
    32, ____.

    • Cú pháp Python này không được phép:

    • Biểu thức

    • Chức năng gọi khác với các hàm toán học.

    • ________ 142/________ 143 Hoạt động

    • ID       Total_Speed          Total_schritte    Avg_Speed
      1817603  2199.422386          149               14.761224
      1817615  inf                  1178              inf
      1817679  inf                  452               inf
      1817888  5436.540730          271               20.061036
      
      44 Biểu thức

    • ID       Total_Speed          Total_schritte    Avg_Speed
      1817603  2199.422386          149               14.761224
      1817615  inf                  1178              inf
      1817679  inf                  452               inf
      1817888  5436.540730          271               20.061036
      
      45 Biểu thức

    • ________ 116/________ 147/________ 148 Toàn cầu

    • Theo nghĩa đen

      ID       Total_Speed          Total_schritte    Avg_Speed
      1817603  2199.422386          149               14.761224
      1817615  inf                  1178              inf
      1817679  inf                  452               inf
      1817888  5436.540730          271               20.061036
      
      48 và
      ID       Total_Speed          Total_schritte    Avg_Speed
      1817603  2199.422386          149               14.761224
      1817615  inf                  1178              inf
      1817679  inf                  452               inf
      1817888  5436.540730          271               20.061036
      
      47

    • ID       Total_Speed          Total_schritte    Avg_Speed
      1817603  2199.422386          149               14.761224
      1817615  inf                  1178              inf
      1817679  inf                  452               inf
      1817888  5436.540730          271               20.061036
      
      51 Biểu thức

  • Biểu thức máy phát

    • Biểu thức Boolean chỉ bao gồm các giá trị vô hướng

Các câu lệnh

Không cho phép các tuyên bố đơn giản cũng không được cho phép. Điều này bao gồm những thứ như

ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
52,
ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
53 và
ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
44.

In [11]: %timeit df.apply(lambda x: integrate_f_typed(x["a"], x["b"], x["N"]), axis=1)
12.9 ms +- 223 us per loop (mean +- std. dev. of 7 runs, 100 loops each)
7 Ví dụ#

In [1]: df = pd.DataFrame(
   ...:     {
   ...:         "a": np.random.randn(1000),
   ...:         "b": np.random.randn(1000),
   ...:         "N": np.random.randint(100, 1000, (1000)),
   ...:         "x": "x",
   ...:     }
   ...: )
   ...: 

In [2]: df
Out[2]: 
            a         b    N  x
0    0.469112 -0.218470  585  x
1   -0.282863 -0.061645  841  x
2   -1.509059 -0.723780  251  x
3   -1.135632  0.551225  972  x
4    1.212112 -0.497767  181  x
..        ...       ...  ... ..
995 -1.512743  0.874737  374  x
996  0.933753  1.120790  246  x
997 -0.308013  0.198768  157  x
998 -0.079915  1.757555  977  x
999 -1.010589 -1.115680  770  x

[1000 rows x 4 columns]
4

In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
8 hoạt động tốt với các biểu thức chứa các mảng lớn.

In [1]: df = pd.DataFrame(
   ...:     {
   ...:         "a": np.random.randn(1000),
   ...:         "b": np.random.randn(1000),
   ...:         "N": np.random.randint(100, 1000, (1000)),
   ...:         "x": "x",
   ...:     }
   ...: )
   ...: 

In [2]: df
Out[2]: 
            a         b    N  x
0    0.469112 -0.218470  585  x
1   -0.282863 -0.061645  841  x
2   -1.509059 -0.723780  251  x
3   -1.135632  0.551225  972  x
4    1.212112 -0.497767  181  x
..        ...       ...  ... ..
995 -1.512743  0.874737  374  x
996  0.933753  1.120790  246  x
997 -0.308013  0.198768  157  x
998 -0.079915  1.757555  977  x
999 -1.010589 -1.115680  770  x

[1000 rows x 4 columns]
5

In [1]: df = pd.DataFrame(
   ...:     {
   ...:         "a": np.random.randn(1000),
   ...:         "b": np.random.randn(1000),
   ...:         "N": np.random.randint(100, 1000, (1000)),
   ...:         "x": "x",
   ...:     }
   ...: )
   ...: 

In [2]: df
Out[2]: 
            a         b    N  x
0    0.469112 -0.218470  585  x
1   -0.282863 -0.061645  841  x
2   -1.509059 -0.723780  251  x
3   -1.135632  0.551225  972  x
4    1.212112 -0.497767  181  x
..        ...       ...  ... ..
995 -1.512743  0.874737  374  x
996  0.933753  1.120790  246  x
997 -0.308013  0.198768  157  x
998 -0.079915  1.757555  977  x
999 -1.010589 -1.115680  770  x

[1000 rows x 4 columns]
6

Đầu tiên, hãy để tạo ra một vài mảng có kích thước tốt để chơi với:

In [1]: df = pd.DataFrame(
   ...:     {
   ...:         "a": np.random.randn(1000),
   ...:         "b": np.random.randn(1000),
   ...:         "N": np.random.randint(100, 1000, (1000)),
   ...:         "x": "x",
   ...:     }
   ...: )
   ...: 

In [2]: df
Out[2]: 
            a         b    N  x
0    0.469112 -0.218470  585  x
1   -0.282863 -0.061645  841  x
2   -1.509059 -0.723780  251  x
3   -1.135632  0.551225  972  x
4    1.212112 -0.497767  181  x
..        ...       ...  ... ..
995 -1.512743  0.874737  374  x
996  0.933753  1.120790  246  x
997 -0.308013  0.198768  157  x
998 -0.079915  1.757555  977  x
999 -1.010589 -1.115680  770  x

[1000 rows x 4 columns]
7

In [1]: df = pd.DataFrame(
   ...:     {
   ...:         "a": np.random.randn(1000),
   ...:         "b": np.random.randn(1000),
   ...:         "N": np.random.randint(100, 1000, (1000)),
   ...:         "x": "x",
   ...:     }
   ...: )
   ...: 

In [2]: df
Out[2]: 
            a         b    N  x
0    0.469112 -0.218470  585  x
1   -0.282863 -0.061645  841  x
2   -1.509059 -0.723780  251  x
3   -1.135632  0.551225  972  x
4    1.212112 -0.497767  181  x
..        ...       ...  ... ..
995 -1.512743  0.874737  374  x
996  0.933753  1.120790  246  x
997 -0.308013  0.198768  157  x
998 -0.079915  1.757555  977  x
999 -1.010589 -1.115680  770  x

[1000 rows x 4 columns]
8

Bây giờ, hãy để so sánh việc thêm chúng lại với nhau bằng cách sử dụng python ol đơn giản so với

In [11]: %timeit df.apply(lambda x: integrate_f_typed(x["a"], x["b"], x["N"]), axis=1)
12.9 ms +- 223 us per loop (mean +- std. dev. of 7 runs, 100 loops each)
7:

In [1]: df = pd.DataFrame(
   ...:     {
   ...:         "a": np.random.randn(1000),
   ...:         "b": np.random.randn(1000),
   ...:         "N": np.random.randint(100, 1000, (1000)),
   ...:         "x": "x",
   ...:     }
   ...: )
   ...: 

In [2]: df
Out[2]: 
            a         b    N  x
0    0.469112 -0.218470  585  x
1   -0.282863 -0.061645  841  x
2   -1.509059 -0.723780  251  x
3   -1.135632  0.551225  972  x
4    1.212112 -0.497767  181  x
..        ...       ...  ... ..
995 -1.512743  0.874737  374  x
996  0.933753  1.120790  246  x
997 -0.308013  0.198768  157  x
998 -0.079915  1.757555  977  x
999 -1.010589 -1.115680  770  x

[1000 rows x 4 columns]
9

In [3]: def f(x):
   ...:     return x * (x - 1)
   ...: 

In [4]: def integrate_f(a, b, N):
   ...:     s = 0
   ...:     dx = (b - a) / N
   ...:     for i in range(N):
   ...:         s += f(a + i * dx)
   ...:     return s * dx
   ...: 
0

Ghi chú

Bây giờ, hãy để Lừa làm điều tương tự nhưng với sự so sánh:

In [3]: def f(x):
   ...:     return x * (x - 1)
   ...: 

In [4]: def integrate_f(a, b, N):
   ...:     s = 0
   ...:     dx = (b - a) / N
   ...:     for i in range(N):
   ...:         s += f(a + i * dx)
   ...:     return s * dx
   ...: 
1

In [11]: %timeit df.apply(lambda x: integrate_f_typed(x["a"], x["b"], x["N"]), axis=1)
12.9 ms +- 223 us per loop (mean +- std. dev. of 7 runs, 100 loops each)
7 cũng hoạt động với các đối tượng gấu trúc không phân bổ:

Các hoạt động như

nên được thực hiện trong Python. Một ngoại lệ sẽ được nêu ra nếu bạn cố gắng thực hiện bất kỳ hoạt động boolean/bitwise nào với các toán tử vô hướng không thuộc loại

ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
59 hoặc
ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
60. Một lần nữa, bạn nên thực hiện các loại hoạt động này trong Python đơn giản.

In [3]: def f(x):
   ...:     return x * (x - 1)
   ...: 

In [4]: def integrate_f(a, b, N):
   ...:     s = 0
   ...:     dx = (b - a) / N
   ...:     for i in range(N):
   ...:         s += f(a + i * dx)
   ...:     return s * dx
   ...: 
2

Phương thức

ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
61#

Ngoài chức năng cấp cao nhất

In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
8, bạn cũng có thể đánh giá một biểu thức trong bối cảnh trên mạng của một
In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
7.

Bất kỳ biểu thức nào là biểu thức

In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
8 hợp lệ cũng là một biểu thức
ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
61 hợp lệ, với lợi ích bổ sung mà bạn không phải là tiền tố tên của
In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
7 cho (các) bạn quan tâm đến việc đánh giá.

In [3]: def f(x):
   ...:     return x * (x - 1)
   ...: 

In [4]: def integrate_f(a, b, N):
   ...:     s = 0
   ...:     dx = (b - a) / N
   ...:     for i in range(N):
   ...:         s += f(a + i * dx)
   ...:     return s * dx
   ...: 
3

Ngoài ra, bạn có thể thực hiện gán các cột trong một biểu thức. Điều này cho phép đánh giá công thức. Mục tiêu gán có thể là tên cột mới hoặc tên cột hiện có và nó phải là mã định danh Python hợp lệ.

In [3]: def f(x):
   ...:     return x * (x - 1)
   ...: 

In [4]: def integrate_f(a, b, N):
   ...:     s = 0
   ...:     dx = (b - a) / N
   ...:     for i in range(N):
   ...:         s += f(a + i * dx)
   ...:     return s * dx
   ...: 
4

Từ khóa

ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
67 xác định liệu nhiệm vụ này sẽ được thực hiện trên
In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
7 gốc hay trả về một bản sao với cột mới.

In [3]: def f(x):
   ...:     return x * (x - 1)
   ...: 

In [4]: def integrate_f(a, b, N):
   ...:     s = 0
   ...:     dx = (b - a) / N
   ...:     for i in range(N):
   ...:         s += f(a + i * dx)
   ...:     return s * dx
   ...: 
5

Khi

ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
67 được đặt thành
ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
70, mặc định, một bản sao của
In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
7 với các cột mới hoặc sửa đổi được trả về và khung ban đầu không thay đổi.

Để thuận tiện, nhiều bài tập có thể được thực hiện bằng cách sử dụng chuỗi nhiều dòng.

Tương đương trong Python tiêu chuẩn sẽ là

In [3]: def f(x):
   ...:     return x * (x - 1)
   ...: 

In [4]: def integrate_f(a, b, N):
   ...:     s = 0
   ...:     dx = (b - a) / N
   ...:     for i in range(N):
   ...:         s += f(a + i * dx)
   ...:     return s * dx
   ...: 
7

In [3]: def f(x): ...: return x * (x - 1) ...: In [4]: def integrate_f(a, b, N): ...: s = 0 ...: dx = (b - a) / N ...: for i in range(N): ...: s += f(a + i * dx) ...: return s * dx ...: 6

Phương thức

ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
72 có từ khóa
ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
67 xác định xem truy vấn có sửa đổi khung gốc hay không.

In [3]: def f(x):
   ...:     return x * (x - 1)
   ...: 

In [4]: def integrate_f(a, b, N):
   ...:     s = 0
   ...:     dx = (b - a) / N
   ...:     for i in range(N):
   ...:         s += f(a + i * dx)
   ...:     return s * dx
   ...: 
8

Các biến cục bộ#

Bạn phải tham khảo rõ ràng bất kỳ biến cục bộ nào mà bạn muốn sử dụng trong một biểu thức bằng cách đặt ký tự

ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
74 trước tên. Ví dụ,

In [3]: def f(x):
   ...:     return x * (x - 1)
   ...: 

In [4]: def integrate_f(a, b, N):
   ...:     s = 0
   ...:     dx = (b - a) / N
   ...:     for i in range(N):
   ...:         s += f(a + i * dx)
   ...:     return s * dx
   ...: 
9

Với

In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
8, bạn không thể sử dụng tiền tố
ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
74, bởi vì nó không được xác định trong bối cảnh đó. Pandas sẽ cho bạn biết điều này nếu bạn cố gắng sử dụng
ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
74 trong một cuộc gọi cấp cao nhất đến
In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
8. Ví dụ,

In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
0

Trong trường hợp này, bạn chỉ nên đề cập đến các biến như bạn trong Python tiêu chuẩn.

In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
1

In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1) 122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each) 8 Sư phạm#

Có hai trình phân tích cú pháp khác nhau và hai động cơ khác nhau mà bạn có thể sử dụng làm phụ trợ.

Trình phân tích cú pháp

ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
84 mặc định cho phép cú pháp trực quan hơn để thể hiện các hoạt động giống như truy vấn (so sánh, liên kết và phân tách). Cụ thể, sự ưu tiên của các toán tử
ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
85 và
ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
86 được thực hiện bằng với mức độ ưu tiên của các hoạt động Boolean tương ứng
ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
87 và
ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
88.

Ví dụ, kết hợp ở trên có thể được viết mà không cần dấu ngoặc đơn. Ngoài ra, bạn có thể sử dụng trình phân tích cú pháp

ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
89 để thực thi ngữ nghĩa Python nghiêm ngặt.

In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
2

Biểu thức tương tự cũng có thể là Anded Anded cùng với từ

ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
87:

In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
3

Các nhà khai thác

ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
87 và
ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
88 ở đây có mức độ ưu tiên tương tự mà họ sẽ làm trong Vanilla Python.

In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1) 122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each) 8 phụ trợ#

Ngoài ra, còn có một tùy chọn để làm cho

In [11]: %timeit df.apply(lambda x: integrate_f_typed(x["a"], x["b"], x["N"]), axis=1)
12.9 ms +- 223 us per loop (mean +- std. dev. of 7 runs, 100 loops each)
7 hoạt động giống hệt với python ol đơn giản.

Ghi chú

Sử dụng động cơ

ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
89 thường không hữu ích, ngoại trừ việc thử nghiệm các công cụ đánh giá khác chống lại nó. Bạn sẽ không đạt được lợi ích hiệu suất bằng cách sử dụng
In [11]: %timeit df.apply(lambda x: integrate_f_typed(x["a"], x["b"], x["N"]), axis=1)
12.9 ms +- 223 us per loop (mean +- std. dev. of 7 runs, 100 loops each)
7 với
ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
97 và trên thực tế có thể gây ra hiệu suất.no performance benefits using
In [11]: %timeit df.apply(lambda x: integrate_f_typed(x["a"], x["b"], x["N"]), axis=1)
12.9 ms +- 223 us per loop (mean +- std. dev. of 7 runs, 100 loops each)
7 with
ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
97 and in fact may incur a performance hit.

Bạn có thể thấy điều này bằng cách sử dụng

In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
8 với động cơ
ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
89. Nó chậm hơn một chút (không nhiều) so với đánh giá cùng một biểu thức trong Python

In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
4

In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
5

In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1) 122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each) 8 Hiệu suất#

In [11]: %timeit df.apply(lambda x: integrate_f_typed(x["a"], x["b"], x["N"]), axis=1)
12.9 ms +- 223 us per loop (mean +- std. dev. of 7 runs, 100 loops each)
7 nhằm tăng tốc độ hoạt động nhất định. Cụ thể, các hoạt động đó liên quan đến các biểu thức phức tạp với các đối tượng lớn ____ 47/________ 55 sẽ thấy một lợi ích hiệu suất đáng kể. Dưới đây là một âm mưu hiển thị thời gian chạy của
In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
8 là chức năng của kích thước của khung liên quan đến tính toán. Hai dòng là hai động cơ khác nhau.

Hướng dẫn python average speed code - mã tốc độ trung bình python

Ghi chú

Sử dụng động cơ

ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
89 thường không hữu ích, ngoại trừ việc thử nghiệm các công cụ đánh giá khác chống lại nó. Bạn sẽ không đạt được lợi ích hiệu suất bằng cách sử dụng
In [11]: %timeit df.apply(lambda x: integrate_f_typed(x["a"], x["b"], x["N"]), axis=1)
12.9 ms +- 223 us per loop (mean +- std. dev. of 7 runs, 100 loops each)
7 với
ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
97 và trên thực tế có thể gây ra hiệu suất.

Hướng dẫn python average speed code - mã tốc độ trung bình python

Bạn có thể thấy điều này bằng cách sử dụng

In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
8 với động cơ
ID       Total_Speed          Total_schritte    Avg_Speed
1817603  2199.422386          149               14.761224
1817615  inf                  1178              inf
1817679  inf                  452               inf
1817888  5436.540730          271               20.061036
89. Nó chậm hơn một chút (không nhiều) so với đánh giá cùng một biểu thức trong Python

In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1) 122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each) 8 Hiệu suất#

In [11]: %timeit df.apply(lambda x: integrate_f_typed(x["a"], x["b"], x["N"]), axis=1)
12.9 ms +- 223 us per loop (mean +- std. dev. of 7 runs, 100 loops each)
7 nhằm tăng tốc độ hoạt động nhất định. Cụ thể, các hoạt động đó liên quan đến các biểu thức phức tạp với các đối tượng lớn ____ 47/________ 55 sẽ thấy một lợi ích hiệu suất đáng kể. Dưới đây là một âm mưu hiển thị thời gian chạy của
In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
8 là chức năng của kích thước của khung liên quan đến tính toán. Hai dòng là hai động cơ khác nhau.

Các hoạt động với các đối tượng nhỏ (khoảng 15K-20K hàng) nhanh hơn bằng cách sử dụng Python đơn giản:

In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
6

Biểu đồ này được tạo bằng cách sử dụng

In [5]: %timeit df.apply(lambda x: integrate_f(x["a"], x["b"], x["N"]), axis=1)
122 ms +- 2.09 ms per loop (mean +- std. dev. of 7 runs, 10 loops each)
7 với 3 cột mỗi cột chứa các giá trị điểm nổi được tạo bằng
In [1]: df = pd.DataFrame(
   ...:     {
   ...:         "a": np.random.randn(1000),
   ...:         "b": np.random.randn(1000),
   ...:         "N": np.random.randint(100, 1000, (1000)),
   ...:         "x": "x",
   ...:     }
   ...: )
   ...: 

In [2]: df
Out[2]: 
            a         b    N  x
0    0.469112 -0.218470  585  x
1   -0.282863 -0.061645  841  x
2   -1.509059 -0.723780  251  x
3   -1.135632  0.551225  972  x
4    1.212112 -0.497767  181  x
..        ...       ...  ... ..
995 -1.512743  0.874737  374  x
996  0.933753  1.120790  246  x
997 -0.308013  0.198768  157  x
998 -0.079915  1.757555  977  x
999 -1.010589 -1.115680  770  x

[1000 rows x 4 columns]
06.

Minutia kỹ thuật liên quan đến đánh giá biểu hiện#