Hướng dẫn dùng erfc function python

❮ Math Methods

Nội dung chính

  • Definition and Usage
  • Parameter Values
  • Technical Details
  • More Examples
  • 014 The cross-entropy error function - Code
  • Đăng nhập để trả lời câu hỏi
  • Có thể bạn quan tâm

Nội dung chính

  • Definition and Usage
  • Parameter Values
  • Technical Details
  • More Examples
  • 014 The cross-entropy error function - Code
  • Đăng nhập để trả lời câu hỏi
  • Có thể bạn quan tâm

Example

Print error function for different numbers:

# Import math Library
import math

# Print error function for different numbers
print (math.erf(0.67))
print (math.erf(1.34))
print (math.erf(-6))

Try it Yourself »


Definition and Usage

The math.erf() method returns the error function of a number.

This method accepts a value between - inf and + inf, and returns a value between - 1 to + 1.


Syntax

Parameter Values

ParameterDescription
x Required. A number to find the error function of

Technical Details

Return Value:A float value, representing the error function of a number
Python Version:3.2

More Examples

Example

Calculate the mathematical error function of the same number, positive and negative:

print (math.erf(1.28))
print (math.erf(-1.28))

Try it Yourself »


❮ Math Methods

Trying to write a function that uses the Trapezium Rule to evaluate the error function integral where we would like the value of the error function and num_traps is the number of trapeziums that the integration interval should be divided into. Using numerical integration and not importing from librariaes. The test code should print: 0.835

import numpy as np
def f(t):
"""The function"""
return np.exp(-t**2)

def trapezium_erf(x, num_traps):
    """Calc error func"""

    d_t = x / num_traps
    area = 0

    for i in range(num_traps):
        trap_start = x
        trap_end = x + d_t
        start_height = f(trap_start)
        end_height = f(trap_end)

        trap_area = ((start_height + end_height) / 2) * d_t 
        area += trap_area
        x = x + d_t
    
    return (2/np.sqrt(np.pi) * area)

#Test code
x = 1.0
area = trapezium_erf(x, 3)
print(f"{area:.3f}")

014 The cross-entropy error function - Code

Sửa lần cuối: Thứ ba, 25 Tháng chín 2018, 2:05 PM

◄ 013 The cross-entropy error function - Theory

Chuyển tới...

015 Visualizing the linear discriminant Bayes classifier Gaussian clouds ►

Tôi đang tìm kiếm một hàm trong Numpy hoặc Scipy (hoặc bất kỳ thư viện Python nghiêm ngặt nào) sẽ cung cấp cho tôi hàm phân phối chuẩn tích lũy trong Python.

  • python
  • numpy
  • scipy
  • statistics

113 hữu ích 0 bình luận 153k xem chia sẻ

answer

138

Hướng dẫn dùng erfc function python

Đây là một ví dụ:

>>> from scipy.stats import norm
>>> norm.cdf(1.96)
0.9750021048517795
>>> norm.cdf(-1.96)
0.024997895148220435

Nói cách khác, khoảng 95% khoảng thông thường chuẩn nằm trong hai độ lệch chuẩn, tập trung vào giá trị trung bình chuẩn bằng 0.

Nếu bạn cần CDF nghịch đảo:

>>> norm.ppf(norm.cdf(1.96))
array(1.9599999999999991)

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

answer

45

Có thể đã quá muộn để trả lời câu hỏi nhưng vì Google vẫn dẫn mọi người đến đây, tôi quyết định viết giải pháp của mình ở đây.

Có nghĩa là, kể từ Python 2.7, maththư viện đã tích hợp hàm lỗimath.erf(x)

Các erf()chức năng có thể được sử dụng để tính toán các chức năng thống kê truyền thống như phân phối chuẩn chuẩn tích lũy:

from math import *
def phi(x):
    #'Cumulative distribution function for the standard normal distribution'
    return (1.0 + erf(x / sqrt(2.0))) / 2.0

Tham khảo:

https://docs.python.org/2/library/math.html

https://docs.python.org/3/library/math.html

Hàm Lỗi và Hàm phân phối Chuẩn có liên quan với nhau như thế nào?

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

answer

29

Bắt đầu Python 3.8, thư viện chuẩn cung cấp NormalDistđối tượng như một phần của statisticsmô-đun.

Nó có thể được sử dụng để lấy hàm phân phối tích lũy ( cdf- xác suất một mẫu ngẫu nhiên X sẽ nhỏ hơn hoặc bằng x) cho một giá trị trung bình nhất định ( mu) và độ lệch chuẩn ( sigma):

from statistics import NormalDist

NormalDist(mu=0, sigma=1).cdf(1.96)
# 0.9750021048517796

Có thể đơn giản hóa cái nào cho phân phối chuẩn chuẩn ( mu = 0sigma = 1):

NormalDist().cdf(1.96)
# 0.9750021048517796

NormalDist().cdf(-1.96)
# 0.024997895148220428

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

answer

18

Phỏng theo đây http://mail.python.org/pipermail/python-list/2000-June/039873.html

from math import *
def erfcc(x):
    """Complementary error function."""
    z = abs(x)
    t = 1. / (1. + 0.5*z)
    r = t * exp(-z*z-1.26551223+t*(1.00002368+t*(.37409196+
        t*(.09678418+t*(-.18628806+t*(.27886807+
        t*(-1.13520398+t*(1.48851587+t*(-.82215223+
        t*.17087277)))))))))
    if (x >= 0.):
        return r
    else:
        return 2. - r

def ncdf(x):
    return 1. - 0.5*erfcc(x/(2**0.5))

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

answer

17

Để xây dựng dựa trên ví dụ của Unknown, hàm tương đương trong Python của hàm normdist () được triển khai trong nhiều thư viện sẽ là:

def normcdf(x, mu, sigma):
    t = x-mu;
    y = 0.5*erfcc(-t/(sigma*sqrt(2.0)));
    if y>1.0:
        y = 1.0;
    return y

def normpdf(x, mu, sigma):
    u = (x-mu)/abs(sigma)
    y = (1/(sqrt(2*pi)*abs(sigma)))*exp(-u*u/2)
    return y

def normdist(x, mu, sigma, f):
    if f:
        y = normcdf(x,mu,sigma)
    else:
        y = normpdf(x,mu,sigma)
    return y

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

answer

11

Câu trả lời của Alex cho bạn thấy một giải pháp cho phân phối chuẩn chuẩn (trung bình = 0, độ lệch chuẩn = 1). Nếu bạn có phân phối chuẩn với meanstd(là sqr(var)) và bạn muốn tính toán:

from scipy.stats import norm

# cdf(x < val)
print norm.cdf(val, m, s)

# cdf(x > val)
print 1 - norm.cdf(val, m, s)

# cdf(v1 < x < v2)
print norm.cdf(v2, m, s) - norm.cdf(v1, m, s)

Đọc thêm về cdf tại đây và thực hiện scipy của phân phối chuẩn với nhiều công thức tại đây .

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

answer

2

Chụp từ trên cao:

from scipy.stats import norm
>>> norm.cdf(1.96)
0.9750021048517795
>>> norm.cdf(-1.96)
0.024997895148220435

Đối với bài kiểm tra hai phía:

Import numpy as np
z = 1.96
p_value = 2 * norm.cdf(-np.abs(z))
0.04999579029644087

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

answer

0

Đơn giản như thế này:

import math
def my_cdf(x):
    return 0.5*(1+math.erf(x/math.sqrt(2)))

Tôi tìm thấy công thức trong trang này https://www.danielsoper.com/statcalc/formulas.aspx?id=55

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