Hướng dẫn dùng np.expm1 python

numpy.expm1(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])= 'expm1'>#

Calculate exp(x) - 1 for all elements in the array.

Parametersxarray_like

Input values.

outndarray, None, or tuple of ndarray and None, optional

A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.

wherearray_like, optional

This condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None, locations within it where the condition is False will remain uninitialized.

**kwargs

For other keyword-only arguments, see the ufunc docs.

Returnsoutndarray or scalar

Element-wise exponential minus one: out = exp(x) - 1. This is a scalar if x is a scalar.

See also

log1p

log(1 + x), the inverse of expm1.

Notes

This function provides greater precision than exp(x) - 1 for small values of x.

Examples

The true value of exp(1e-10) - 1 is 1.00000000005e-10 to about 32 significant digits. This example shows the superiority of expm1 in this case.

>>> np.expm1(1e-10)
1.00000000005e-10
>>> np.exp(1e-10) - 1
1.000000082740371e-10

Mục đích của numpy.log1p () là gì?

Xin chào, tôi mới làm quen với kaggle và đang làm việc trên tập dữ liệu Dự đoán nhà . Tôi vừa xem qua một trong những Kernel này và không thể hiểu nó numpy.log1p()làm gì trong đường dẫn thứ ba của mã này

Tôi đã googled nó và tài liệu của numpy cho biết nó

Lợi nhuận :

  • Một mảng có giá trị lôgarit tự nhiên là x + 1;
  • trong đó x thuộc về tất cả các phần tử của mảng đầu vào.

Nhưng mục đích của việc tìm nhật ký có thêm một (+1) trong khi tìm độ lệch của mảng ban đầu và mảng đã biến đổi của các đối tượng địa lý giống nhau là gì? Nó làm gì ?

Ai đó có thể hướng dẫn tôi không?

  • python
  • numpy

25 hữu ích 4 bình luận 26k xem chia sẻ

answer

53

Tài liệu NumPy đưa ra gợi ý :

Đối với đầu vào có giá trị thực, log1pcũng chính xác đối với độ chính xác xnhỏ đến 1 + x == 1mức độ chính xác của dấu chấm động.

Vì vậy, ví dụ, chúng ta hãy thêm một số nhỏ khác 0 và 1.0. Làm tròn lỗi làm cho nó a 1.0.

>>> 1e-100 == 0.0
False
>>> 1e-100 + 1.0 == 1.0
True

Nếu chúng tôi cố gắng lấy logtổng không chính xác đó, chúng tôi sẽ nhận được kết quả không chính xác (so sánh với WolframAlpha ):

>>> np.log(1e-100 + 1)
0.0

Nhưng nếu chúng ta sử dụng log1p(), chúng ta sẽ nhận được kết quả chính xác

>>> np.log1p(1e-100)
1e-100

Nguyên tắc tương tự áp dụng cho exp1m()logaddexp(): Càng nhỏ càng chính xác x.

53 hữu ích 2 bình luận chia sẻ

answer

9

Nếu x nằm trong khoảng 0 ... + Inf thì nó sẽ không bao giờ gây ra lỗi (như chúng ta biết log (0) sẽ gây ra lỗi).

Không phải lúc nào cũng là lựa chọn tốt nhất, vì như bạn thấy, bạn sẽ mất một đường cong lớn trước x = 0, đó là một trong những điều tốt nhất về hàm log

9 hữu ích 1 bình luận chia sẻ

answer

0

Khi giá trị đầu vào của bạn quá nhỏ, bằng cách sử dụng np.log1phoặc np.expm1để tính toán, bạn sẽ nhận được kết quả đáng tin cậy hơn np.loghoặc np.exptheo cách diễn giải từ liên kết này .

0 hữu ích 0 bình luận chia sẻ

Đăng nhập để trả lời câu hỏi

Có thể bạn quan tâm

numpy.log1p(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])= 'log1p'>#

Return the natural logarithm of one plus the input array, element-wise.

Calculates log(1 + x).

Parametersxarray_like

Input values.

outndarray, None, or tuple of ndarray and None, optional

A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.

wherearray_like, optional

This condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None, locations within it where the condition is False will remain uninitialized.

**kwargs

For other keyword-only arguments, see the ufunc docs.

Returnsyndarray

Natural logarithm of 1 + x, element-wise. This is a scalar if x is a scalar.

Notes

For real-valued input, log1p is accurate also for x so small that 1 + x == 1 in floating-point accuracy.

Logarithm is a multivalued function: for each x there is an infinite number of z such that exp(z) = 1 + x. The convention is to return the z whose imaginary part lies in [-pi, pi].

For real-valued input data types, log1p always returns real output. For each value that cannot be expressed as a real number or infinity, it yields nan and sets the invalid floating point error flag.

For complex-valued input, log1p is a complex analytical function that has a branch cut [-inf, -1] and is continuous from above on it. log1p handles the floating-point negative zero as an infinitesimal negative number, conforming to the C99 standard.

References

1

M. Abramowitz and I.A. Stegun, “Handbook of Mathematical Functions”, 10th printing, 1964, pp. 67. https://personal.math.ubc.ca/~cbm/aands/page_67.htm

2

Wikipedia, “Logarithm”. https://en.wikipedia.org/wiki/Logarithm

Examples

>>> np.log1p(1e-99)
1e-99
>>> np.log(1 + 1e-99)
0.0