Prepare > Python > Numpy > Polynomials
Polynomials | HackerRank
Given the coefficients, use polynomials in NumPy.
www.hackerrank.com
문제
Task
You are given the coefficients of a polynomial P.
Your task is to find the value of P at point x.
Input Format
The first line contains the space separated value of the coefficients in P.
The second line contains the value of x.
Output Format
Print the desired value.
=> 다항식 P의 계수들을 줄건데 특정 x에서의 P값을 구하라
코드
import numpy as np
P = list(map(float, input().split()))
print(
np.polyval(P, float(input()))
)
노트
np.poly()
print(numpy.poly([-1, 1, 1, 10])) #Output : [ 1 -11 9 11 -10]
- 입력변수를 근으로 하는 다항식의 계수를 리턴
$$ (x+1)*(x-1)*(x-1)*(x-10) $$
$$ x^4-11x^3+9x^2+11x-10 $$
np.roots()
print(numpy.roots([1, 0, -1])) #Output : [-1. 1.]
- 입력변수를 계수로 하는 다항식의 근을 리턴
$$ x^2-1 = 0 $$
np.polyint()
print(numpy.polyint([1, 1, 1]))
#Output : [ 0.33333333 0.5 1. 0. ]
- 입력변수를 계수로 하는 다항식의 역도함수를 리턴(고등학교에서 배우는 적분으로 생각)
$$ \int \:x^2+x+1 dx $$
$$ \frac{x^3}{3}+\frac{x^2}{2}+x+C $$
np.polyder()
print(numpy.polyder([1, 1, 1, 1])) #Output : [3 2 1]
- 입력변수를 계수로하는 다항식의 도함수 리턴
$$ \frac{d}{dx}\left(x^3+x^2+x+1\right) $$
$$ 3x^2+2x+1 $$
np.polyval()
print(numpy.polyval([1, -2, 0, 2], 4)) #Output : 34
- 첫 번째 입력변수를 계수로하는 다항식의
- 두 번째 입력변수에서의 값을 구한다
$$ x^3-2x^2+2 $$
$$ x=4 $$
np.polyfit()
print(numpy.polyfit([0,1,-1, 2, -2], [0,1,1, 4, 4], 2))
#Output : [ 1.00000000e+00 0.00000000e+00 -3.97205465e-16]
- x와 y로 이뤄진 2차원 그래프에서 회귀선의 식을 찾는 함수
- 첫 번째 파라미터: x
- 두 번째 파라미터: y
- 세 번째 파라미터: 회귀선의 차수
참조
Python Numpy 강좌 : 제 16강 - 다항식 계산 (1)
다항식 생성(Poly1d)
076923.github.io
[Numpy] 파이썬 1차/2차/n차 회귀식 구하기 및 시각화 예제(np.polyfit 함수 활용)
Python 넘파이 다항 회귀 예제 : np.polyfit 함수 파이썬에서 numpy 모듈의 polyfit 메소드를 활용하여 1차, 2차 및 n차 다항 회귀식을 구해보고, 결과를 시각화해보는 예시를 다루어보겠습니다. 1차 회귀
jimmy-ai.tistory.com