Python 로그(log) 함수
Python에서 log 함수는 "math"을 import해서 쓸 수 있다.
import math
밑이 2인 로그 함수 (log2x)
import math print(math.log2(1)) # output: 0.0 print(math.log2(2)) # output: 1.0 print(math.log2(4)) # output: 2.0 print(math.log2(5)) # output: 2.321928094887362
밑이 10인 로그 함수 (log10x)
import math print(math.log10(1)) # output: 0.0 print(math.log10(2)) # output: 0.3010299956639812 print(math.log10(10)) # output: 1.0 print(math.log10(100)) # output: 2.0
밑이 e인 로그 함수 (logex or ln x or log x)
밑이 e인 로그 함수를 자연로그라고 부르고 아래의 세가지 방법으로 표기할 수 있다.
logex=ln x=log x
앞의 두가지는 모호함이 없지만, log x는 수학분야에서 자연로그로 사용되는 것이 흔하지만, 다른 분야에선 상용로그(밑이 10인 로그, log10x)로 사용되기도 해서 혼동될 수 있다. 하지만, Python의 함수 이름에서도 알 수 있듯이 log x가 자연로그로 사용되었다. 참고로, "math.ln()"이라는 함수는 없다.
자연로그의 밑 "e"는 다음의 극한값으로 표현되며, python에서 "math.e"로 간단하게 사용할 수 있다.
e=lim
import math print(math.log(1)) # output: 0.0 print(math.log(2)) # output: 0.6931471805599453 print(math.log(2.71828182845904)) # output: 0.9999999999999981 print(math.log(2.718281828459045)) # output: 1.0 print(math.log(math.e)) # output: 1.0 print(math.log(math.e ** 2)) # output: 2.0
자연로그 밑 e 에 해당하는 수를 소숫점 아래 14자리를 써도 1.0이 안나오고 소숫점 아래 15자리를 써야 1.0이 나오지만 계산식에 따라 1.0으로 정확하게 계산되지 않을 수도 있으므로 e 는 되도록 math.e를 쓰는 것이 좋다.
밑이 2, 10, e 가 아닌 로그 함수
밑이 2나 10 또는 e 가 아닌 어떤 수로도 로그 함수를 구할 수 있다. 아래의 예제는 밑이 5인 로그 함수이다.
import math print(math.log(1, 5)) # output: 0.0 print(math.log(2, 5)) # output: 0.43067655807339306 print(math.log(5, 5)) # output: 1.0 print(math.log(5 ** 2, 5)) # output: 2.0 print(math.log(5 ** 3, 5)) # output: 3.0000000000000004 print(math.log(pow(5, 3), 5)) # output: 3.0000000000000004
pow()는 제곱을 구하는 함수라서 '**'와 같은 의미를 가진다. 5의 3 제곱을 log_5 5^3 은 3이 나와야 하지만 정확하게 3.0으로 계산되지 않는 것을 볼 수 있다. 프로그램으로 구할 경우 이런 경우는 충분히 발생할 수 있다. 만약 정확도가 엄청나게 높아야 하는 경우엔 이런 케이스도 고려해야 한다.
math — Mathematical functions — Python 3.9.2 documentation
math — Mathematical functions This module provides access to the mathematical functions defined by the C standard. These functions cannot be used with complex numbers; use the functions of the same name from the cmath module if you require support for co
docs.python.org
자연로그
위키백과, 우리 모두의 백과사전. 자연로그 함수 그래프 자연로그(自然log, 영어: natural logarithm)는 e를 밑으로 하는 로그를 뜻한다. 즉, e x = y {\displaystyle e^{x}=y} 일 때, ln y = x {\displaystyle \ln y=x}
ko.wikipedia.org
'PYTHON' 카테고리의 다른 글
Pyinstaller를 PyCharm에 연결해서 사용하는 방법 (0) | 2019.01.29 |
---|---|
[scapy] import error 해결 방법 (0) | 2019.01.24 |
[scapy] pcap 파일안에 UDP packet은 몇개있을까? (1) | 2019.01.24 |
[scapy] pcap 파일안에 들어있는 전체 패킷 갯수는? (1) | 2019.01.23 |
댓글
이 글 공유하기
다른 글
-
Pyinstaller를 PyCharm에 연결해서 사용하는 방법
Pyinstaller를 PyCharm에 연결해서 사용하는 방법
2019.01.29PyCharm에서 Pyinstaller를 편하게 연결해서 사용하고 싶은데…. 어떻게 하면 될까? File 메뉴에서 setting에 들어간 다음, Tools에서 External tools를 Click! "+"를 누르면 아래와 같은 화면이 나오는데 Name, Description, Program, Arguments, Working directory를 아래와 같이 입력한 다음 "OK"를 클릭한다. Arguments와 Working directory에서 "$"로 묶여있는 부분은 PyCharm에서 정의한 macro이다. "Insert Macro…." 버튼을 눌러서 추가하거나 직접 입력해줘도 된다. Arguments에서 "--onefile"은 말그대로 단 하나의 exe 파일로 만들어주는 옵션이다. "--wind… -
[scapy] import error 해결 방법
[scapy] import error 해결 방법
2019.01.24Python 3.7을 사용하는데 pip로 scapy를 install하고 import하니까 error가 났다. > pip install scapy 이렇게 설치하게 되면 현시점에서 가장 최신버전으로 설치가 된다. 내가 설치한 현시점 최신버전은 2.4.2였다. 근데 googling해보니 이 scapy 2.4.2이 python 3.7과 안맞는 부분이 있나보다. import하면 error를 띄우는데 아래와 같이 uninstall하고 2.4.0 버전으로 다시 설치하니 import할때의 error가 없어졌다. 기능상 문제도 없는 것 같다. uninstall을 꼭 해야 하는지는 잘 모르겠음… > pip uninstall scapy > pip install scapy==2.4.0 -
[scapy] pcap 파일안에 UDP packet은 몇개있을까?
[scapy] pcap 파일안에 UDP packet은 몇개있을까?
2019.01.24.pcap 파일안에 UDP 패킷이 몇개 있는지 어떻게 알 수 있을까? 다음 예제 파일은 test.pcapng 파일을 읽어서 Ethernet/IP/ICMP/TCP/UDP packet이 각각 몇개씩 있는지 확인하고 출력해준다. 핵심인 haslayer() 함수는 각각의 패킷에 어떤 protocol layer가 있는지 확인해준다. Return은 boolean으로 True/False로 확인하면 된다. # Imports import scapy.all as sc pkts = sc.rdpcap('test.pcapng') print('Total packets :', len(pkts)) # init counts count_ether = 0 count_ip = 0 count_icmp = 0 count_tcp = 0 coun… -
[scapy] pcap 파일안에 들어있는 전체 패킷 갯수는?
[scapy] pcap 파일안에 들어있는 전체 패킷 갯수는?
2019.01.23어떤 .pcap 파일안에 들어있는 전체 패킷 갯수는 어떻게 알아낼까? Python scapy를 이용해서 아래와 같이 단 몇줄로 쉽게 알아낼 수 있다. Python version은 3.7이고, IDE는 spyder3를 이용했다. # getTotalNumPackets.py import scapy.all as sc pkts = sc.rdpcap('test.pcapng') print('Total packets =', len(pkts)) 단 세줄로 구현할 수 있는데 첫번째로 scapy의 모든 class를 import하고 sc로 별칭을 정해준다. 그리고 두번째 줄에서 scapy의 rdpcap() 함수를 이용해서 test.pcapng를 parsing하여 모든 패킷들을 pkts에 넣어준다. 여기서 주의할 점은 test…
댓글을 사용할 수 없습니다.