Pseudo-Vandermonde 행렬이란?
주어진 차수의 Pseudo-Vandermonde(유사 반데르몽드) 행렬을 생성하려면 Python NumPy의 polyvander2d() 함수를 사용합니다. 이 메서드는 지정된 차수 deg와 샘플 포인트 (x, y)에 대한 유사 반데르몽드 행렬을 반환하며, 주로 2변수 다항식의 최소제곱 피팅이나 회귀 분석에서 계수를 구하는 데 활용됩니다.
매개변수 설명
- x, y – 점 좌표 배열로, 두 배열은 반드시 동일한 형태(shape)를 가져야 합니다.
- dtype 변환 – 배열 요소 중 복소수가 하나라도 포함되어 있으면 complex128로, 그렇지 않으면 float64로 자동 변환됩니다. 스칼라 값은 1차원 배열로 변환됩니다.
- deg – [x_deg, y_deg] 형태의 최대 차수 리스트입니다.
구현 단계
먼저 필요한 라이브러리를 임포트합니다 −
import numpy as np from numpy.polynomial.polynomial import polyvander2d
numpy.array() 메서드를 사용하여 모두 동일한 형태를 가진 점 좌표 배열을 생성합니다 −
x = np.array([0.1, 1.4]) y = np.array([1.7, 2.8])
생성한 배열을 화면에 출력합니다 −
print("Array1...\n",x)
print("\nArray2...\n",y)
배열의 데이터 타입(dtype)을 확인합니다 −
print("\nArray1 datatype...\n",x.dtype)
print("\nArray2 datatype...\n",y.dtype)
두 배열의 차원(ndim)을 확인합니다 −
print("\nDimensions of Array1...\n",x.ndim)
print("\nDimensions of Array2...\n",y.ndim)
두 배열의 형태(shape)를 확인합니다 −
print("\nShape of Array1...\n",x.shape)
print("\nShape of Array2...\n",y.shape)
주어진 차수의 Pseudo-Vandermonde 행렬을 생성하려면 polyvander2d() 함수를 호출합니다 −
x_deg, y_deg = 2, 3
print("\nResult...\n",polyvander2d(x,y, [x_deg, y_deg]))
전체 예제 코드
import numpy as np
from numpy.polynomial.polynomial import polyvander2d
# numpy.array() 메서드로 동일한 형태의 점 좌표 배열 생성
x = np.array([0.1, 1.4])
y = np.array([1.7, 2.8])
# 배열 출력
print("Array1...\n",x)
print("\nArray2...\n",y)
# 데이터 타입 출력
print("\nArray1 datatype...\n",x.dtype)
print("\nArray2 datatype...\n",y.dtype)
# 두 배열의 차원 확인
print("\nDimensions of Array1...\n",x.ndim)
print("\nDimensions of Array2...\n",y.ndim)
# 두 배열의 형태 확인
print("\nShape of Array1...\n",x.shape)
print("\nShape of Array2...\n",y.shape)
# polyvander2d()로 주어진 차수의 Pseudo-Vandermonde 행렬 생성
x_deg, y_deg = 2, 3
print("\nResult...\n",polyvander2d(x,y, [x_deg, y_deg]))
실행 결과
Array1...
[0.1 1.4]
Array2...
[1.7 2.8]
Array1 datatype...
float64
Array2 datatype...
float64
Dimensions of Array1...
1
Dimensions of Array2...
1
Shape of Array1...
(2,)
Shape of Array2...
(2,)
Result...
[[1.000000e+00 1.700000e+00 2.890000e+00 4.913000e+00 1.000000e-01
1.700000e-01 2.890000e-01 4.913000e-01 1.000000e-02 1.700000e-02
2.890000e-02 4.913000e-02]
[1.000000e+00 2.800000e+00 7.840000e+00 2.195200e+01 1.400000e+00
3.920000e+00 1.097600e+01 3.073280e+01 1.960000e+00 5.488000e+00
1.536640e+01 4.302592e+01]]
결과 해석
x_deg=2, y_deg=3으로 설정했기 때문에 각 샘플 포인트마다 x의 0~2차 항과 y의 0~3차 항이 모든 조합으로 곱해져 총 (2+1)×(3+1)=12개의 열을 가진 행렬이 생성됩니다. 결과 행렬의 첫 번째 행은 (0.1, 1.7), 두 번째 행은 (1.4, 2.8) 좌표에 대한 다항식 기저 함수 값들에 해당합니다. 이렇게 만들어진 행렬은 2변수 다항식 회귀 문제를 선형 시스템으로 풀 때 그대로 활용할 수 있습니다.