2020. 4. 20. 12:57ㆍMachine Learning Study
1. normal_eq.py
- fit 메서드 조건
""" |
Linear regression 모델을 적합한다. |
Matrix X와 Vector Y가 입력 값으로 들어오면 Normal equation을 활용하여, weight값을 |
찾는다. 이 때, instance가 생성될 때, fit_intercept 설정에 따라 fit 실행이 달라진다. |
fit을 할 때는 입력되는 X의 값은 반드시 새로운 변수(self._new_X)에 저장 |
된 후 실행되어야 한다. |
fit_intercept가 True일 경우: |
- Matrix X의 0번째 Column에 값이 1인 column vector를추가한다. |
적합이 종료된 후 각 변수의 계수(coefficient 또는 weight값을 의미)는 self._coef와 |
self._intercept_coef에 저장된다. 이때 self._coef는 numpy array을 각 변수항의 |
weight값을 저장한 1차원 vector이며, self._intercept_coef는 상수항의 weight를 |
저장한 scalar(float) 이다. |
Parameters |
---------- |
X : numpy array, 2차원 matrix 형태로 [n_samples,n_features] 구조를 가진다 |
y : numpy array, 1차원 vector 형태로 [n_targets]의 구조를 가진다. |
- predict 메서드 조건
""" |
적합된 Linear regression 모델을 사용하여 입력된 Matrix X의 예측값을 반환한다. |
이 때, 입력된 Matrix X는 별도의 전처리가 없는 상태로 입력되는 걸로 가정한다. |
fit_intercept가 True일 경우: |
- Matrix X의 0번째 Column에 값이 1인 column vector를추가한다. |
normalize가 True일 경우: |
- Standard normalization으로 Matrix X의 column 0(상수)를 제외한 모든 값을 |
정규화을 실행함 |
- 정규화를 할때는 self._mu_X와 self._std_X 에 있는 값을 사용한다. |
Parameters |
---------- |
X : numpy array, 2차원 matrix 형태로 [n_samples,n_features] 구조를 가진다 |
Returns |
------- |
y : numpy array, 예측된 값을 1차원 vector 형태로 [n_predicted_targets]의 |
구조를 가진다. |
""" |
import numpy as np
import pandas as pd
class LinearRegression(object):
def __init__(self, fit_intercept=True, copy_X=True):
self.fit_intercept = fit_intercept
self.copy_X = copy_X
self._coef = None
self._intercept = None
self._new_X = None
def fit(self, X, y):
self._new_X = np.array(X)
y = y.reshape(-1, 1)
if self.fit_intercept:
intercept_vector = np.ones([len(self._new_X), 1])
self._new_X = np.concatenate((intercept_vector, self._new_X), axis=1)
# (X^T * X)^-1
# coef = w0로서, 1차원 vector
weights = np.linalg.inv(
self._new_X.T.dot(self._new_X)).dot(self._new_X.T.dot(y)).flatten()
if selff.fit_intercept:
self._coef = weights[0]
self._intercept = weights[1:]
else:
self._coef = weights
def predict(self, X):
test_X = np.array(X)
if self.fit_intercept:
intercept_vector = np.ones([len(test_X), 1])
test_X = np.concatenate((intercept_vector, test_X), axis=1)
weights = np.concatenate(([self._intercept], self._coef), axis=0)
else:
weights = self._coef
return test_X.dot(weights)
@property
def coef(self):
return self._coef
@property
def intercept(self):
return self._intercept
2. normal_eq_learning.py
import pandas as pd
import numpy as np
import normal_eq
import imp
from sklearn import linear_model
imp.reload(normal_eq)
# Data Load
df = pd.read_csv("data/test.csv", encoding="utf-8")
X = df['x'].values.reshape(-1, 1)
y = df['y'].values
# Build Model
lr = normal_eq.LinearRegression(fit_intercept=True)
lr.fit(X, y)
lr.intercept
lr.coef
X_test = df['x'].values.reshape(-1, 1)
lr.predict(X_test)[:10]
Skitlearn을 이용한 normal equation
sk_lr = linear_model.LinearRegression(normalize=False)
sk_lr.intercept_
sk_lr.coef_
sk_lr.predict(X_test)[:10]
* 매우 간단하다.
'Machine Learning Study' 카테고리의 다른 글
Linear Regression - Gradient Descent (0) | 2020.04.23 |
---|---|
Linear Regression - Normal Equation (0) | 2020.04.20 |
Linear Regression (선형회귀) (0) | 2020.04.17 |
Linear Algebra (선형대수) (0) | 2020.04.16 |
Numpy Study (0) | 2020.04.16 |